Skip to content

load_action

Load an action in Blender.

BlendActionLoader

Bases: BlenderLoader

Load action from a .blend file.

Warning

Loading the same asset more then once is not properly supported at the moment.

Source code in client/ayon_blender/plugins/load/load_action.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
class BlendActionLoader(plugin.BlenderLoader):
    """Load action from a .blend file.

    Warning:
        Loading the same asset more then once is not properly supported at the
        moment.
    """

    product_types = {"action"}
    representations = {"blend"}

    label = "Link Action"
    icon = "code-fork"
    color = "orange"

    def process_asset(
        self, context: dict, name: str, namespace: Optional[str] = None,
        options: Optional[Dict] = None
    ) -> Optional[List]:
        """
        Arguments:
            name: Use pre-defined name
            namespace: Use pre-defined namespace
            context: Full parenthood of representation to load
            options: Additional settings dictionary
        """

        libpath = self.filepath_from_context(context)
        folder_name = context["folder"]["name"]
        product_name = context["product"]["name"]
        lib_container = plugin.prepare_scene_name(folder_name, product_name)
        container_name = plugin.prepare_scene_name(
            folder_name, product_name, namespace
        )

        container = bpy.data.collections.new(lib_container)
        container.name = container_name
        containerise_existing(
            container,
            name,
            namespace,
            context,
            self.__class__.__name__,
        )

        container_metadata = container.get(AVALON_PROPERTY)

        container_metadata["libpath"] = libpath
        container_metadata["lib_container"] = lib_container

        relative = bpy.context.preferences.filepaths.use_relative_paths
        with bpy.data.libraries.load(
            libpath, link=True, relative=relative
        ) as (_, data_to):
            data_to.collections = [lib_container]

        collection = bpy.context.scene.collection

        collection.children.link(bpy.data.collections[lib_container])

        animation_container = collection.children[lib_container].make_local()

        objects_list = []

        # Link meshes first, then armatures.
        # The armature is unparented for all the non-local meshes,
        # when it is made local.
        for obj in animation_container.objects:

            obj = obj.make_local()

            anim_data = obj.animation_data

            if anim_data is not None and anim_data.action is not None:

                anim_data.action.make_local()

            if not obj.get(AVALON_PROPERTY):

                obj[AVALON_PROPERTY] = dict()

            avalon_info = obj[AVALON_PROPERTY]
            avalon_info.update({"container_name": container_name})

            objects_list.append(obj)

        animation_container.pop(AVALON_PROPERTY)

        # Save the list of objects in the metadata container
        container_metadata["objects"] = objects_list

        bpy.ops.object.select_all(action='DESELECT')

        nodes = list(container.objects)
        nodes.append(container)
        self[:] = nodes
        return nodes

    def update(self, container: Dict, context: Dict):
        """Update the loaded asset.

        This will remove all objects of the current collection, load the new
        ones and add them to the collection.
        If the objects of the collection are used in another collection they
        will not be removed, only unlinked. Normally this should not be the
        case though.

        Warning:
            No nested collections are supported at the moment!
        """
        repre_entity = context["representation"]
        collection = bpy.data.collections.get(
            container["objectName"]
        )

        libpath = Path(get_representation_path(repre_entity))
        extension = libpath.suffix.lower()

        logger.info(
            "Container: %s\nRepresentation: %s",
            pformat(container, indent=2),
            pformat(repre_entity, indent=2),
        )

        assert collection, (
            f"The asset is not loaded: {container['objectName']}"
        )
        assert not (collection.children), (
            "Nested collections are not supported."
        )
        assert libpath, (
            "No existing library file found for {container['objectName']}"
        )
        assert libpath.is_file(), (
            f"The file doesn't exist: {libpath}"
        )
        assert extension in plugin.VALID_EXTENSIONS, (
            f"Unsupported file: {libpath}"
        )

        collection_metadata = collection.get(AVALON_PROPERTY)

        collection_libpath = collection_metadata["libpath"]
        normalized_collection_libpath = (
            str(Path(bpy.path.abspath(collection_libpath)).resolve())
        )
        normalized_libpath = (
            str(Path(bpy.path.abspath(str(libpath))).resolve())
        )
        logger.debug(
            "normalized_collection_libpath:\n  %s\nnormalized_libpath:\n  %s",
            normalized_collection_libpath,
            normalized_libpath,
        )
        if normalized_collection_libpath == normalized_libpath:
            logger.info("Library already loaded, not updating...")
            return

        strips = []

        for obj in list(collection_metadata["objects"]):
            # Get all the strips that use the action
            arm_objs = [
                arm for arm in bpy.data.objects if arm.type == 'ARMATURE']

            for armature_obj in arm_objs:
                if armature_obj.animation_data is not None:
                    for track in armature_obj.animation_data.nla_tracks:
                        for strip in track.strips:
                            if strip.action == obj.animation_data.action:
                                strips.append(strip)

            bpy.data.actions.remove(obj.animation_data.action)
            bpy.data.objects.remove(obj)

        lib_container = collection_metadata["lib_container"]

        bpy.data.collections.remove(bpy.data.collections[lib_container])

        relative = bpy.context.preferences.filepaths.use_relative_paths
        with bpy.data.libraries.load(
            str(libpath), link=True, relative=relative
        ) as (_, data_to):
            data_to.collections = [lib_container]

        scene = bpy.context.scene

        scene.collection.children.link(bpy.data.collections[lib_container])

        anim_container = scene.collection.children[lib_container].make_local()

        objects_list = []

        # Link meshes first, then armatures.
        # The armature is unparented for all the non-local meshes,
        # when it is made local.
        for obj in anim_container.objects:

            obj = obj.make_local()

            anim_data = obj.animation_data

            if anim_data is not None and anim_data.action is not None:

                anim_data.action.make_local()

                for strip in strips:

                    strip.action = anim_data.action
                    strip.action_frame_end = anim_data.action.frame_range[1]

            if not obj.get(AVALON_PROPERTY):

                obj[AVALON_PROPERTY] = dict()

            avalon_info = obj[AVALON_PROPERTY]
            avalon_info.update({"container_name": collection.name})

            objects_list.append(obj)

        anim_container.pop(AVALON_PROPERTY)

        # Save the list of objects in the metadata container
        collection_metadata["objects"] = objects_list
        collection_metadata["libpath"] = str(libpath)
        collection_metadata["representation"] = repre_entity["id"]
        collection_metadata["project_name"] = context["project"]["name"]

        bpy.ops.object.select_all(action='DESELECT')

    def remove(self, container: Dict) -> bool:
        """Remove an existing container from a Blender scene.

        Arguments:
            container (openpype:container-1.0): Container to remove,
                from `host.ls()`.

        Returns:
            bool: Whether the container was deleted.

        Warning:
            No nested collections are supported at the moment!
        """

        collection = bpy.data.collections.get(
            container["objectName"]
        )
        if not collection:
            return False
        assert not (collection.children), (
            "Nested collections are not supported."
        )

        collection_metadata = collection.get(AVALON_PROPERTY)
        objects = collection_metadata["objects"]
        lib_container = collection_metadata["lib_container"]

        for obj in list(objects):
            # Get all the strips that use the action
            arm_objs = [
                arm for arm in bpy.data.objects if arm.type == 'ARMATURE']

            for armature_obj in arm_objs:
                if armature_obj.animation_data is not None:
                    for track in armature_obj.animation_data.nla_tracks:
                        for strip in track.strips:
                            if strip.action == obj.animation_data.action:
                                track.strips.remove(strip)

            bpy.data.actions.remove(obj.animation_data.action)
            bpy.data.objects.remove(obj)

        bpy.data.collections.remove(bpy.data.collections[lib_container])
        bpy.data.collections.remove(collection)

        return True

