Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Blender: Workfile Loader #4234

Merged
merged 7 commits into from
Dec 16, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Objects imported are stored in collections for each scene
  • Loading branch information
simonebarbieri committed Dec 16, 2022
commit a0c60afb286bb615b67aa3e0fa156767f3c167e5
35 changes: 29 additions & 6 deletions openpype/hosts/blender/plugins/load/import_workfile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
from pathlib import Path

import bpy

from openpype.hosts.blender.api import plugin


def get_unique_number(asset, subset):
count = 1
name = f"{asset}_{count:0>2}_{subset}"
collection_names = [coll.name for coll in bpy.data.collections]
while name in collection_names:
count += 1
name = f"{asset}_{count:0>2}_{subset}"
return f"{count:0>2}"


class AppendBlendLoader(plugin.AssetLoader):
"""Append workfile in Blender (unmanaged)

Expand All @@ -28,6 +40,7 @@ def load(self, context, name=None, namespace=None, data=None):
# We do not containerize imported content, it remains unmanaged
return


class ImportBlendLoader(plugin.AssetLoader):
"""Import workfile in the current Blender scene (unmanaged)

Expand All @@ -46,16 +59,26 @@ class ImportBlendLoader(plugin.AssetLoader):
color = "#775555"

def load(self, context, name=None, namespace=None, data=None):
asset = context['asset']['name']
subset = context['subset']['name']

unique_number = get_unique_number(asset, subset)
group_name = plugin.asset_name(asset, subset, unique_number)

with bpy.data.libraries.load(self.fname) as (data_from, data_to):
for attr in dir(data_to):
if attr == "scenes":
continue
setattr(data_to, attr, getattr(data_from, attr))

# Add objects to current scene
scene = bpy.context.scene
for obj in data_to.objects:
scene.collection.objects.link(obj)
current_scene = bpy.context.scene

for scene in data_to.scenes:
# scene.name = group_name
collection = bpy.data.collections.new(name=group_name)
for obj in scene.objects:
collection.objects.link(obj)
current_scene.collection.children.link(collection)
for coll in scene.collection.children:
collection.children.link(coll)

# We do not containerize imported content, it remains unmanaged
return