Skip to content

validate_shape_render_stats

ValidateShapeRenderStats

Bases: MayaInstancePlugin, OptionalPyblishPluginMixin

Ensure all render stats are set to the default values.

Source code in client/ayon_maya/plugins/publish/validate_shape_render_stats.py
12
13
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
class ValidateShapeRenderStats(plugin.MayaInstancePlugin,
                               OptionalPyblishPluginMixin):
    """Ensure all render stats are set to the default values."""

    order = ValidateMeshOrder
    families = ['model']
    label = 'Shape Default Render Stats'
    actions = [ayon_maya.api.action.SelectInvalidAction,
               RepairAction]

    defaults = {'castsShadows': 1,
                'receiveShadows': 1,
                'motionBlur': 1,
                'primaryVisibility': 1,
                'smoothShading': 1,
                'visibleInReflections': 1,
                'visibleInRefractions': 1,
                'doubleSided': 1,
                'opposite': 0}

    @classmethod
    def get_invalid(cls, instance):
        # It seems the "surfaceShape" and those derived from it have
        # `renderStat` attributes.
        shapes = cmds.ls(instance, long=True, type='surfaceShape')
        invalid = set()
        for shape in shapes:
            for attr, default_value in cls.defaults.items():
                if cmds.attributeQuery(attr, node=shape, exists=True):
                    value = cmds.getAttr('{}.{}'.format(shape, attr))
                    if value != default_value:
                        invalid.add(shape)

        return invalid

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

        defaults_str = "\n".join(
            "- {}: {}\n".format(key, value)
            for key, value in self.defaults.items()
        )
        description = (
            "## Shape Default Render Stats\n"
            "Shapes are detected with non-default render stats.\n\n"
            "To ensure a model's shapes behave like a shape would by default "
            "we require the render stats to have not been altered in "
            "the published models.\n\n"
            "### How to repair?\n"
            "You can reset the default values on the shapes by using the "
            "repair action."
        )

        raise PublishValidationError(
            "Shapes with non-default renderStats "
            "found: {0}".format(", ".join(sorted(invalid))),
            description=description,
            detail="The expected default values "
                   "are:\n\n{}".format(defaults_str)
        )

    @classmethod
    def repair(cls, instance):
        for shape in cls.get_invalid(instance):
            for attr, default_value in cls.defaults.items():
                if cmds.attributeQuery(attr, node=shape, exists=True):
                    plug = '{0}.{1}'.format(shape, attr)
                    value = cmds.getAttr(plug)
                    if value != default_value:
                        cmds.setAttr(plug, default_value)