Skip to content

plugin

LayoutLoader

Bases: Loader

Load Layout from a JSON file

Source code in client/ayon_unreal/api/plugin.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
class LayoutLoader(Loader):
    """Load Layout from a JSON file"""

    product_types = {"layout"}
    representations = {"json"}

    label = "Load Layout"
    icon = "code-fork"
    color = "orange"
    loaded_layout_dir = "{folder[path]}/{product[name]}"
    remove_loaded_assets = False

    @staticmethod
    def _get_fbx_loader(loaders, family):
        name = ""
        if family in ['rig', 'skeletalMesh']:
            name = "SkeletalMeshFBXLoader"
        elif family in ['model', 'staticMesh']:
            name = "StaticMeshFBXLoader"
        elif family == 'camera':
            name = "CameraLoader"

        if name == "":

            return None

        for loader in loaders:
            if loader.__name__ == name:
                return loader

        return None

    @staticmethod
    def _get_abc_loader(loaders, family):
        name = ""
        if family in ['rig', 'skeletalMesh']:
            name = "SkeletalMeshAlembicLoader"
        elif family in ['model', 'staticMesh']:
            name = "StaticMeshAlembicLoader"

        if name == "":
            return None

        for loader in loaders:
            if loader.__name__ == name:
                return loader

        return None

    def _transform_from_basis(self, transform, basis, unreal_import=False):
        """Transform a transform from a basis to a new basis."""
        # Get the basis matrix
        basis_matrix = unreal.Matrix(
            basis[0],
            basis[1],
            basis[2],
            basis[3]
        )
        transform_matrix = unreal.Matrix(
            transform[0],
            transform[1],
            transform[2],
            transform[3]
        )

        new_transform = None
        if unreal_import:
            new_transform = transform_matrix * basis_matrix
        else:
            new_transform = (
                basis_matrix.get_inverse() * transform_matrix * basis_matrix)

        return new_transform.transform()

    def _get_repre_entities_by_version_id(self, project_name, data, repre_extension, force_loaded=False):
        version_ids = {
            element.get("version")
            for element in data
            if element.get("representation")
        }
        version_ids.discard(None)
        output = collections.defaultdict(list)
        if not version_ids:
            return output
        # Extract extensions from data with backward compatibility for "ma"
        extensions = {
            element.get("extension", "ma")
            for element in data
            if element.get("representation")
        }

        # Update extensions based on the force_loaded flag
        updated_extensions = set()

        for ext in extensions:
            if not force_loaded or repre_extension == "json":
                if ext == "ma":
                    updated_extensions.update({"fbx", "abc"})
                else:
                    updated_extensions.add(ext)
            else:
                updated_extensions.update({repre_extension})

        repre_entities = ayon_api.get_representations(
            project_name,
            representation_names=updated_extensions,
            version_ids=version_ids,
            fields={"id", "versionId", "name"}
        )
        for repre_entity in repre_entities:
            version_id = repre_entity["versionId"]
            output[version_id].append(repre_entity)
        return output

    def imprint(
        self,
        context,
        folder_path,
        folder_name,
        loaded_assets,
        asset_dir,
        asset_name,
        container_name,
        project_name,
        hierarchy_dir=None
    ):
        data = {
            "schema": "ayon:container-2.0",
            "id": AYON_CONTAINER_ID,
            "asset": folder_name,
            "folder_path": folder_path,
            "namespace": asset_dir,
            "container_name": container_name,
            "asset_name": asset_name,
            "loader": str(self.__class__.__name__),
            "representation": context["representation"]["id"],
            "parent": context["representation"]["versionId"],
            "family": context["product"]["productType"],
            "loaded_assets": loaded_assets,
            "project_name": project_name
        }
        if hierarchy_dir is not None:
            data["master_directory"] = hierarchy_dir
        imprint(
            "{}/{}".format(asset_dir, container_name), data)

    def _load_assets(self, instance_name, repre_id, product_type, repr_format):
        all_loaders = discover_loader_plugins()
        loaders = loaders_from_representation(
            all_loaders, repre_id)

        loader = None

        if repr_format == 'fbx':
            loader = self._get_fbx_loader(loaders, product_type)
        elif repr_format == 'abc':
            loader = self._get_abc_loader(loaders, product_type)

        if not loader:
            if repr_format == "ma":
                msg = (
                    f"No valid {product_type} loader found for {repre_id} ({repr_format}), "
                    f"consider using {product_type} loader (fbx/abc) instead."
                )
                self.log.warning(msg)
            else:
                self.log.error(
                    f"No valid loader found for {repre_id} "
                    f"({repr_format}) "
                    f"{product_type}")
            return

        options = {
            # "asset_dir": asset_dir
        }

        assets = load_container(
            loader,
            repre_id,
            namespace=instance_name,
            options=options
        )
        return assets

    def _remove_Loaded_asset(self, container):
        """
        Delete the layout. First, check if the assets loaded with the layout
        are used by other layouts. If not, delete the assets.
        """
        if self.remove_loaded_assets:
            remove_asset_confirmation_dialog = unreal.EditorDialog.show_message(
                "The removal of the loaded assets",
                "The layout will be removed. Do you want to delete all associated assets as well?",
                unreal.AppMsgType.YES_NO)
            if (remove_asset_confirmation_dialog == unreal.AppReturnType.YES):
                remove_loaded_asset(container)

