Skip to content

validate_no_action

ValidateNoAction

Bases: BlenderInstancePlugin

Ensure that objects have action with animation data.

Source code in client/ayon_blender/plugins/publish/validate_no_action.py
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
class ValidateNoAction(plugin.BlenderInstancePlugin):
    """Ensure that objects have action with animation data."""

    order = ValidateContentsOrder
    hosts = ["blender"]
    families = ["action"]
    label = "No Action"
    actions = [SelectInvalidAction, RepairAction]

    @classmethod
    def get_invalid(cls, instance):
        invalid = []
        for data in instance:
            if not (
                isinstance(data, bpy.types.Object) and data.type in
                {'MESH', 'EMPTY', 'ARMATURE'}
            ):
                continue
            # just in case the instance node contains either Armature or top empty
            child = data.children[0] if data.children else data
            if child and child.type == 'ARMATURE':
                if not child.animation_data:
                    cls.log.error(f"No animation data: {child.name}")
                    invalid.append(child)
                else:
                    product_name = instance.data["productName"]
                    if not child.animation_data.action:
                        cls.log.error(f"No action data: {child.name}")
                        invalid.append(child)
                    elif child.animation_data.action.name != product_name:
                        cls.log.error(
                            f"Action name mismatch: {product_name} ({child.animation_data.action.name})"
                        )
                        invalid.append(child)
        return invalid

    def process(self, instance):
        invalid = self.get_invalid(instance)
        if invalid:
            names = ", ".join(obj.name for obj in invalid)
            raise PublishValidationError(
                "Objects found in instance which have"
                f" no action: {names}",
                title="No Action found on Objects",
                description=self.get_description()
            )

    def get_description(self):
        return inspect.cleandoc("""
            ### No Action found on Objects
            Objects must contain any action data.
            Please add the action to the objects before publishing.
        """)

    @classmethod
    def repair(cls, instance):
        product_name = instance.data["productName"]
        invalid_object = cls.get_invalid(instance)
        for obj in invalid_object:
            if not obj.animation_data:
                obj.animation_data_create()
            if not obj.animation_data.action:
                action = bpy.data.actions.new(name=product_name)
                obj.animation_data.action = action
            else:
                obj.animation_data.action.name = product_name
        cls.log.info("Created action data for objects in instance.")