Skip to content

collect_shapes

Collect layers for shape export.

CollectShapes

Bases: InstancePlugin

Collect trackpoint data.

Source code in client/ayon_mocha/plugins/publish/collect_shapes.py
 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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
class CollectShapes(pyblish.api.InstancePlugin):
    """Collect trackpoint data."""
    label = "Collect Shape Data"
    order = pyblish.api.CollectorOrder - 0.45
    hosts: ClassVar[list[str]] = ["mochapro"]
    families: ClassVar[list[str]] = ["matteshapes"]
    log: Logger

    @staticmethod
    def new_product_name(
        create_context: CreateContext,
        layer_name: str,
        product_type: str,
        variant: str) -> str:
        """Return the new product name."""
        sanitized_layer_name = layer_name.replace(" ", "_")
        variant = f"{sanitized_layer_name}{variant.capitalize()}"

        return get_product_name(
            project_name=create_context.project_name,
            task_name=create_context.get_current_task_name(),
            task_type=create_context.get_current_task_type(),
            host_name=create_context.host_name,
            product_type=product_type,
            variant=variant,
        )

    def process(self, instance: pyblish.api.Instance) -> None:
        """Process the instance.

        Raises:
            KnownPublishError: If layer mode is invalid.

        """
        # copy creator settings to the instance itself
        creator_attrs = instance.data["creator_attributes"]
        registered_exporters = get_shape_exporters()
        selected_exporters = [
            exporter
            for exporter in registered_exporters
            if exporter.id in creator_attrs["exporter"]
        ]

        instance.data["use_exporters"] = selected_exporters
        project: Project = instance.context.data["project"]
        layers: list[Layer] = []
        if creator_attrs["layer_mode"] == "selected":
            layers.extend(
                project.layers[selected_layer_idx]
                for selected_layer_idx in creator_attrs["layers"]
            )
        elif creator_attrs["layer_mode"] == "all":
            layers = project.layers
        else:
            msg = f"Invalid layer mode: {creator_attrs['layer_mode']}"
            raise KnownPublishError(msg)

        for layer in layers:
            new_instance = instance.context.create_instance(
                f"{instance.name} - {layer.name}"
            )
            for k, v in instance.data.items():
                # this is needed because the data is not always
                # "deepcopyable".
                try:
                    new_instance.data[k] = deepcopy(v)
                except TypeError:  # noqa: PERF203
                    new_instance.data[k] = v

            # new_instance.data = instance.data
            new_instance.data["label"] = f"{instance.name} ({layer.name})"
            new_instance.data["name"] = f"{instance.name}_{layer.name}"
            new_instance.data["productName"] = self.new_product_name(
                instance.context.data["create_context"],
                layer.name,
                instance.data["productType"],
                instance.data["variant"],
            )
            self.set_layer_data_on_instance(new_instance, layer)
        if layers:
            instance.context.remove(instance)

    @staticmethod
    def set_layer_data_on_instance(
            instance: pyblish.api.Instance, layer: Layer) -> None:
        """Set data on instance."""
        instance.data["layer"] = layer
        instance.data["frameStart"] = layer.in_point()
        instance.data["frameEnd"] = layer.out_point()

new_product_name(create_context, layer_name, product_type, variant) staticmethod

Return the new product name.

Source code in client/ayon_mocha/plugins/publish/collect_shapes.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@staticmethod
def new_product_name(
    create_context: CreateContext,
    layer_name: str,
    product_type: str,
    variant: str) -> str:
    """Return the new product name."""
    sanitized_layer_name = layer_name.replace(" ", "_")
    variant = f"{sanitized_layer_name}{variant.capitalize()}"

    return get_product_name(
        project_name=create_context.project_name,
        task_name=create_context.get_current_task_name(),
        task_type=create_context.get_current_task_type(),
        host_name=create_context.host_name,
        product_type=product_type,
        variant=variant,
    )

process(instance)

Process the instance.

Raises:

Type Description
KnownPublishError

If layer mode is invalid.

Source code in client/ayon_mocha/plugins/publish/collect_shapes.py
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
def process(self, instance: pyblish.api.Instance) -> None:
    """Process the instance.

    Raises:
        KnownPublishError: If layer mode is invalid.

    """
    # copy creator settings to the instance itself
    creator_attrs = instance.data["creator_attributes"]
    registered_exporters = get_shape_exporters()
    selected_exporters = [
        exporter
        for exporter in registered_exporters
        if exporter.id in creator_attrs["exporter"]
    ]

    instance.data["use_exporters"] = selected_exporters
    project: Project = instance.context.data["project"]
    layers: list[Layer] = []
    if creator_attrs["layer_mode"] == "selected":
        layers.extend(
            project.layers[selected_layer_idx]
            for selected_layer_idx in creator_attrs["layers"]
        )
    elif creator_attrs["layer_mode"] == "all":
        layers = project.layers
    else:
        msg = f"Invalid layer mode: {creator_attrs['layer_mode']}"
        raise KnownPublishError(msg)

    for layer in layers:
        new_instance = instance.context.create_instance(
            f"{instance.name} - {layer.name}"
        )
        for k, v in instance.data.items():
            # this is needed because the data is not always
            # "deepcopyable".
            try:
                new_instance.data[k] = deepcopy(v)
            except TypeError:  # noqa: PERF203
                new_instance.data[k] = v

        # new_instance.data = instance.data
        new_instance.data["label"] = f"{instance.name} ({layer.name})"
        new_instance.data["name"] = f"{instance.name}_{layer.name}"
        new_instance.data["productName"] = self.new_product_name(
            instance.context.data["create_context"],
            layer.name,
            instance.data["productType"],
            instance.data["variant"],
        )
        self.set_layer_data_on_instance(new_instance, layer)
    if layers:
        instance.context.remove(instance)

set_layer_data_on_instance(instance, layer) staticmethod

Set data on instance.

Source code in client/ayon_mocha/plugins/publish/collect_shapes.py
101
102
103
104
105
106
107
@staticmethod
def set_layer_data_on_instance(
        instance: pyblish.api.Instance, layer: Layer) -> None:
    """Set data on instance."""
    instance.data["layer"] = layer
    instance.data["frameStart"] = layer.in_point()
    instance.data["frameEnd"] = layer.out_point()