Skip to content

load_blend

BlendLoader

Bases: BlenderLoader

Load assets from a .blend file.

Source code in client/ayon_blender/plugins/load/load_blend.py
 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
class BlendLoader(plugin.BlenderLoader):
    """Load assets from a .blend file."""

    product_types = {"model", "rig", "layout", "camera"}
    representations = {"blend"}

    label = "Append Blend"
    icon = "code-fork"
    color = "orange"

    @staticmethod
    def _get_asset_container(objects):
        empties = [obj for obj in objects if obj.type == 'EMPTY']

        for empty in empties:
            if empty.get(AVALON_PROPERTY) and empty.parent is None:
                return empty

        return None

    @staticmethod
    def get_all_container_parents(asset_group):
        parent_containers = []
        parent = asset_group.parent
        while parent:
            if parent.get(AVALON_PROPERTY):
                parent_containers.append(parent)
            parent = parent.parent

        return parent_containers

    def _post_process_layout(self, container, asset, representation):
        rigs = [
            obj for obj in container.children_recursive
            if (
                obj.type == 'EMPTY' and
                obj.get(AVALON_PROPERTY) and
                obj.get(AVALON_PROPERTY).get('family') == 'rig'
            )
        ]
        if not rigs:
            return

        # Create animation instances for each rig
        creator_identifier = "io.openpype.creators.blender.animation"
        host = registered_host()
        create_context = CreateContext(host)

        for rig in rigs:
            create_context.create(
                creator_identifier=creator_identifier,
                variant=rig.name.split(':')[-1],
                pre_create_data={
                    "use_selection": False,
                    "asset_group": rig
                }
            )

    def _process_data(self, libpath, group_name):
        # Append all the data from the .blend file
        with bpy.data.libraries.load(
            libpath, link=False, relative=False
        ) as (data_from, data_to):
            for attr in dir(data_to):
                setattr(data_to, attr, getattr(data_from, attr))

        members = []

        # Rename the object to add the asset name
        for attr in dir(data_to):
            for data in getattr(data_to, attr):
                data.name = f"{group_name}:{data.name}"
                members.append(data)

        container = self._get_asset_container(data_to.objects)
        assert container, "No asset group found"

        container.name = group_name
        container.empty_display_type = 'SINGLE_ARROW'

        # Link the collection to the scene
        bpy.context.scene.collection.objects.link(container)

        # Link all the container children to the collection
        for obj in container.children_recursive:
            bpy.context.scene.collection.objects.link(obj)

        # Remove the library from the blend file
        filepath = bpy.path.basename(libpath)
        # Blender has a limit of 63 characters for any data name.
        # If the filepath is longer, it will be truncated.
        if len(filepath) > 63:
            filepath = filepath[:63]
        library = bpy.data.libraries.get(filepath)
        bpy.data.libraries.remove(library)

        return container, members

    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"]

        try:
            product_type = context["product"]["productType"]
        except ValueError:
            product_type = "model"

        representation = context["representation"]["id"]

        asset_name = plugin.prepare_scene_name(folder_name, product_name)
        unique_number = plugin.get_unique_number(folder_name, product_name)
        group_name = plugin.prepare_scene_name(
            folder_name, product_name, unique_number
        )
        namespace = namespace or f"{folder_name}_{unique_number}"

        avalon_container = bpy.data.collections.get(AVALON_CONTAINERS)
        if not avalon_container:
            avalon_container = bpy.data.collections.new(name=AVALON_CONTAINERS)
            bpy.context.scene.collection.children.link(avalon_container)

        container, members = self._process_data(libpath, group_name)

        if product_type == "layout":
            self._post_process_layout(container, folder_name, representation)

        avalon_container.objects.link(container)

        data = {
            "schema": "openpype:container-2.0",
            "id": AVALON_CONTAINER_ID,
            "name": name,
            "namespace": namespace or '',
            "loader": str(self.__class__.__name__),
            "representation": context["representation"]["id"],
            "libpath": libpath,
            "asset_name": asset_name,
            "parent": context["representation"]["versionId"],
            "productType": context["product"]["productType"],
            "objectName": group_name,
            "members": members,
            "project_name": context["project"]["name"],
        }

        container[AVALON_PROPERTY] = data

        objects = [
            obj for obj in bpy.data.objects
            if obj.name.startswith(f"{group_name}:")
        ]

        self[:] = objects
        return objects

    def exec_update(self, container: Dict, context: Dict):
        """
        Update the loaded asset.
        """
        repre_entity = context["representation"]
        group_name = container["objectName"]
        asset_group = bpy.data.objects.get(group_name)
        libpath = Path(get_representation_path(repre_entity)).as_posix()

        assert asset_group, (
            f"The asset is not loaded: {container['objectName']}"
        )

        transform = asset_group.matrix_basis.copy()
        old_data = dict(asset_group.get(AVALON_PROPERTY))
        old_members = old_data.get("members", [])
        parent = asset_group.parent

        actions = {}
        objects_with_anim = [
            obj for obj in asset_group.children_recursive
            if obj.animation_data]
        for obj in objects_with_anim:
            # Check if the object has an action and, if so, add it to a dict
            # so we can restore it later. Save and restore the action only
            # if it wasn't originally loaded from the current asset.
            if obj.animation_data.action not in old_members:
                actions[obj.name] = obj.animation_data.action

        self.exec_remove(container)

        asset_group, members = self._process_data(libpath, group_name)

        avalon_container = bpy.data.collections.get(AVALON_CONTAINERS)
        avalon_container.objects.link(asset_group)

        asset_group.matrix_basis = transform
        asset_group.parent = parent

        # Restore the actions
        for obj in asset_group.children_recursive:
            if obj.name in actions:
                if not obj.animation_data:
                    obj.animation_data_create()
                obj.animation_data.action = actions[obj.name]

        # Restore the old data, but reset members, as they don't exist anymore
        # This avoids a crash, because the memory addresses of those members
        # are not valid anymore
        old_data["members"] = []
        asset_group[AVALON_PROPERTY] = old_data

        new_data = {
            "libpath": libpath,
            "representation": repre_entity["id"],
            "parent": repre_entity["versionId"],
            "members": members,
            "project_name": context["project"]["name"],
        }

        imprint(asset_group, new_data)

        # We need to update all the parent container members
        parent_containers = self.get_all_container_parents(asset_group)

        for parent_container in parent_containers:
            parent_members = parent_container[AVALON_PROPERTY]["members"]
            parent_container[AVALON_PROPERTY]["members"] = (
                parent_members + members)

    def exec_remove(self, container: Dict) -> bool:
        """
        Remove an existing container from a Blender scene.
        """
        group_name = container["objectName"]
        asset_group = bpy.data.objects.get(group_name)

        attrs = [
            attr for attr in dir(bpy.data)
            if isinstance(
                getattr(bpy.data, attr),
                bpy.types.bpy_prop_collection
            )
        ]

        members = asset_group.get(AVALON_PROPERTY).get("members", [])

        # We need to update all the parent container members
        parent_containers = self.get_all_container_parents(asset_group)

        for parent in parent_containers:
            parent.get(AVALON_PROPERTY)["members"] = list(filter(
                lambda i: i not in members,
                parent.get(AVALON_PROPERTY).get("members", [])))

        for attr in attrs:
            for data in getattr(bpy.data, attr):
                if data in members:
                    # Skip the asset group
                    if data == asset_group:
                        continue
                    getattr(bpy.data, attr).remove(data)

        bpy.data.objects.remove(asset_group)