Loader

Bases: LoaderPlugin, ABC

This serves as skeleton for future Ayon specific functionality

Source code in client/ayon_unreal/api/plugin.py
272
273
274
class Loader(LoaderPlugin, ABC):
    """This serves as skeleton for future Ayon specific functionality"""
    pass

UnrealActorCreator

Bases: UnrealBaseCreator

Base class for Unreal creator plugins based on actors.

Source code in client/ayon_unreal/api/plugin.py
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
269
class UnrealActorCreator(UnrealBaseCreator):
    """Base class for Unreal creator plugins based on actors."""

    def create(self, product_name, instance_data, pre_create_data):
        """Create instance of the asset.

        Args:
            product_name (str): Name of the product.
            instance_data (dict): Data for the instance.
            pre_create_data (dict): Data for the instance.

        Returns:
            CreatedInstance: Created instance.
        """
        try:
            if UNREAL_VERSION.major == 5:
                world = unreal.UnrealEditorSubsystem().get_editor_world()
            else:
                world = unreal.EditorLevelLibrary.get_editor_world()

            # Check if the level is saved
            if world.get_path_name().startswith("/Temp/"):
                raise CreatorError(
                    "Level must be saved before creating instances.")

            # Check if instance data has members, filled by the plugin.
            # If not, use selection.
            if not instance_data.get("members"):
                actor_subsystem = unreal.EditorActorSubsystem()
                sel_actors = actor_subsystem.get_selected_level_actors()
                selection = [a.get_path_name() for a in sel_actors]

                instance_data["members"] = selection
            instance_data["level"] = world.get_path_name()

            super(UnrealActorCreator, self).create(
                product_name,
                instance_data,
                pre_create_data)

        except Exception as exc:
            raise CreatorError(f"Creator error: {exc}") from exc

    def get_pre_create_attr_defs(self):
        return [
            UILabelDef("Select actors to create instance from them."),
        ]

create(product_name, instance_data, pre_create_data)

Create instance of the asset.

Parameters:

Name Type Description Default
product_name str

Name of the product.

required
instance_data dict

Data for the instance.

required
pre_create_data dict

Data for the instance.

required

Returns:

Name Type Description
CreatedInstance

Created instance.

Source code in client/ayon_unreal/api/plugin.py
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
def create(self, product_name, instance_data, pre_create_data):
    """Create instance of the asset.

    Args:
        product_name (str): Name of the product.
        instance_data (dict): Data for the instance.
        pre_create_data (dict): Data for the instance.

    Returns:
        CreatedInstance: Created instance.
    """
    try:
        if UNREAL_VERSION.major == 5:
            world = unreal.UnrealEditorSubsystem().get_editor_world()
        else:
            world = unreal.EditorLevelLibrary.get_editor_world()

        # Check if the level is saved
        if world.get_path_name().startswith("/Temp/"):
            raise CreatorError(
                "Level must be saved before creating instances.")

        # Check if instance data has members, filled by the plugin.
        # If not, use selection.
        if not instance_data.get("members"):
            actor_subsystem = unreal.EditorActorSubsystem()
            sel_actors = actor_subsystem.get_selected_level_actors()
            selection = [a.get_path_name() for a in sel_actors]

            instance_data["members"] = selection
        instance_data["level"] = world.get_path_name()

        super(UnrealActorCreator, self).create(
            product_name,
            instance_data,
            pre_create_data)

    except Exception as exc:
        raise CreatorError(f"Creator error: {exc}") from exc

UnrealAssetCreator

Bases: UnrealBaseCreator

Base class for Unreal creator plugins based on assets.

