Skip to content

plugin

GenericCreateSaver

Bases: Creator

Source code in client/ayon_fusion/api/plugin.py
 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
class GenericCreateSaver(Creator):
    default_variants = ["Main", "Mask"]
    description = "Fusion Saver to generate image sequence"
    icon = "fa5.eye"

    instance_attributes = [
        "reviewable"
    ]

    settings_category = "fusion"

    image_format = "exr"

    # TODO: This should be renamed together with Nuke so it is aligned
    temp_rendering_path_template = (
        "{workdir}/renders/fusion/{product[name]}/"
        "{product[name]}.{frame}.{ext}"
    )

    def create(self, product_name, instance_data, pre_create_data):
        self.pass_pre_attributes_to_instance(instance_data, pre_create_data)

        instance = CreatedInstance(
            product_type=self.product_type,
            product_name=product_name,
            data=instance_data,
            creator=self,
        )
        data = instance.data_to_store()
        comp = get_current_comp()
        with comp_lock_and_undo_chunk(comp):
            args = (-32768, -32768)  # Magical position numbers
            saver = comp.AddTool("Saver", *args)

            self._update_tool_with_data(saver, data=data)

        # Register the CreatedInstance
        self._imprint(saver, data)

        # Insert the transient data
        instance.transient_data["tool"] = saver

        self._add_instance_to_context(instance)

        return instance

    def collect_instances(self):
        comp = get_current_comp()
        tools = comp.GetToolList(False, "Saver").values()
        for tool in tools:
            data = self.get_managed_tool_data(tool)
            if not data:
                continue

            # Add instance
            created_instance = CreatedInstance.from_existing(data, self)

            # Collect transient data
            created_instance.transient_data["tool"] = tool

            self._add_instance_to_context(created_instance)

    def update_instances(self, update_list):
        for created_inst, _changes in update_list:
            new_data = created_inst.data_to_store()
            tool = created_inst.transient_data["tool"]
            self._update_tool_with_data(tool, new_data)
            self._imprint(tool, new_data)

    def remove_instances(self, instances):
        for instance in instances:
            # Remove the tool from the scene

            tool = instance.transient_data["tool"]
            if tool:
                tool.Delete()

            # Remove the collected CreatedInstance to remove from UI directly
            self._remove_instance_from_context(instance)

    def _imprint(self, tool, data):
        # Save all data in a "openpype.{key}" = value data

        # Instance id is the tool's name so we don't need to imprint as data
        data.pop("instance_id", None)

        active = data.pop("active", None)
        if active is not None:
            # Use active value to set the passthrough state
            tool.SetAttrs({"TOOLB_PassThrough": not active})

        for key, value in data.items():
            tool.SetData(f"openpype.{key}", value)

    def _update_tool_with_data(self, tool, data):
        """Update tool node name and output path based on product data"""
        if "productName" not in data:
            return

        original_product_name = tool.GetData("openpype.productName")
        original_format = tool.GetData(
            "openpype.creator_attributes.image_format"
        )

        product_name = data["productName"]
        if (
            original_product_name != product_name
            or tool.GetData("openpype.task") != data["task"]
            or tool.GetData("openpype.folderPath") != data["folderPath"]
            or original_format != data["creator_attributes"]["image_format"]
        ):
            self._configure_saver_tool(data, tool, product_name)

    def _configure_saver_tool(self, data, tool, product_name):
        formatting_data = deepcopy(data)

        # get frame padding from anatomy templates
        frame_padding = self.project_anatomy.templates_obj.frame_padding

        # get output format
        ext = data["creator_attributes"]["image_format"]

        # Product name and type
        product_type = formatting_data["productType"]
        f_product_name = formatting_data["productName"]

        # Get instance context entities
        project_entity = self.create_context.get_current_project_entity()
        folder_path: str = data["folderPath"]
        task_name: str = data["task"]
        folder_entity = None
        task_entity = None
        if folder_path:
            folder_entity = self.create_context.get_folder_entity(folder_path)
            if task_name:
                task_entity = self.create_context.get_task_entity(
                    folder_path, task_name)

        # If the folder path and task do not match the current context then the
        # workdir is not just the `AYON_WORKDIR`. Hence, we need to actually
        # compute the resulting workdir
        if (
            folder_path == self.create_context.get_current_folder_path()
            and task_name == self.create_context.get_current_task_name()
        ):
            workdir = os.path.normpath(os.getenv("AYON_WORKDIR"))
        else:
            # Note: This may error when no task is set for the instance
            #  however default render template would include task name anyway
            #  disallowing the instance to have no task set at an earlier stage
            #  already.
            workdir = get_workdir(
                project_entity=project_entity,
                folder_entity=folder_entity,
                task_entity=task_entity,
                host_name=self.create_context.host_name,
            )

        formatting_data.update({
            "workdir": workdir,
            "frame": "0" * frame_padding,
            "ext": ext,
            "product": {
                "name": f_product_name,
                "type": product_type,
            },
            # Backwards compatibility
            "subset": f_product_name,
            "family": product_type,
        })

        # build file path to render
        temp_rendering_path_template = (
            self.temp_rendering_path_template
            .replace("{task}", "{task[name]}")
        )

        extra_data = get_template_data(
            project_entity,
            folder_entity,
            task_entity,
            host_name=self.create_context.host_name,
            settings=self.create_context.get_current_project_settings(),
        )
        anatomy = self.create_context.project_anatomy
        extra_data["root"] = anatomy.roots
        formatting_data.update(extra_data)

        filepath = temp_rendering_path_template.format(**formatting_data)

        comp = get_current_comp()
        tool["Clip"] = comp.ReverseMapPath(os.path.normpath(filepath))

        # Rename tool
        if tool.Name != product_name:
            print(f"Renaming {tool.Name} -> {product_name}")
            tool.SetAttrs({"TOOLS_Name": product_name})

    def get_managed_tool_data(self, tool):
        """Return data of the tool if it matches creator identifier"""
        data = tool.GetData("openpype")
        if not isinstance(data, dict):
            return

        if (
            data.get("creator_identifier") != self.identifier
            or data.get("id") not in {
                AYON_INSTANCE_ID, AVALON_INSTANCE_ID
            }
        ):
            return

        # Get active state from the actual tool state
        attrs = tool.GetAttrs()
        passthrough = attrs["TOOLB_PassThrough"]
        data["active"] = not passthrough

        # Override publisher's UUID generation because tool names are
        # already unique in Fusion in a comp
        data["instance_id"] = tool.Name

        return data

    def get_instance_attr_defs(self):
        """Settings for publish page"""
        return self.get_pre_create_attr_defs()

    def pass_pre_attributes_to_instance(self, instance_data, pre_create_data):
        creator_attrs = instance_data["creator_attributes"] = {}
        for pass_key in pre_create_data.keys():
            creator_attrs[pass_key] = pre_create_data[pass_key]

    def _get_render_target_enum(self):
        rendering_targets = {
            "local": "Local machine rendering",
            "frames": "Use existing frames",
        }
        if "farm_rendering" in self.instance_attributes:
            rendering_targets["farm"] = "Farm rendering"

        return EnumDef(
            "render_target", items=rendering_targets, label="Render target"
        )

    def _get_reviewable_bool(self):
        return BoolDef(
            "review",
            default=("reviewable" in self.instance_attributes),
            label="Review",
        )

    def _get_image_format_enum(self):
        image_format_options = ["exr", "tga", "tif", "png", "jpg"]
        return EnumDef(
            "image_format",
            items=image_format_options,
            default=self.image_format,
            label="Output Image Format",
        )

get_instance_attr_defs()

Settings for publish page

Source code in client/ayon_fusion/api/plugin.py
246
247
248
def get_instance_attr_defs(self):
    """Settings for publish page"""
    return self.get_pre_create_attr_defs()

get_managed_tool_data(tool)

Return data of the tool if it matches creator identifier

Source code in client/ayon_fusion/api/plugin.py
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def get_managed_tool_data(self, tool):
    """Return data of the tool if it matches creator identifier"""
    data = tool.GetData("openpype")
    if not isinstance(data, dict):
        return

    if (
        data.get("creator_identifier") != self.identifier
        or data.get("id") not in {
            AYON_INSTANCE_ID, AVALON_INSTANCE_ID
        }
    ):
        return

    # Get active state from the actual tool state
    attrs = tool.GetAttrs()
    passthrough = attrs["TOOLB_PassThrough"]
    data["active"] = not passthrough

    # Override publisher's UUID generation because tool names are
    # already unique in Fusion in a comp
    data["instance_id"] = tool.Name

    return data