exec_remove(container)

Remove an existing container from a Blender scene.

Source code in client/ayon_blender/plugins/load/load_blend.py
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
def exec_remove(self, container: Dict) -> bool:
    """
    Remove an existing container from a Blender scene.
    """
    group_name = container["objectName"]
    asset_group = bpy.data.objects.get(group_name)

    attrs = [
        attr for attr in dir(bpy.data)
        if isinstance(
            getattr(bpy.data, attr),
            bpy.types.bpy_prop_collection
        )
    ]

    members = asset_group.get(AVALON_PROPERTY).get("members", [])

    # We need to update all the parent container members
    parent_containers = self.get_all_container_parents(asset_group)

    for parent in parent_containers:
        parent.get(AVALON_PROPERTY)["members"] = list(filter(
            lambda i: i not in members,
            parent.get(AVALON_PROPERTY).get("members", [])))

    for attr in attrs:
        for data in getattr(bpy.data, attr):
            if data in members:
                # Skip the asset group
                if data == asset_group:
                    continue
                getattr(bpy.data, attr).remove(data)

    bpy.data.objects.remove(asset_group)

exec_update(container, context)

Update the loaded asset.

Source code in client/ayon_blender/plugins/load/load_blend.py
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
def exec_update(self, container: Dict, context: Dict):
    """
    Update the loaded asset.
    """
    repre_entity = context["representation"]
    group_name = container["objectName"]
    asset_group = bpy.data.objects.get(group_name)
    libpath = Path(get_representation_path(repre_entity)).as_posix()

    assert asset_group, (
        f"The asset is not loaded: {container['objectName']}"
    )

    transform = asset_group.matrix_basis.copy()
    old_data = dict(asset_group.get(AVALON_PROPERTY))
    old_members = old_data.get("members", [])
    parent = asset_group.parent

    actions = {}
    objects_with_anim = [
        obj for obj in asset_group.children_recursive
        if obj.animation_data]
    for obj in objects_with_anim:
        # Check if the object has an action and, if so, add it to a dict
        # so we can restore it later. Save and restore the action only
        # if it wasn't originally loaded from the current asset.
        if obj.animation_data.action not in old_members:
            actions[obj.name] = obj.animation_data.action

    self.exec_remove(container)

    asset_group, members = self._process_data(libpath, group_name)

    avalon_container = bpy.data.collections.get(AVALON_CONTAINERS)
    avalon_container.objects.link(asset_group)

    asset_group.matrix_basis = transform
    asset_group.parent = parent

    # Restore the actions
    for obj in asset_group.children_recursive:
        if obj.name in actions:
            if not obj.animation_data:
                obj.animation_data_create()
            obj.animation_data.action = actions[obj.name]

    # Restore the old data, but reset members, as they don't exist anymore
    # This avoids a crash, because the memory addresses of those members
    # are not valid anymore
    old_data["members"] = []
    asset_group[AVALON_PROPERTY] = old_data

    new_data = {
        "libpath": libpath,
        "representation": repre_entity["id"],
        "parent": repre_entity["versionId"],
        "members": members,
        "project_name": context["project"]["name"],
    }

    imprint(asset_group, new_data)

    # We need to update all the parent container members
    parent_containers = self.get_all_container_parents(asset_group)

    for parent_container in parent_containers:
        parent_members = parent_container[AVALON_PROPERTY]["members"]
        parent_container[AVALON_PROPERTY]["members"] = (
            parent_members + members)

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_blend.py
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
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"]

    try:
        product_type = context["product"]["productType"]
    except ValueError:
        product_type = "model"

    representation = context["representation"]["id"]

    asset_name = plugin.prepare_scene_name(folder_name, product_name)
    unique_number = plugin.get_unique_number(folder_name, product_name)
    group_name = plugin.prepare_scene_name(
        folder_name, product_name, unique_number
    )
    namespace = namespace or f"{folder_name}_{unique_number}"

    avalon_container = bpy.data.collections.get(AVALON_CONTAINERS)
    if not avalon_container:
        avalon_container = bpy.data.collections.new(name=AVALON_CONTAINERS)
        bpy.context.scene.collection.children.link(avalon_container)

    container, members = self._process_data(libpath, group_name)

    if product_type == "layout":
        self._post_process_layout(container, folder_name, representation)

    avalon_container.objects.link(container)

    data = {
        "schema": "openpype:container-2.0",
        "id": AVALON_CONTAINER_ID,
        "name": name,
        "namespace": namespace or '',
        "loader": str(self.__class__.__name__),
        "representation": context["representation"]["id"],
        "libpath": libpath,
        "asset_name": asset_name,
        "parent": context["representation"]["versionId"],
        "productType": context["product"]["productType"],
        "objectName": group_name,
        "members": members,
        "project_name": context["project"]["name"],
    }

    container[AVALON_PROPERTY] = data

    objects = [
        obj for obj in bpy.data.objects
        if obj.name.startswith(f"{group_name}:")
    ]

    self[:] = objects
    return objects