Source code in client/ayon_unreal/api/plugin.py
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
class UnrealAssetCreator(UnrealBaseCreator):
    """Base class for Unreal creator plugins based on assets."""

    def create(self, product_name, instance_data, pre_create_data):
        """Create instance of the asset.

        Args:
            product_name (str): Name of the product.
            instance_data (dict): Data for the instance.
            pre_create_data (dict): Data for the instance.

        Returns:
            CreatedInstance: Created instance.
        """
        try:
            # Check if instance data has members, filled by the plugin.
            # If not, use selection.
            if not pre_create_data.get("members"):
                pre_create_data["members"] = []

                if pre_create_data.get("use_selection"):
                    utilib = unreal.EditorUtilityLibrary
                    sel_objects = utilib.get_selected_assets()
                    pre_create_data["members"] = [
                        a.get_path_name() for a in sel_objects]

            super(UnrealAssetCreator, self).create(
                product_name,
                instance_data,
                pre_create_data)

        except Exception as exc:
            raise CreatorError(f"Creator error: {exc}") from exc

    def get_pre_create_attr_defs(self):
        return [
            BoolDef("use_selection", label="Use selection", default=True)
        ]

create(product_name, instance_data, pre_create_data)

Create instance of the asset.

Parameters:

Name Type Description Default
product_name str

Name of the product.

required
instance_data dict

Data for the instance.

required
pre_create_data dict

Data for the instance.

required

Returns:

Name Type Description
CreatedInstance

Created instance.

Source code in client/ayon_unreal/api/plugin.py
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
def create(self, product_name, instance_data, pre_create_data):
    """Create instance of the asset.

    Args:
        product_name (str): Name of the product.
        instance_data (dict): Data for the instance.
        pre_create_data (dict): Data for the instance.

    Returns:
        CreatedInstance: Created instance.
    """
    try:
        # Check if instance data has members, filled by the plugin.
        # If not, use selection.
        if not pre_create_data.get("members"):
            pre_create_data["members"] = []

            if pre_create_data.get("use_selection"):
                utilib = unreal.EditorUtilityLibrary
                sel_objects = utilib.get_selected_assets()
                pre_create_data["members"] = [
                    a.get_path_name() for a in sel_objects]

        super(UnrealAssetCreator, self).create(
            product_name,
            instance_data,
            pre_create_data)

    except Exception as exc:
        raise CreatorError(f"Creator error: {exc}") from exc

UnrealBaseAutoCreator

Bases: AutoCreator, UnrealCreateLogic

Base class for Unreal auto creator plugins.

Source code in client/ayon_unreal/api/plugin.py
154
155
156
157
158
159
160
161
162
163
164
class UnrealBaseAutoCreator(AutoCreator, UnrealCreateLogic):
    """Base class for Unreal auto creator plugins."""

    def collect_instances(self):
        return self._default_collect_instances()

    def update_instances(self, update_list):
        return self._default_update_instances(update_list)

    def remove_instances(self, instances):
        return self._default_remove_instances(instances)

UnrealBaseCreator

Bases: UnrealCreateLogic, Creator

Base class for Unreal creator plugins.

Source code in client/ayon_unreal/api/plugin.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
class UnrealBaseCreator(UnrealCreateLogic, Creator):
    """Base class for Unreal creator plugins."""

    def create(self, subset_name, instance_data, pre_create_data):
        self.create_unreal(subset_name, instance_data, pre_create_data)

    def collect_instances(self):
        return self._default_collect_instances()

    def update_instances(self, update_list):
        return self._default_update_instances(update_list)

    def remove_instances(self, instances):
        return self._default_remove_instances(instances)

UnrealCreateLogic

Universal class for logic that Unreal creators could inherit from.

Source code in client/ayon_unreal/api/plugin.py
 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