process_asset(context, name, namespace=None, options=None)

Parameters:

Name Type Description Default
name str

Use pre-defined name

required
namespace Optional[str]

Use pre-defined namespace

None
context dict

Full parenthood of representation to load

required
options Optional[Dict]

Additional settings dictionary

None
Source code in client/ayon_blender/plugins/load/load_action.py
 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
def process_asset(
    self, context: dict, name: str, namespace: Optional[str] = None,
    options: Optional[Dict] = None
) -> Optional[List]:
    """
    Arguments:
        name: Use pre-defined name
        namespace: Use pre-defined namespace
        context: Full parenthood of representation to load
        options: Additional settings dictionary
    """

    libpath = self.filepath_from_context(context)
    folder_name = context["folder"]["name"]
    product_name = context["product"]["name"]
    lib_container = plugin.prepare_scene_name(folder_name, product_name)
    container_name = plugin.prepare_scene_name(
        folder_name, product_name, namespace
    )

    container = bpy.data.collections.new(lib_container)
    container.name = container_name
    containerise_existing(
        container,
        name,
        namespace,
        context,
        self.__class__.__name__,
    )

    container_metadata = container.get(AVALON_PROPERTY)

    container_metadata["libpath"] = libpath
    container_metadata["lib_container"] = lib_container

    relative = bpy.context.preferences.filepaths.use_relative_paths
    with bpy.data.libraries.load(
        libpath, link=True, relative=relative
    ) as (_, data_to):
        data_to.collections = [lib_container]

    collection = bpy.context.scene.collection

    collection.children.link(bpy.data.collections[lib_container])

    animation_container = collection.children[lib_container].make_local()

    objects_list = []

    # Link meshes first, then armatures.
    # The armature is unparented for all the non-local meshes,
    # when it is made local.
    for obj in animation_container.objects:

        obj = obj.make_local()

        anim_data = obj.animation_data

        if anim_data is not None and anim_data.action is not None:

            anim_data.action.make_local()

        if not obj.get(AVALON_PROPERTY):

            obj[AVALON_PROPERTY] = dict()

        avalon_info = obj[AVALON_PROPERTY]
        avalon_info.update({"container_name": container_name})

        objects_list.append(obj)

    animation_container.pop(AVALON_PROPERTY)

    # Save the list of objects in the metadata container
    container_metadata["objects"] = objects_list

    bpy.ops.object.select_all(action='DESELECT')

    nodes = list(container.objects)
    nodes.append(container)
    self[:] = nodes
    return nodes

