14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 | class ExtractPlayblast(
plugin.BlenderExtractor, publish.OptionalPyblishPluginMixin
):
"""
Extract viewport playblast.
Takes review camera and creates review Quicktime video based on viewport
capture.
"""
label = "Extract Playblast"
hosts = ["blender"]
families = ["review"]
optional = True
order = pyblish.api.ExtractorOrder + 0.01
presets = "{}"
def process(self, instance):
if not self.is_active(instance.data):
return
# get scene fps
fps = instance.data.get("fps")
if fps is None:
fps = bpy.context.scene.render.fps
instance.data["fps"] = fps
self.log.debug(f"fps: {fps}")
# If start and end frames cannot be determined,
# get them from Blender timeline.
start = instance.data.get("frameStart", bpy.context.scene.frame_start)
end = instance.data.get("frameEnd", bpy.context.scene.frame_end)
self.log.debug(f"start: {start}, end: {end}")
assert end >= start, "Invalid time range!"
# get cameras
camera = instance.data("review_camera", None)
# get isolate objects list
isolate = instance.data("isolate", None)
# get output path
stagingdir = self.staging_dir(instance)
folder_name = instance.data["folderEntity"]["name"]
product_name = instance.data["productName"]
filename = f"{folder_name}_{product_name}"
path = os.path.join(stagingdir, filename)
self.log.debug(f"Outputting images to {path}")
presets = json.loads(self.presets)
preset = presets.get("default")
preset.update({
"camera": camera,
"start_frame": start,
"end_frame": end,
"filename": path,
"overwrite": True,
"isolate": isolate,
})
preset.setdefault(
"image_settings",
{
"file_format": "PNG",
"color_mode": "RGB",
"color_depth": "8",
"compression": 15,
},
)
with maintained_time():
path = capture(**preset)
self.log.debug(f"playblast path {path}")
self._maintain_publisher_focus()
collected_files = os.listdir(stagingdir)
collections, remainder = clique.assemble(
collected_files,
patterns=[f"{filename}\\.{clique.DIGITS_PATTERN}\\.png$"],
minimum_items=1
)
if len(collections) > 1:
raise RuntimeError(
f"More than one collection found in stagingdir: {stagingdir}"
)
elif len(collections) == 0:
raise RuntimeError(
f"No collection found in stagingdir: {stagingdir}"
)
frame_collection = collections[0]
self.log.debug(f"Found collection of interest {frame_collection}")
# `instance.data["files"]` must be `str` if single frame
files = list(frame_collection)
if len(files) == 1:
files = files[0]
tags = ["review"]
if not instance.data.get("keepImages"):
tags.append("delete")
representation = {
"name": "png",
"ext": "png",
"files": files,
"stagingDir": stagingdir,
"frameStart": start,
"frameEnd": end,
"fps": fps,
"tags": tags,
"camera_name": camera
}
instance.data.setdefault("representations", []).append(representation)
def _maintain_publisher_focus(self):
"""Restore publisher at the top widget by using bpy.app.timers."""
def delayed_publisher_restore():
"""Delayed call to bring publisher window back to front."""
# Double-check availability before calling
if not hasattr(bpy.ops.wm, 'ayon_publisher'):
return
bpy.ops.wm.ayon_publisher()
self.log.debug("Publisher focus restoration setup completed")
bpy.app.timers.register(
delayed_publisher_restore,
first_interval=0.1
)
|