Skip to content

validate_render_output_paths

ValidateCompositorNodeFileOutputPaths

Bases: BlenderInstancePlugin, OptionalPyblishPluginMixin

Validate output render paths from the Compositor Node Output File.

This validator checks that the render output paths set in the CompositorNodeOutputFile adhere to a few strict requirements: - The output base path must include the workfile name in the output path. - The output filename must end with .{frame}.{ext} where it is fine if the path on the node is set as filename. because if frame number and extension are missing Blender will automatically append them.

Source code in client/ayon_blender/plugins/publish/validate_render_output_paths.py
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
class ValidateCompositorNodeFileOutputPaths(
    plugin.BlenderInstancePlugin,
    OptionalPyblishPluginMixin
):
    """Validate output render paths from the Compositor Node Output File.

    This validator checks that the render output paths set in the
    `CompositorNodeOutputFile` adhere to a few strict requirements:
    - The output base path must include the workfile name in the output path.
    - The output filename must end with `.{frame}.{ext}` where it is fine
      if the path on the node is set as `filename.` because if frame number
      and extension are missing Blender will automatically append them.
    """

    order = ValidateContentsOrder
    families = ["render"]
    hosts = ["blender"]
    label = "Validate Compositor Node File Output Paths"
    optional = True
    actions = [RepairAction]

    # TODO: Fix validator - it should just validate against the pre-collected
    #  expected output files instead so that we do not need to duplicate the
    #  logic of exactly figuring out the output filepaths.

    def process(self, instance):
        if not self.is_active(instance.data):
            return

        invalid_error_message = self.get_invalid(instance)
        if invalid_error_message:
            raise PublishValidationError(
                invalid_error_message,
                title="Invalid compositor render outputs",
                description=self.get_description()
            )

    @classmethod
    def get_invalid(cls, instance) -> Optional[str]:

        workfile_filepath: str = bpy.data.filepath
        if not workfile_filepath:
            cls.log.warning("No workfile scene filepath set. "
                            "Please save the workfile.")
            return None

        workfile_filename = os.path.basename(workfile_filepath)
        workfile_filename_no_ext, _ext = os.path.splitext(workfile_filename)

        # Get expected files per AOV
        expected_files: dict[str, list[str]] = (
            instance.data["expectedFiles"][0]
        )

        # For each AOV output check the output filenames as they must end with
        # `.{frame}.{ext}` where the frame is a number and ext is the extension
        for _aov, output_files in expected_files.items():
            first_file = output_files[0]

            # Ensure filename ends with `.{frame}.{ext}` by checking whether
            file_no_ext = os.path.splitext(first_file)[0]
            if not file_no_ext[-1].isdigit():
                cls.log.warning(
                    f"Output file '{first_file}' does not end with "
                    "`.{frame}.{extension}`."
                )
                return (
                    "Output file does not contain a frame number before the "
                    "extension."
                )

            # Before the digits there must be a dot `.`
            file_no_frame = file_no_ext.rstrip("1234567890")
            if not file_no_frame.endswith("."):
                cls.log.warning(
                    f"Output file '{first_file}' does not end with "
                    "`.{frame}.{extension}`."
                )
                return (
                    "Output file does not end with a dot separator before the "
                    "frame number."
                )

            if workfile_filename_no_ext not in first_file:
                return (
                    "Render output does not include workfile name: "
                    f"{workfile_filename_no_ext}.\n\n"
                    "Use Repair action to fix the render base filepath."
                )

        return None

    @classmethod
    def repair(cls, instance):
        """Update the render output path to include the scene name."""
        output_node: "bpy.types.CompositorNodeOutputFile" = (
            instance.data["transientData"]["instance_node"]
        )

        # Check whether CompositorNodeOutputFile is rendering to multilayer EXR
        file_format: str = output_node.format.file_format
        is_multilayer: bool = file_format == "OPEN_EXR_MULTILAYER"

        filename = os.path.basename(bpy.data.filepath)
        filename, ext = os.path.splitext(filename)
        orig_output_path = output_node.base_path
        if is_multilayer:
            # If the output node is a multilayer EXR then the base path
            # includes the render filename like `Main_beauty.####.exr`
            # So we split that off, and assume that the parent folder to
            # the filename is the workfile filename named folder.
            render_folder, render_filename = os.path.split(orig_output_path)
            output_node_dir = os.path.dirname(render_folder)
            new_output_dir = os.path.join(output_node_dir,
                                          filename,
                                          render_filename)
        else:
            output_node_dir = os.path.dirname(orig_output_path)
            new_output_dir = os.path.join(output_node_dir, filename)

        output_node.base_path = new_output_dir

        # Repair all output filenames to ensure they end with `.{frame}.{ext}`
        base_path: str = output_node.base_path

        if is_multilayer:
            file_format = output_node.format.file_format
            ext = render_lib.get_file_format_extension(file_format)
            ext = f".{ext}"
            output_node.base_path = fix_filename(base_path, extension=ext)
        else:
            for file_slot in output_node.file_slots:
                if file_slot.use_node_format:
                    file_format = output_node.format.file_format
                else:
                    file_format = file_slot.format.file_format

                ext = render_lib.get_file_format_extension(file_format)
                ext = f".{ext}"
                file_slot.path = fix_filename(file_slot.path, extension=ext)

    @staticmethod
    def get_description():
        return inspect.cleandoc("""
        ### Compositor Output Filepaths Invalid

        The Output File node in the Compositor has invalid output paths.

        The filepaths must:

        - Include the workfile name in the output path, this is to ensure
          unique render paths for each workfile version.

        - End with `.####.{ext}`. It is allowed to specify no extension and
          frame tokens at all. As such, `filename.` is valid, because if frame
          number and extension are missing Blender will automatically append
          them.
        """)