remove(container)

Remove an existing container from a Blender scene.

Parameters:

Name Type Description Default
container openpype

container-1.0): Container to remove, from host.ls().

required

Returns:

Name Type Description
bool bool

Whether the container was deleted.

Warning

No nested collections are supported at the moment!

Source code in client/ayon_blender/plugins/load/load_action.py
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
def remove(self, container: Dict) -> bool:
    """Remove an existing container from a Blender scene.

    Arguments:
        container (openpype:container-1.0): Container to remove,
            from `host.ls()`.

    Returns:
        bool: Whether the container was deleted.

    Warning:
        No nested collections are supported at the moment!
    """

    collection = bpy.data.collections.get(
        container["objectName"]
    )
    if not collection:
        return False
    assert not (collection.children), (
        "Nested collections are not supported."
    )

    collection_metadata = collection.get(AVALON_PROPERTY)
    objects = collection_metadata["objects"]
    lib_container = collection_metadata["lib_container"]

    for obj in list(objects):
        # Get all the strips that use the action
        arm_objs = [
            arm for arm in bpy.data.objects if arm.type == 'ARMATURE']

        for armature_obj in arm_objs:
            if armature_obj.animation_data is not None:
                for track in armature_obj.animation_data.nla_tracks:
                    for strip in track.strips:
                        if strip.action == obj.animation_data.action:
                            track.strips.remove(strip)

        bpy.data.actions.remove(obj.animation_data.action)
        bpy.data.objects.remove(obj)

    bpy.data.collections.remove(bpy.data.collections[lib_container])
    bpy.data.collections.remove(collection)

    return True

update(container, context)

Update the loaded asset.

