Skip to content

create_render

RenderlayerCreator

Bases: Cinema4DCreator

Creator which creates an instance per renderlayer in the workfile.

Create and manages render product instancess per Cinema4D Take in workfile. This generates a singleton node in the scene which, if it exists, tells the Creator to collect Cinema4D Takes as individual instances. As such, triggering create doesn't actually create the instance node per layer but only the node which tells the Creator it may now collect an instance per Take.

It collects Cinema4D Takes each turning into a render product instance.

Source code in client/ayon_cinema4d/plugins/create/create_render.py
 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
class RenderlayerCreator(plugin.Cinema4DCreator):
    """Creator which creates an instance per renderlayer in the workfile.

    Create and manages render product instancess per Cinema4D Take in workfile.
    This generates a singleton node in the scene which, if it exists, tells the
    Creator to collect Cinema4D Takes as individual instances.
    As such, triggering create doesn't actually create the instance node per
    layer but only the node which tells the Creator it may now collect
    an instance per Take.

    It collects Cinema4D Takes each turning into a render product instance.
    """
    settings_category = "cinema4d"

    identifier = "io.ayon.creators.cinema4d.render"
    label = "Render"
    description = "Create a render product per Cinema4D Take."
    detailed_description = inspect.cleandoc(__doc__)
    product_type = "render"
    product_base_type = "render"
    icon = "eye"

    _required_keys = ("creator_identifier", "productName")

    def _is_marked_workfile_as_render_enabled(self, take_data) -> bool:
        """Return whether the detecting of Takes as render instances is
        enabled in the current workfile.

        This will be true if at least one Take in the scene has render instance
        data imprinted onto it.
        """
        for take in lib.iter_objects(take_data.GetMainTake()):
            data = self._read_instance_node(take)
            if all(key in data for key in self._required_keys):
                return True
        return False

    def create(self, product_name, instance_data, pre_create_data):

        # On creating a first renderlayer instance, we tag the scene so that
        # from that moment onwards, we collect all takes as renderlayers.
        # So we put some data somewhere that says, "takes are now collected".
        # self._mark_workfile_as_render_enabled()

        doc: c4d.documents.BaseDocument = c4d.documents.GetActiveDocument()
        take_data = doc.GetTakeData()
        if take_data is None:
            return

        instance_node = None
        variant_name: str = instance_data.get("variant", "Main")
        print(variant_name)
        if not self._is_marked_workfile_as_render_enabled(take_data):
            # If there's already a take with the variant name, we skip creating
            # a new take but instead just mark the existing take
            for take in lib.iter_objects(take_data.GetMainTake()):
                if take.GetName() == variant_name:
                    # If there's already a take with the variant name,
                    # we do nothing
                    instance_node = take

        # Create a new take
        if instance_node is None:
            # Add a take so that at least something happens on Create for the
            # user
            root = take_data.GetMainTake()
            instance_node = take_data.AddTake(variant_name, root, None)
            c4d.EventAdd()

        # Enforce forward compatibility to avoid the instance to default
        # to the legacy `AVALON_INSTANCE_ID`
        instance_data["id"] = AYON_INSTANCE_ID
        # Use the uniqueness of the node in Cinema4D as the instance id
        instance_data["instance_id"] = str(hash(instance_node))
        instance = CreatedInstance(
            product_type=self.product_type,
            product_name=product_name,
            data=instance_data,
            transient_data={
                "instance_node": instance_node,
                "take": instance_node
            },
            creator=self,
        )

        # Store the instance data
        data = instance.data_to_store()
        self.imprint_instance_node(instance_node, data)

        self._add_instance_to_context(instance)

        # Then directly refresh with all existing entries
        self.collect_instances()

    def collect_instances(self):
        doc: c4d.documents.BaseDocument = c4d.documents.GetActiveDocument()
        take_data = doc.GetTakeData()
        if not self._is_marked_workfile_as_render_enabled(take_data):
            return

        # Each Cinema4D Take is considered a renderlayer
        for take in lib.iter_objects(take_data.GetMainTake()):

            data = self._read_instance_node(take)
            if all(key in data for key in self._required_keys):
                data = self.read_take_overrides(take, data)
                instance = CreatedInstance.from_existing(data, creator=self)
            else:
                take_name: str = take.GetName()
                variant = self._sanitize_take_variant_name(take_name)

                # No existing scene instance node for this layer. Note that
                # this instance will not have the `instance_node` data yet
                # until it's been saved/persisted at least once.
                folder_entity = self.create_context.get_current_folder_entity()
                task_entity = self.create_context.get_current_task_entity()
                instance_data = {
                    "folderPath": folder_entity["path"],
                    "task": task_entity["name"],
                    "variant": variant,
                }

                # Allow subclass to override data behavior
                instance_data = self.read_take_overrides(
                    take, instance_data
                )

                instance = CreatedInstance(
                    product_type=self.product_type,
                    # Defined in `read_take_overrides`
                    product_name=instance_data["productName"],
                    data=instance_data,
                    creator=self
                )

            instance.transient_data["instance_node"] = take
            instance.transient_data["take"] = take
            self._add_instance_to_context(instance)

    def read_take_overrides(
            self,
            take: c4d.modules.takesystem.BaseTake,
            instance_data: dict) -> dict:
        """Overridable read logic to read certain data from the take itself.

        Arguments:
            take (c4d.modules.takesystem.BaseTake): The render take.
            instance_data (dict): The instance's data dictionary.

        Returns:
            dict: The instance's data dictionary with overrides.

        """
        # Override some regular "read" logic like active state
        # retrieved from take active state
        instance_data["active"] = take.IsChecked()

        project_name = self.create_context.get_current_project_name()
        folder_entity = self.create_context.get_current_folder_entity()
        task_entity = self.create_context.get_current_task_entity()
        variant = self._sanitize_take_variant_name(take.GetName())

        host_name = self.create_context.host_name

        # Always keep product name in sync with the take name
        product_name = self.get_product_name(
            project_name,
            folder_entity,
            task_entity,
            variant,
            host_name,
        )
        instance_data["productName"] = product_name
        instance_data["variant"] = variant

        return instance_data

    def _sanitize_take_variant_name(self, variant: str) -> str:
        # Sanitize take variant name (e.g. remove spaces)
        # because variants and products names are not allowed to have
        # spaces in them.
        variant = variant.replace(" ", "_").replace("-", "_")
        return variant

    def imprint_instance_node_data_overrides(self,
                                             data: dict,
                                             instance):
        """Persist instance overrides in a custom way.

        Using this you can persist data to the scene that needs to be persisted
        in an alternate way than regular instance attributes, e.g. a native
        Cinema4D attribute or alike such as toggling the take active
        state.

        Make sure to `pop` the data you have already persisted if that data
        is also read from native Cinema4D node attribute in the scene in
        `read_instance_node_overrides`. This avoids it still getting written
        into the `UserData` of the instance node as well.

        Arguments:
            data (dict): The data available to be 'persisted'.
            instance (CreatedInstance): The instance operating on.

        Returns:
            dict: The instance's data that should be persisted into the scene
                in the regular manner.

        """
        take: c4d.modules.takesystem.BaseTake = instance.transient_data["take"]
        take.SetChecked(data.pop("active"))
        take.SetName(data.pop("variant"))
        return data

    def update_instances(self, update_list):
        # We only generate the persisting layer data into the scene once
        # we save with the UI on e.g. validate or publish
        for instance, _changes in update_list:
            instance_node = instance.transient_data["take"]

            data = instance.data_to_store()
            # Allow subclass to override imprinted data behavior
            # The returned data may be altered (e.g. some data popped) that
            # custom imprint logic stored elsewhere
            data = self.imprint_instance_node_data_overrides(data,
                                                             instance)
            self.imprint_instance_node(instance_node, data=data)
        c4d.EventAdd()

    def imprint_instance_node(self, node, data):
        self._imprint(node, data)

    def remove_instances(self, instances):
        # Disallow 'deleting the "Main" take because it can't be removed
        for instance in instances:
            take: c4d.modules.takesystem.BaseTake = (
                instance.transient_data.get("take")
            )
            if not take:
                continue

            if take.IsMain():
                # Remove any imprinted instance data, but avoid deleting it
                # because deleting the main take will crash Cinema4D
                existing_user_data = take.GetUserDataContainer()
                instance_data_keys = set(instance.data_to_store().keys())
                for description_id, base_container in existing_user_data:
                    key = base_container[c4d.DESC_NAME]
                    if key in instance_data_keys:
                        take.RemoveUserData(description_id)
            else:
                take.Remove()

            # Remove the collected CreatedInstance to remove from UI directly
            self._remove_instance_from_context(instance)
        c4d.EventAdd()

    def get_pre_create_attr_defs(self):
        return []