repair(instance) classmethod

Update the render output path to include the scene name.

Source code in client/ayon_blender/plugins/publish/validate_render_output_paths.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
@classmethod
def repair(cls, instance):
    """Update the render output path to include the scene name."""
    output_node: "bpy.types.CompositorNodeOutputFile" = (
        instance.data["transientData"]["instance_node"]
    )

    # Check whether CompositorNodeOutputFile is rendering to multilayer EXR
    file_format: str = output_node.format.file_format
    is_multilayer: bool = file_format == "OPEN_EXR_MULTILAYER"

    filename = os.path.basename(bpy.data.filepath)
    filename, ext = os.path.splitext(filename)
    orig_output_path = output_node.base_path
    if is_multilayer:
        # If the output node is a multilayer EXR then the base path
        # includes the render filename like `Main_beauty.####.exr`
        # So we split that off, and assume that the parent folder to
        # the filename is the workfile filename named folder.
        render_folder, render_filename = os.path.split(orig_output_path)
        output_node_dir = os.path.dirname(render_folder)
        new_output_dir = os.path.join(output_node_dir,
                                      filename,
                                      render_filename)
    else:
        output_node_dir = os.path.dirname(orig_output_path)
        new_output_dir = os.path.join(output_node_dir, filename)

    output_node.base_path = new_output_dir

    # Repair all output filenames to ensure they end with `.{frame}.{ext}`
    base_path: str = output_node.base_path

    if is_multilayer:
        file_format = output_node.format.file_format
        ext = render_lib.get_file_format_extension(file_format)
        ext = f".{ext}"
        output_node.base_path = fix_filename(base_path, extension=ext)
    else:
        for file_slot in output_node.file_slots:
            if file_slot.use_node_format:
                file_format = output_node.format.file_format
            else:
                file_format = file_slot.format.file_format

            ext = render_lib.get_file_format_extension(file_format)
            ext = f".{ext}"
            file_slot.path = fix_filename(file_slot.path, extension=ext)

ValidateSceneRenderFilePath

Bases: BlenderInstancePlugin, OptionalPyblishPluginMixin

Validate Scene Render Output File Path is not empty.

Validates bpy.context.scene.render.filepath is set to a valid directory.

Source code in client/ayon_blender/plugins/publish/validate_render_output_paths.py
 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
