Skip to content

validate_no_material

ValidateNoMaterial

Bases: BlenderInstancePlugin

Ensure that objects have material assigned.

Source code in client/ayon_blender/plugins/publish/validate_no_material.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
class ValidateNoMaterial(plugin.BlenderInstancePlugin):
    """Ensure that objects have material assigned."""

    order = ValidateContentsOrder
    hosts = ["blender"]
    families = ["look"]
    label = "No Material"
    actions = [SelectInvalidAction, RepairAction]

    @classmethod
    def get_invalid(cls, instance):
        invalid = []
        for obj in instance:
            if not (
                isinstance(obj, bpy.types.Object)
                and hasattr(obj.data, "materials")
            ):
                continue
            if not obj.active_material:
                cls.log.error(f"No active material: {obj.name}")
                invalid.append(obj)
        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 material: {names}",
                title="No Material Assigned on Objects",
                description=self.get_description()
            )

    def get_description(self):
        return inspect.cleandoc("""
            ### No Material Assigned to Objects
            Objects must have a material assigned.
            Please assign a material 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.active_material:
                empty_material = bpy.data.materials.new(name=product_name)
                empty_material.use_nodes = True
                obj.data.materials.append(empty_material)