imprint_instance_node_data_overrides(data, instance)

Persist instance overrides in a custom way.

Using this you can persist data to the scene that needs to be persisted in an alternate way than regular instance attributes, e.g. a native Cinema4D attribute or alike such as toggling the take active state.

Make sure to pop the data you have already persisted if that data is also read from native Cinema4D node attribute in the scene in read_instance_node_overrides. This avoids it still getting written into the UserData of the instance node as well.

Parameters:

Name Type Description Default
data dict

The data available to be 'persisted'.

required
instance CreatedInstance

The instance operating on.

required

Returns:

Name Type Description
dict

The instance's data that should be persisted into the scene in the regular manner.

Source code in client/ayon_cinema4d/plugins/create/create_render.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
def imprint_instance_node_data_overrides(self,
                                         data: dict,
                                         instance):
    """Persist instance overrides in a custom way.

    Using this you can persist data to the scene that needs to be persisted
    in an alternate way than regular instance attributes, e.g. a native
    Cinema4D attribute or alike such as toggling the take active
    state.

    Make sure to `pop` the data you have already persisted if that data
    is also read from native Cinema4D node attribute in the scene in
    `read_instance_node_overrides`. This avoids it still getting written
    into the `UserData` of the instance node as well.

    Arguments:
        data (dict): The data available to be 'persisted'.
        instance (CreatedInstance): The instance operating on.

    Returns:
        dict: The instance's data that should be persisted into the scene
            in the regular manner.

    """
    take: c4d.modules.takesystem.BaseTake = instance.transient_data["take"]
    take.SetChecked(data.pop("active"))
    take.SetName(data.pop("variant"))
    return data

