Skip to content

validate_rendersettings

Maya validator for render settings.

ValidateRenderSettings

Bases: MayaInstancePlugin, OptionalPyblishPluginMixin

Validates the global render settings

  • File Name Prefix must start with: <Scene> all other token are customizable but sane values for Arnold are:

    <Scene>/<RenderLayer>/<RenderLayer>_<RenderPass>

    token is supported also, useful for multiple renderable cameras per render layer.

    For Redshift omit token. Redshift will append it automatically if AOVs are enabled and if you user Multipart EXR it doesn't make much sense.

  • Frame Padding must be:

    • default: 4
  • Animation must be toggled on, in Render Settings - Common tab:

    • vray: Animation on standard of specific
    • arnold: Frame / Animation ext: Any choice without "(Single Frame)"
    • redshift: Animation toggled on
NOTE

The repair function of this plugin does not repair the animation setting of the render settings due to multiple possibilities.

Source code in client/ayon_maya/plugins/publish/validate_rendersettings.py
 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
class ValidateRenderSettings(plugin.MayaInstancePlugin,
                             OptionalPyblishPluginMixin):
    """Validates the global render settings

    * File Name Prefix must start with: `<Scene>`
        all other token are customizable but sane values for Arnold are:

        `<Scene>/<RenderLayer>/<RenderLayer>_<RenderPass>`

        <Camera> token is supported also, useful for multiple renderable
        cameras per render layer.

        For Redshift omit <RenderPass> token. Redshift will append it
        automatically if AOVs are enabled and if you user Multipart EXR
        it doesn't make much sense.

    * Frame Padding must be:
        * default: 4

    * Animation must be toggled on, in Render Settings - Common tab:
        * vray: Animation on standard of specific
        * arnold: Frame / Animation ext: Any choice without "(Single Frame)"
        * redshift: Animation toggled on

    NOTE:
        The repair function of this plugin does not repair the animation
        setting of the render settings due to multiple possibilities.

    """

    order = ValidateContentsOrder
    label = "Validate Render Settings"
    hosts = ["maya"]
    families = ["renderlayer"]
    actions = [RepairAction]
    optional = True

    ImagePrefixes = {
        'mentalray': 'defaultRenderGlobals.imageFilePrefix',
        'vray': 'vraySettings.fileNamePrefix',
        'arnold': 'defaultRenderGlobals.imageFilePrefix',
        'renderman': 'rmanGlobals.imageFileFormat',
        'redshift': 'defaultRenderGlobals.imageFilePrefix',
        'mayahardware2': 'defaultRenderGlobals.imageFilePrefix',
    }

    ImagePrefixTokens = {
        'mentalray': '<Scene>/<RenderLayer>/<RenderLayer>{aov_separator}<RenderPass>',  # noqa: E501
        'arnold': '<Scene>/<RenderLayer>/<RenderLayer>{aov_separator}<RenderPass>',  # noqa: E501
        'redshift': '<Scene>/<RenderLayer>/<RenderLayer>',
        'vray': '<Scene>/<Layer>/<Layer>',
        'renderman': '<layer>{aov_separator}<aov>.<f4>.<ext>',
        'mayahardware2': '<Scene>/<RenderLayer>/<RenderLayer>',
    }

    _aov_chars = {
        "dot": ".",
        "dash": "-",
        "underscore": "_"
    }

    redshift_AOV_prefix = "<BeautyPath>/<BeautyFile>{aov_separator}<RenderPass>"  # noqa: E501

    renderman_dir_prefix = "<scene>/<layer>"

    R_AOV_TOKEN = re.compile(
        r'%a|<aov>|<renderpass>', re.IGNORECASE)
    R_LAYER_TOKEN = re.compile(
        r'%l|<layer>|<renderlayer>', re.IGNORECASE)
    R_CAMERA_TOKEN = re.compile(r'%c|Camera>')
    R_SCENE_TOKEN = re.compile(r'%s|<scene>', re.IGNORECASE)

    DEFAULT_PADDING = 4
    VRAY_PREFIX = "<Scene>/<Layer>/<Layer>"
    DEFAULT_PREFIX = "<Scene>/<RenderLayer>/<RenderLayer>_<RenderPass>"

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

        invalid = self.get_invalid(instance)
        if invalid:
            raise PublishValidationError(
                title="Invalid Render Settings",
                message=("Invalid render settings found "
                         "for '{}'!".format(instance.name))
            )

    @classmethod
    def get_invalid(cls, instance):

        invalid = False

        renderer = instance.data['renderer']
        layer = instance.data['renderlayer']
        cameras = instance.data.get("cameras", [])

        # Prefix attribute can return None when a value was never set
        prefix = lib.get_attr_in_layer(cls.ImagePrefixes[renderer],
                                       layer=layer) or ""
        padding = lib.get_attr_in_layer(
            attr=RenderSettings.get_padding_attr(renderer),
            layer=layer
        )

        anim_override = lib.get_attr_in_layer("defaultRenderGlobals.animation",
                                              layer=layer)

        prefix = prefix.replace(
            "{aov_separator}", instance.data.get("aovSeparator", "_"))

        default_prefix = cls.ImagePrefixTokens[renderer]

        if not anim_override:
            invalid = True
            cls.log.error("Animation needs to be enabled. Use the same "
                          "frame for start and end to render single frame")

        if not re.search(cls.R_LAYER_TOKEN, prefix):
            invalid = True
            cls.log.error("Wrong image prefix [ {} ] - "
                          "doesn't have: '<renderlayer>' or "
                          "'<layer>' token".format(prefix))

        if len(cameras) > 1 and not re.search(cls.R_CAMERA_TOKEN, prefix):
            invalid = True
            cls.log.error("Wrong image prefix [ {} ] - "
                          "doesn't have: '<Camera>' token".format(prefix))
            cls.log.error(
                "Note that to needs to have capital 'C' at the beginning")

        # renderer specific checks
        if renderer == "vray":
            vray_settings = cmds.ls(type="VRaySettingsNode")
            if not vray_settings:
                node = cmds.createNode("VRaySettingsNode")
            else:
                node = vray_settings[0]

            scene_sep = cmds.getAttr(
                "{}.fileNameRenderElementSeparator".format(node))
            if scene_sep != instance.data.get("aovSeparator", "_"):
                cls.log.error("AOV separator is not set correctly.")
                invalid = True

        if renderer == "redshift":
            redshift_AOV_prefix = cls.redshift_AOV_prefix.replace(
                "{aov_separator}", instance.data.get("aovSeparator", "_")
            )
            if re.search(cls.R_AOV_TOKEN, prefix):
                invalid = True
                cls.log.error(("Do not use AOV token [ {} ] - "
                               "Redshift is using image prefixes per AOV so "
                               "it doesn't make much sense using it in global"
                               "image prefix").format(prefix))
            # get redshift AOVs
            rs_aovs = cmds.ls(type="RedshiftAOV", referencedNodes=False)
            for aov in rs_aovs:
                aov_prefix = cmds.getAttr("{}.filePrefix".format(aov))
                # check their image prefix
                if aov_prefix != redshift_AOV_prefix:
                    cls.log.error(("AOV ({}) image prefix is not set "
                                   "correctly {} != {}").format(
                        cmds.getAttr("{}.name".format(aov)),
                        aov_prefix,
                        redshift_AOV_prefix
                    ))
                    invalid = True

                # check aov file format
                aov_ext = cmds.getAttr("{}.fileFormat".format(aov))
                default_ext = cmds.getAttr("redshiftOptions.imageFormat")
                aov_type = cmds.getAttr("{}.aovType".format(aov))
                if aov_type == "Cryptomatte":
                    # redshift Cryptomatte AOV always uses "Cryptomatte (EXR)"
                    # so we ignore validating file format for it.
                    pass

                elif default_ext != aov_ext:
                    labels = get_redshift_image_format_labels()
                    cls.log.error(
                        "AOV file format {} does not match global file format "
                        "{}".format(labels[aov_ext], labels[default_ext])
                    )
                    invalid = True

        if renderer == "renderman":
            file_prefix = cmds.getAttr("rmanGlobals.imageFileFormat")
            dir_prefix = cmds.getAttr("rmanGlobals.imageOutputDir")

            if file_prefix.lower() != prefix.lower():
                invalid = True
                cls.log.error("Wrong image prefix [ {} ]".format(file_prefix))

            if dir_prefix.lower() != cls.renderman_dir_prefix.lower():
                invalid = True
                cls.log.error("Wrong directory prefix [ {} ]".format(
                    dir_prefix))

        if renderer == "arnold":
            multipart = cmds.getAttr("defaultArnoldDriver.mergeAOVs")
            if multipart:
                if re.search(cls.R_AOV_TOKEN, prefix):
                    invalid = True
                    cls.log.error("Wrong image prefix [ {} ] - "
                                  "You can't use '<renderpass>' token "
                                  "with merge AOVs turned on".format(prefix))
                default_prefix = re.sub(
                    cls.R_AOV_TOKEN, "", default_prefix)
                # remove aov token from prefix to pass validation
                default_prefix = default_prefix.split("{aov_separator}")[0]
            elif not re.search(cls.R_AOV_TOKEN, prefix):
                invalid = True
                cls.log.error("Wrong image prefix [ {} ] - "
                              "doesn't have: '<renderpass>' or "
                              "token".format(prefix))

        default_prefix = default_prefix.replace(
            "{aov_separator}", instance.data.get("aovSeparator", "_"))
        if prefix.lower() != default_prefix.lower():
            cls.log.warning("warning: prefix differs from "
                            "recommended {}".format(
                                default_prefix))

        if padding != cls.DEFAULT_PADDING:
            invalid = True
            cls.log.error("Expecting padding of {} ( {} )".format(
                cls.DEFAULT_PADDING, "0" * cls.DEFAULT_PADDING))

        # load validation definitions from settings
        settings_lights_flag = instance.context.data["project_settings"].get(
            "maya", {}).get(
            "render_settings", {}).get(
            "enable_all_lights", False)

        instance_lights_flag = instance.data.get("renderSetupIncludeLights")
        if settings_lights_flag != instance_lights_flag:
            cls.log.warning(
                "Instance flag for \"Render Setup Include Lights\" is set to "
                "{} and Settings flag is set to {}".format(
                    instance_lights_flag, settings_lights_flag
                )
            )

        # go through definitions and test if such node.attribute exists.
        # if so, compare its value from the one required.
        for data in cls.get_nodes(instance, renderer):
            for node in data["nodes"]:
                # Why is captured 'PublishValidationError'? How it can be
                #   raised by 'cmds.getAttr(...)'?
                try:
                    render_value = cmds.getAttr(
                        "{}.{}".format(node, data["attribute"])
                    )
                except PublishValidationError:
                    invalid = True
                    cls.log.error(
                        "Cannot get value of {}.{}".format(
                            node, data["attribute"]
                        )
                    )
                else:
                    if render_value not in data["values"]:
                        invalid = True
                        cls.log.error(
                            "Invalid value {} set on {}.{}. Expecting "
                            "{}".format(
                                render_value,
                                node,
                                data["attribute"],
                                data["values"]
                            )
                        )

        return invalid

    @classmethod
    def get_nodes(cls, instance, renderer):
        maya_settings = instance.context.data["project_settings"]["maya"]
        renderer_key = "{}_render_attributes".format(renderer)
        validation_settings = (
            maya_settings["publish"]["ValidateRenderSettings"].get(
                renderer_key
            )
        ) or []
        validation_settings = [
            (item["type"], item["value"])
            for item in validation_settings
        ]
        result = []
        for attr, values in OrderedDict(validation_settings).items():
            values = [convert_to_int_or_float(v) for v in values if v]

            # Validate the settings has values.
            if not values:
                cls.log.error(
                    "Settings for {} is missing values.".format(attr)
                )
                continue

            cls.log.debug("{}: {}".format(attr, values))
            if "." not in attr:
                cls.log.warning(
                    "Skipping invalid attribute defined in validation "
                    "settings: \"{}\"".format(attr)
                )
                continue

            node_type, attribute_name = attr.split(".", 1)

            # first get node of that type
            nodes = cmds.ls(type=node_type)

            if not nodes:
                cls.log.warning(
                    "No nodes of type \"{}\" found.".format(node_type)
                )
                continue

            result.append(
                {
                    "attribute": attribute_name,
                    "nodes": nodes,
                    "values": values
                }
            )

        return result

    @classmethod
    def repair(cls, instance):
        renderer = instance.data['renderer']
        layer_node = instance.data['setMembers']
        redshift_AOV_prefix = cls.redshift_AOV_prefix.replace(
            "{aov_separator}", instance.data.get("aovSeparator", "_")
        )
        default_prefix = cls.ImagePrefixTokens[renderer].replace(
            "{aov_separator}", instance.data.get("aovSeparator", "_")
        )

        for data in cls.get_nodes(instance, renderer):
            if not data["values"]:
                continue
            for node in data["nodes"]:
                lib.set_attribute(data["attribute"], data["values"][0], node)
        with lib.renderlayer(layer_node):

            # Repair animation must be enabled
            cmds.setAttr("defaultRenderGlobals.animation", True)

            # Repair prefix
            if renderer == "arnold":
                multipart = cmds.getAttr("defaultArnoldDriver.mergeAOVs")
                if multipart:
                    separator_variations = [
                        "_<RenderPass>",
                        "<RenderPass>_",
                        "<RenderPass>",
                    ]
                    for variant in separator_variations:
                        default_prefix = default_prefix.replace(variant, "")

            if renderer != "renderman":
                prefix_attr = RenderSettings.get_image_prefix_attr(renderer)
                fname_prefix = default_prefix
                cmds.setAttr(prefix_attr, fname_prefix, type="string")

                # Repair padding
                padding_attr = RenderSettings.get_padding_attr(renderer)
                cmds.setAttr(padding_attr, cls.DEFAULT_PADDING)
            else:
                # renderman handles stuff differently
                cmds.setAttr("rmanGlobals.imageFileFormat",
                             default_prefix,
                             type="string")
                cmds.setAttr("rmanGlobals.imageOutputDir",
                             cls.renderman_dir_prefix,
                             type="string")

            if renderer == "vray":
                vray_settings = cmds.ls(type="VRaySettingsNode")
                if not vray_settings:
                    node = cmds.createNode("VRaySettingsNode")
                else:
                    node = vray_settings[0]

                cmds.optionMenuGrp("vrayRenderElementSeparator",
                                   v=instance.data.get("aovSeparator", "_"))
                cmds.setAttr(
                    "{}.fileNameRenderElementSeparator".format(node),
                    instance.data.get("aovSeparator", "_"),
                    type="string"
                )

            if renderer == "redshift":
                # get redshift AOVs
                rs_aovs = cmds.ls(type="RedshiftAOV", referencedNodes=False)
                for aov in rs_aovs:
                    # fix AOV prefixes
                    cmds.setAttr(
                        "{}.filePrefix".format(aov),
                        redshift_AOV_prefix, type="string")
                    # fix AOV file format
                    default_ext = cmds.getAttr(
                        "redshiftOptions.imageFormat", asString=True)
                    cmds.setAttr(
                        "{}.fileFormat".format(aov), default_ext)

get_redshift_image_format_labels()

Return nice labels for Redshift image formats.

Source code in client/ayon_maya/plugins/publish/validate_rendersettings.py
34
35
36
37
def get_redshift_image_format_labels():
    """Return nice labels for Redshift image formats."""
    var = "$g_redshiftImageFormatLabels"
    return mel.eval("{0}={0}".format(var))