class ValidateSceneRenderFilePath(
    plugin.BlenderInstancePlugin,
    OptionalPyblishPluginMixin
):
    """Validate Scene Render Output File Path is not empty.

    Validates `bpy.context.scene.render.filepath` is set to a valid directory.
    """
    order = ValidateContentsOrder
    families = ["render"]
    hosts = ["blender"]
    label = "Validate Scene Render Filepath"
    optional = True
    actions = [RepairAction]

    def process(self, instance):
        if not self.is_active(instance.data):
            return

        if not bpy.data.filepath:
            # Blender workfile is not saved, so we can't validate the
            # scene render filepath correctly.
            self.log.warning(
                "Blender workfile is not saved. "
                "Please save the workfile before publishing."
            )
            return

        expected_render_path = self._get_expected_render_path(instance)
        if (
            Path(bpy.context.scene.render.filepath) !=
            Path(expected_render_path)
        ):
            self.log.warning(
                f"Current scene output: {bpy.context.scene.render.filepath} "
            )
            self.log.info(f"Expected scene output: {expected_render_path}")
            raise PublishValidationError(
                message=(
                    "Scene Render filepath not set correctly. "
                    "Use Repair action to fix the render filepath."
                ),
                description=self.get_description(),
                title="Invalid scene render filepath set"
            )

        if not bpy.context.scene.render.use_overwrite:
            raise PublishValidationError(
                title="Scene render overwrite is disabled",
                message="Scene Render overwrite is disabled.",
                description=(
                    "### Scene Render Overwrite Disabled\n\n"
                    "It's recommended to enable this so that requeue on farm "
                    "will not skip rendering just because the file already "
                    "exists. Use Repair action to enable overwrite."
                )
            )

    @staticmethod
    def _get_expected_render_path(instance: pyblish.api.Instance) -> str:
        """Get the expected render path based on the current scene."""
        project_settings = instance.context.data["project_settings"]
        return render_lib.get_tmp_scene_render_output_path(project_settings)

    @classmethod
    def repair(cls, instance):
        project_settings = instance.context.data["project_settings"]
        render_lib.set_tmp_scene_render_output_path(project_settings)

        # Force enable overwrite so re-queue on the farm does not stop just
        # because a file already exists.
        bpy.context.scene.render.use_overwrite = True

        bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)

    @staticmethod
    def get_description():
        return inspect.cleandoc("""
        ### Scene render filepath invalid

        The scene output filepath is set to incorrectly.

        We are enforcing the scene output filepath to be set to a `tmp`
        file inside the renders folder of the work directory. This is because
        the scene render output is unused by AYON since we only manage the
        Compositor's Output File node for render outputs. The scene wide render
        outputs can't be disabled, so we set it to a temporary filepath.

        The temporary filepath is unique per workfile version to avoid 
        conflicts of different workfile versions being rendered simultaneous 
        on the farm and resulting in write locks on the same file.
        """)

fix_filename(path, extension=None)

Ensure the filename ends with .{frame}.{ext}.

It's also fine for the path to not specify frame number and extension, in which case Blender will automatically add it.

Examples:

>>> fix_filename("folder/beauty")
'folder/beauty.'
>>> fix_filename("folder/beauty#.exr")
'folder/beauty.#.exr'
>>> fix_filename("test.exr")
'test.####.exr'
>>> fix_filename("test.", extension=".exr")
'test.'
>>> fix_filename("test.####.aov.exr", extension=".png")
'test.aov.####.png'

Parameters:

Name Type Description Default
path str

The file path to fix.

required
extension Optional[str]

The file extension to use. If not provided, it will be inferred from the filename if it has an extension. If the path does not have an extension, the extensions argument will remain unused. The extension should start with a dot, e.g. .exr.

None

Returns:

Name Type Description
str str

The fixed file path with the filename ending in .{frame}.{ext}.

Source code in client/ayon_blender/plugins/publish/validate_render_output_paths.py
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
def fix_filename(path: str, extension: Optional[str] = None) -> str:
    """Ensure the filename ends with `.{frame}.{ext}`.

    It's also fine for the path to not specify frame number
    and extension, in which case Blender will automatically add it.

    Examples:
        >>> fix_filename("folder/beauty")
        'folder/beauty.'

        >>> fix_filename("folder/beauty#.exr")
        'folder/beauty.#.exr'

        >>> fix_filename("test.exr")
        'test.####.exr'

        >>> fix_filename("test.", extension=".exr")
        'test.'

        >>> fix_filename("test.####.aov.exr", extension=".png")
        'test.aov.####.png'

    Arguments:
        path (str): The file path to fix.
        extension (Optional[str]): The file extension to use. If not provided,
            it will be inferred from the filename if it has an extension.
            If the `path` does not have an extension, the `extensions` argument
            will remain unused. The extension should start with a dot, e.g.
            `.exr`.

    Returns:
        str: The fixed file path with the filename ending in `.{frame}.{ext}`.

    """
    folder, filename = os.path.split(path)

    # Get characteristics of the current filename to determine what
    # we want to preserve.
    has_extension: bool = bool(re.search(r".*\.[A-Za-z]+$", filename))
    if extension is None and has_extension:
        extension = os.path.splitext(filename)[-1]
    has_frame_token: bool = "#" in filename
    frame_padding: int = filename.count("#") or 4

    # Remove extension and frame tokens
    if has_extension:
        filename = os.path.splitext(filename)[0]
    if has_frame_token:
        # remove any dots with frame tokens to avoid e.g. `test.####.aov.exr`
        # becoming `test..aov.exr`
        filename = filename.replace(".#", "")
        filename = filename.replace("#", "")

    # Remove any trailing dots or underscores
    filename = filename.rstrip("._")

    filename += "."  # Ensure there's a dot before the frame number
    if has_extension or has_frame_token:
        filename += f"{'#' * frame_padding}"
    if has_extension:
        filename += extension

    return os.path.join(folder, filename)