This will remove all objects of the current collection, load the new ones and add them to the collection. If the objects of the collection are used in another collection they will not be removed, only unlinked. Normally this should not be the case though.

Warning

No nested collections are supported at the moment!

Source code in client/ayon_blender/plugins/load/load_action.py
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
def update(self, container: Dict, context: Dict):
    """Update the loaded asset.

    This will remove all objects of the current collection, load the new
    ones and add them to the collection.
    If the objects of the collection are used in another collection they
    will not be removed, only unlinked. Normally this should not be the
    case though.

    Warning:
        No nested collections are supported at the moment!
    """
    repre_entity = context["representation"]
    collection = bpy.data.collections.get(
        container["objectName"]
    )

    libpath = Path(get_representation_path(repre_entity))
    extension = libpath.suffix.lower()

    logger.info(
        "Container: %s\nRepresentation: %s",
        pformat(container, indent=2),
        pformat(repre_entity, indent=2),
    )

    assert collection, (
        f"The asset is not loaded: {container['objectName']}"
    )
    assert not (collection.children), (
        "Nested collections are not supported."
    )
    assert libpath, (
        "No existing library file found for {container['objectName']}"
    )
    assert libpath.is_file(), (
        f"The file doesn't exist: {libpath}"
    )
    assert extension in plugin.VALID_EXTENSIONS, (
        f"Unsupported file: {libpath}"
    )

    collection_metadata = collection.get(AVALON_PROPERTY)

    collection_libpath = collection_metadata["libpath"]
    normalized_collection_libpath = (
        str(Path(bpy.path.abspath(collection_libpath)).resolve())
    )
    normalized_libpath = (
        str(Path(bpy.path.abspath(str(libpath))).resolve())
    )
    logger.debug(
        "normalized_collection_libpath:\n  %s\nnormalized_libpath:\n  %s",
        normalized_collection_libpath,
        normalized_libpath,
    )
    if normalized_collection_libpath == normalized_libpath:
        logger.info("Library already loaded, not updating...")
        return

    strips = []

    for obj in list(collection_metadata["objects"]):
        # Get all the strips that use the action
        arm_objs = [
            arm for arm in bpy.data.objects if arm.type == 'ARMATURE']

        for armature_obj in arm_objs:
            if armature_obj.animation_data is not None:
                for track in armature_obj.animation_data.nla_tracks:
                    for strip in track.strips:
                        if strip.action == obj.animation_data.action:
                            strips.append(strip)

        bpy.data.actions.remove(obj.animation_data.action)
        bpy.data.objects.remove(obj)

    lib_container = collection_metadata["lib_container"]

    bpy.data.collections.remove(bpy.data.collections[lib_container])

    relative = bpy.context.preferences.filepaths.use_relative_paths
    with bpy.data.libraries.load(
        str(libpath), link=True, relative=relative
    ) as (_, data_to):
        data_to.collections = [lib_container]

    scene = bpy.context.scene

    scene.collection.children.link(bpy.data.collections[lib_container])

    anim_container = scene.collection.children[lib_container].make_local()

    objects_list = []

    # Link meshes first, then armatures.
    # The armature is unparented for all the non-local meshes,
    # when it is made local.
    for obj in anim_container.objects:

        obj = obj.make_local()

        anim_data = obj.animation_data

        if anim_data is not None and anim_data.action is not None:

            anim_data.action.make_local()

            for strip in strips:

                strip.action = anim_data.action
                strip.action_frame_end = anim_data.action.frame_range[1]

        if not obj.get(AVALON_PROPERTY):

            obj[AVALON_PROPERTY] = dict()

        avalon_info = obj[AVALON_PROPERTY]
        avalon_info.update({"container_name": collection.name})

        objects_list.append(obj)

    anim_container.pop(AVALON_PROPERTY)

    # Save the list of objects in the metadata container
    collection_metadata["objects"] = objects_list
    collection_metadata["libpath"] = str(libpath)
    collection_metadata["representation"] = repre_entity["id"]
    collection_metadata["project_name"] = context["project"]["name"]

    bpy.ops.object.select_all(action='DESELECT')