Skip to content

validate_mesh_has_uv

ValidateMeshHasUvs

Bases: BlenderInstancePlugin, OptionalPyblishPluginMixin

Validate that the current mesh has UV's.

Source code in client/ayon_blender/plugins/publish/validate_mesh_has_uv.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
class ValidateMeshHasUvs(
    plugin.BlenderInstancePlugin,
    OptionalPyblishPluginMixin,
):
    """Validate that the current mesh has UV's."""

    order = ValidateContentsOrder
    hosts = ["blender"]
    families = ["model"]
    label = "Mesh Has UVs"
    actions = [ayon_blender.api.action.SelectInvalidAction]
    optional = True

    @staticmethod
    def has_uvs(obj: bpy.types.Object) -> bool:
        """Check if an object has uv's."""
        if not obj.data.uv_layers:
            return False
        for uv_layer in obj.data.uv_layers:
            for polygon in obj.data.polygons:
                for loop_index in polygon.loop_indices:
                    if (
                        loop_index >= len(uv_layer.data)
                        or not uv_layer.data[loop_index].uv
                    ):
                        return False

        return True

    @classmethod
    def get_invalid(cls, instance) -> List:
        invalid = []
        for obj in instance:
            if isinstance(obj, bpy.types.Object) and obj.type == 'MESH':
                if obj.mode != "OBJECT":
                    cls.log.warning(
                        f"Mesh object {obj.name} should be in 'OBJECT' mode"
                        " to be properly checked."
                    )
                if not cls.has_uvs(obj):
                    invalid.append(obj)
        return invalid

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

        invalid = self.get_invalid(instance)
        if invalid:
            raise PublishValidationError(
                f"Meshes found in instance without valid UV's: {invalid}"
            )

has_uvs(obj) staticmethod

Check if an object has uv's.

Source code in client/ayon_blender/plugins/publish/validate_mesh_has_uv.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@staticmethod
def has_uvs(obj: bpy.types.Object) -> bool:
    """Check if an object has uv's."""
    if not obj.data.uv_layers:
        return False
    for uv_layer in obj.data.uv_layers:
        for polygon in obj.data.polygons:
            for loop_index in polygon.loop_indices:
                if (
                    loop_index >= len(uv_layer.data)
                    or not uv_layer.data[loop_index].uv
                ):
                    return False

    return True