Skip to content

create_workfile

CreateWorkfile

Bases: AutoCreator

Workfile auto-creator.

The workfile instance stores its data on the AYON_CONTAINERS collection as custom attributes, because unlike other instances it doesn't have an instance node of its own.

Source code in client/ayon_cinema4d/plugins/create/create_workfile.py
  7
  8
  9
 10
 11
 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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class CreateWorkfile(AutoCreator):
    """Workfile auto-creator.

    The workfile instance stores its data on the `AYON_CONTAINERS` collection
    as custom attributes, because unlike other instances it doesn't have an
    instance node of its own.

    """
    identifier = "io.ayon.creators.cinema4d.workfile"
    label = "Workfile"
    product_type = "workfile"
    icon = "fa5.file"

    node_name = "AYON_workfile"

    def create(self):
        """Create workfile instances."""
        workfile_instance = next(
            (
                instance for instance in self.create_context.instances
                if instance.creator_identifier == self.identifier
            ),
            None,
        )

        project_name = self.project_name
        folder_path = self.create_context.get_current_folder_path()
        task_name = self.create_context.get_current_task_name()
        host_name = self.create_context.host_name

        existing_folder_path = None
        if workfile_instance is not None:
            existing_folder_path = workfile_instance.get("folderPath")

        if not workfile_instance:
            folder_entity = ayon_api.get_folder_by_path(
                project_name, folder_path
            )
            task_entity = ayon_api.get_task_by_name(
                project_name, folder_entity["id"], task_name
            )
            product_name = self.get_product_name(
                project_name,
                folder_entity,
                task_entity,
                task_name,
                host_name,
            )
            data = {
                "folderPath": folder_path,
                "task": task_name,
                "variant": task_name,
            }

            # Enforce forward compatibility to avoid the instance to default
            # to the legacy `AVALON_INSTANCE_ID`
            data["id"] = AYON_INSTANCE_ID

            data.update(
                self.get_dynamic_data(
                    project_name,
                    folder_entity,
                    task_entity,
                    task_name,
                    host_name,
                    workfile_instance,
                )
            )
            self.log.info("Auto-creating workfile instance...")
            workfile_instance = CreatedInstance(
                self.product_type, product_name, data, self
            )
            self._add_instance_to_context(workfile_instance)

        elif (
            existing_folder_path != folder_path
            or workfile_instance["task"] != task_name
        ):
            # Update instance context if it's different
            folder_entity = ayon_api.get_folder_by_path(
                project_name, folder_path
            )
            task_entity = ayon_api.get_task_by_name(
                project_name, folder_entity["id"], task_name
            )
            product_name = self.get_product_name(
                project_name,
                folder_entity,
                task_entity,
                self.default_variant,
                host_name,
            )

            workfile_instance["folderPath"] = folder_path
            workfile_instance["task"] = task_name
            workfile_instance["productName"] = product_name

    def collect_instances(self):

        shared_data = cache_instance_data(self.collection_shared_data)
        for obj in shared_data["cinema4d_cached_instances"].get(
                self.identifier, []):

            data = lib.read(obj)
            data["instance_id"] = str(hash(obj))

            # Add instance
            created_instance = CreatedInstance.from_existing(data, self)

            # Collect transient data
            created_instance.transient_data["instance_node"] = obj

            # Add instance to create context
            self._add_instance_to_context(created_instance)

            # Collect only one
            break

    def update_instances(self, update_list):
        for created_inst, _changes in update_list:

            # If it has no node yet, then it's a new workfile instance
            node = created_inst.transient_data.get("instance_node")
            if not node:
                node = plugin.create_selection([], name=self.node_name)
                plugin.parent_to_ayon_null(node)
                created_inst.transient_data["instance_node"] = node

            new_data = created_inst.data_to_store()

            # Do not store instance id since it's the node hash
            new_data.pop("instance_id", None)

            lib.imprint(node, new_data, group="AYON")

    def remove_instances(self, instances):
        for instance in instances:
            node = instance.transient_data["instance_node"]
            node.Remove()

            self._remove_instance_from_context(instance)

create()

Create workfile instances.

Source code in client/ayon_cinema4d/plugins/create/create_workfile.py
 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
def create(self):
    """Create workfile instances."""
    workfile_instance = next(
        (
            instance for instance in self.create_context.instances
            if instance.creator_identifier == self.identifier
        ),
        None,
    )

    project_name = self.project_name
    folder_path = self.create_context.get_current_folder_path()
    task_name = self.create_context.get_current_task_name()
    host_name = self.create_context.host_name

    existing_folder_path = None
    if workfile_instance is not None:
        existing_folder_path = workfile_instance.get("folderPath")

    if not workfile_instance:
        folder_entity = ayon_api.get_folder_by_path(
            project_name, folder_path
        )
        task_entity = ayon_api.get_task_by_name(
            project_name, folder_entity["id"], task_name
        )
        product_name = self.get_product_name(
            project_name,
            folder_entity,
            task_entity,
            task_name,
            host_name,
        )
        data = {
            "folderPath": folder_path,
            "task": task_name,
            "variant": task_name,
        }

        # Enforce forward compatibility to avoid the instance to default
        # to the legacy `AVALON_INSTANCE_ID`
        data["id"] = AYON_INSTANCE_ID

        data.update(
            self.get_dynamic_data(
                project_name,
                folder_entity,
                task_entity,
                task_name,
                host_name,
                workfile_instance,
            )
        )
        self.log.info("Auto-creating workfile instance...")
        workfile_instance = CreatedInstance(
            self.product_type, product_name, data, self
        )
        self._add_instance_to_context(workfile_instance)

    elif (
        existing_folder_path != folder_path
        or workfile_instance["task"] != task_name
    ):
        # Update instance context if it's different
        folder_entity = ayon_api.get_folder_by_path(
            project_name, folder_path
        )
        task_entity = ayon_api.get_task_by_name(
            project_name, folder_entity["id"], task_name
        )
        product_name = self.get_product_name(
            project_name,
            folder_entity,
            task_entity,
            self.default_variant,
            host_name,
        )

        workfile_instance["folderPath"] = folder_path
        workfile_instance["task"] = task_name
        workfile_instance["productName"] = product_name