class UnrealCreateLogic():
    """Universal class for logic that Unreal creators could inherit from."""
    root = "/Game/Ayon/AyonPublishInstances"
    suffix = "_INS"


    @staticmethod
    def get_cached_instances(shared_data):
        """Cache instances for Creators to shared data.

        Create `unreal_cached_subsets` key when needed in shared data and
        fill it with all collected instances from the scene under its
        respective creator identifiers.

        If legacy instances are detected in the scene, create
        `unreal_cached_legacy_subsets` there and fill it with
        all legacy subsets under product_type as a key.

        Args:
            Dict[str, Any]: Shared data.

        Return:
            Dict[str, Any]: Shared data dictionary.

        """
        if shared_data.get("unreal_cached_subsets") is None:
            unreal_cached_subsets = collections.defaultdict(list)
            unreal_cached_legacy_subsets = collections.defaultdict(list)
            for instance in ls_inst():
                creator_id = instance.get("creator_identifier")
                if creator_id:
                    unreal_cached_subsets[creator_id].append(instance)
                else:
                    product_type = instance.get("product_type")
                    unreal_cached_legacy_subsets[product_type].append(instance)

            shared_data["unreal_cached_subsets"] = unreal_cached_subsets
            shared_data["unreal_cached_legacy_subsets"] = (
                unreal_cached_legacy_subsets
            )
        return shared_data

    def _default_collect_instances(self):
        # cache instances if missing
        self.get_cached_instances(self.collection_shared_data)
        for instance in self.collection_shared_data[
                "unreal_cached_subsets"].get(self.identifier, []):
            # Unreal saves metadata as string, so we need to convert it back
            instance['creator_attributes'] = ast.literal_eval(
                instance.get('creator_attributes', '{}'))
            instance['publish_attributes'] = ast.literal_eval(
                instance.get('publish_attributes', '{}'))
            instance['members'] = ast.literal_eval(
                instance.get('members', '[]'))
            instance['families'] = ast.literal_eval(
                instance.get('families', '[]'))
            instance['active'] = ast.literal_eval(
                instance.get('active', ''))
            created_instance = CreatedInstance.from_existing(instance, self)
            self._add_instance_to_context(created_instance)

    def _default_update_instances(self, update_list):
        for created_inst, changes in update_list:
            instance_node = created_inst.get("instance_path", "")

            if not instance_node:
                unreal.log_warning(
                    f"Instance node not found for {created_inst}")
                continue

            new_values = {
                key: changes[key].new_value
                for key in changes.changed_keys
            }
            imprint(
                instance_node,
                new_values
            )

    def _default_remove_instances(self, instances):
        for instance in instances:
            instance_node = instance.data.get("instance_path", "")
            if instance_node:
                unreal.EditorAssetLibrary.delete_asset(instance_node)

            self._remove_instance_from_context(instance)


    def create_unreal(self, product_name, instance_data, pre_create_data):
        try:
            instance_name = f"{product_name}{self.suffix}"
            pub_instance = create_publish_instance(instance_name, self.root)

            instance_data["product_name"] = product_name
            instance_data["instance_path"] = f"{self.root}/{instance_name}"

            instance = CreatedInstance(
                self.product_type,
                product_name,
                instance_data,
                self)
            self._add_instance_to_context(instance)

            pub_instance.set_editor_property('add_external_assets', True)
            assets = pub_instance.get_editor_property('asset_data_external')

            ar = unreal.AssetRegistryHelpers.get_asset_registry()

            for member in pre_create_data.get("members", []):
                obj = ar.get_asset_by_object_path(member).get_asset()
                assets.add(obj)

            imprint(f"{self.root}/{instance_name}",
                    instance.data_to_store())

            return instance

        except Exception as exc:
            raise CreatorError(f"Creator error: {exc}") from exc

get_cached_instances(shared_data) staticmethod

Cache instances for Creators to shared data.

Create unreal_cached_subsets key when needed in shared data and fill it with all collected instances from the scene under its respective creator identifiers.

If legacy instances are detected in the scene, create unreal_cached_legacy_subsets there and fill it with all legacy subsets under product_type as a key.

Parameters:

Name Type Description Default
Dict[str, Any]

Shared data.

required
Return

Dict[str, Any]: Shared data dictionary.

Source code in client/ayon_unreal/api/plugin.py
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
@staticmethod
def get_cached_instances(shared_data):
    """Cache instances for Creators to shared data.

    Create `unreal_cached_subsets` key when needed in shared data and
    fill it with all collected instances from the scene under its
    respective creator identifiers.

    If legacy instances are detected in the scene, create
    `unreal_cached_legacy_subsets` there and fill it with
    all legacy subsets under product_type as a key.

    Args:
        Dict[str, Any]: Shared data.

    Return:
        Dict[str, Any]: Shared data dictionary.

    """
    if shared_data.get("unreal_cached_subsets") is None:
        unreal_cached_subsets = collections.defaultdict(list)
        unreal_cached_legacy_subsets = collections.defaultdict(list)
        for instance in ls_inst():
            creator_id = instance.get("creator_identifier")
            if creator_id:
                unreal_cached_subsets[creator_id].append(instance)
            else:
                product_type = instance.get("product_type")
                unreal_cached_legacy_subsets[product_type].append(instance)

        shared_data["unreal_cached_subsets"] = unreal_cached_subsets
        shared_data["unreal_cached_legacy_subsets"] = (
            unreal_cached_legacy_subsets
        )
    return shared_data