read_take_overrides(take, instance_data)

Overridable read logic to read certain data from the take itself.

Parameters:

Name Type Description Default
take BaseTake

The render take.

required
instance_data dict

The instance's data dictionary.

required

Returns:

Name Type Description
dict dict

The instance's data dictionary with overrides.

Source code in client/ayon_cinema4d/plugins/create/create_render.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def read_take_overrides(
        self,
        take: c4d.modules.takesystem.BaseTake,
        instance_data: dict) -> dict:
    """Overridable read logic to read certain data from the take itself.

    Arguments:
        take (c4d.modules.takesystem.BaseTake): The render take.
        instance_data (dict): The instance's data dictionary.

    Returns:
        dict: The instance's data dictionary with overrides.

    """
    # Override some regular "read" logic like active state
    # retrieved from take active state
    instance_data["active"] = take.IsChecked()

    project_name = self.create_context.get_current_project_name()
    folder_entity = self.create_context.get_current_folder_entity()
    task_entity = self.create_context.get_current_task_entity()
    variant = self._sanitize_take_variant_name(take.GetName())

    host_name = self.create_context.host_name

    # Always keep product name in sync with the take name
    product_name = self.get_product_name(
        project_name,
        folder_entity,
        task_entity,
        variant,
        host_name,
    )
    instance_data["productName"] = product_name
    instance_data["variant"] = variant

    return instance_data