소스 검색

add basic loading of files function

Sergiu 10 달 전
부모
커밋
2457cb6eec

+ 148 - 4
03_blender/sd_blender/__init__.py

@@ -41,22 +41,137 @@ bl_info = {
 def load_scene():
     print("Loading Scene")
     # load scene data
+    scene_data = load_scene_data()
     # create parent collections
     create_parent_collections("01_Products")
     create_parent_collections("02_Elements")
     create_parent_collections("03_Shapes")
     # append products
-
+    products_data = load_objects_data(scene_data, "product")
+    for index, product in enumerate(products_data):
+        append_objects(product, index)
     # append elements
     # append shapes
     # set lighting
     # set camera
 
 
-def append_objects():
+def load_scene_data():
+    print("Loading Scene Data")
+    # load scene data
+
+    # to be replaced with actual data
+    # open scene_info.json
+    script_path = Path(__file__).resolve()
+    scene_data_path = script_path.parent / "sample_scene" / "scene_info.json"
+    with scene_data_path.open() as file:
+        scene_data = json.load(file)
+        print(scene_data)
+    return scene_data
+
+
+def load_objects_data(scene_data, object_type: str):
+    print("Loading Assets Data")
+    # load assets data
+    objects_data = []
+    for object in scene_data["scene"]["objects"]:
+        if object["group_type"] == object_type:
+            # get additional object data by id and combine with object data
+            object_data = get_object_data_by_id(object["id"])
+            object.update(object_data)
+            objects_data.append(object)
+    return objects_data
+
+    # to be replaced with actual data
+
+
+def get_object_data_by_id(object_id: str):
+    print("Getting Object Data")
+    # open assets_database.json
+    script_path = Path(__file__).resolve()
+    assets_data_path = script_path.parent / "sample_scene" / "assets_database.json"
+
+    with assets_data_path.open() as file:
+        assets_data = json.load(file)
+    # get object data by id
+    for object in assets_data:
+        if object["id"] == object_id:
+            return object
+
+
+def append_objects(productInfo, index):
     print("Appending Objects")
-    # append objects
-    # set transforms
+
+    blendFileName = productInfo["name"]
+
+    # visibleLayersJSONName = productInfo["Version"]
+
+    # activeSwitchMaterials = json.dumps(productInfo["ActiveMaterials"])
+
+    collectionName = blendFileName + "_" + str(index)
+
+    append_active_layers(collectionName, productInfo)
+
+    # replace_switch_materials(shotInfo, productInfo["ActiveMaterials"])
+
+
+def append_active_layers(newCollectionName, product_info):
+    utility_collections = ["MaterialLibrary", "Animation_Controller", "Animation_Rig"]
+
+    # visible_layers = utility_collections + product_info["VisibleLayers"]
+    visible_layers = utility_collections
+
+    script_path = Path(__file__).resolve().parent
+    filePath = str(
+        script_path / product_info["path"] / (product_info["name"] + ".blend")
+    )
+
+    print(filePath)
+
+    # delete all objects and collections from product collection
+
+    create_parent_collections(newCollectionName)
+
+    # append active collections
+    with bpy.data.libraries.load(filePath) as (data_from, data_to):
+        data_to.collections = []
+        for name in data_from.collections:
+
+            if name in visible_layers:
+                data_to.collections.append(name)
+            if "NonConfigurable" in name:
+                data_to.collections.append(name)
+
+    # link appended colections to newCollection
+    for collection in data_to.collections:
+        try:
+            bpy.context.scene.collection.children.link(collection)
+        except:
+            print(collection)
+        link_collection_to_collection(newCollectionName, collection)
+
+        # hide utility collections
+        for utilityCollectionName in utility_collections:
+            if utilityCollectionName in collection.name:
+                # rename utility collections
+                collection.name = newCollectionName + "_" + utilityCollectionName
+                hide_collection(collection)
+
+    #
+    # make all objects in collection local
+    for obj in bpy.data.collections[newCollectionName].all_objects:
+        if obj.type == "MESH":
+            obj.make_local()
+            obj.data.make_local()
+
+    # remove duplicated material slots
+    mats = bpy.data.materials
+
+    for obj in bpy.data.collections[newCollectionName].all_objects:
+        for slot in obj.material_slots:
+            part = slot.name.rpartition(".")
+            if part[2].isnumeric() and part[0] in mats:
+                slot.material = mats.get(part[0])
 
 
 def create_parent_collections(group_name: str):
@@ -68,6 +183,11 @@ def create_parent_collections(group_name: str):
         create_collection(group_name)
 
 
+# -------------------------------------------------------------------
+# Utilities
+# -------------------------------------------------------------------
+
+
 def remove_collection_and_objects(collection_name):
     oldObjects = list(bpy.data.collections[collection_name].all_objects)
     for obj in oldObjects:
@@ -103,6 +223,25 @@ def get_subcollection_names(collection):
     return subcollection_names
 
 
+# link collection to collection
+def link_collection_to_collection(parentCollectionName, childCollection):
+
+    if bpy.context.scene.collection.children:
+        parentCollection = bpy.context.scene.collection.children.get(
+            parentCollectionName
+        )
+        # Add it to the main collection
+
+        try:
+            childCollection = bpy.data.collections[childCollection]
+        except:
+            print("Collection not found.")
+            return
+
+        parentCollection.children.link(childCollection)
+        bpy.context.scene.collection.children.unlink(childCollection)
+
+
 # function that checks if a collection exists
 def collection_exists(collection_name):
     return collection_name in bpy.data.collections
@@ -114,6 +253,11 @@ def create_collection(collection_name):
     bpy.context.scene.collection.children.link(new_collection)
 
 
+def hide_collection(collection):
+    collection.hide_render = True
+    collection.hide_viewport = True
+
+
 # -------------------------------------------------------------------
 # Operators
 # -------------------------------------------------------------------

BIN
03_blender/sd_blender/__pycache__/__init__.cpython-310.pyc


+ 12 - 0
03_blender/sd_blender/sample_scene/assets_database.json

@@ -0,0 +1,12 @@
+[
+    {
+        "id": "f8761a95-c28a-47ec-9254-e76cc3b693c7",
+        "name": "LNG_AntiAging_SleepingMask_MY2023",
+        "path": "sample_scene//Products//LNG_AntiAging_SleepingMask_MY2023"
+    },
+    {
+        "id": "15a314a1-8ba1-4e0e-ad0c-f605b06f89f8",
+        "name": "LNG_AntiAging_Serum_MY2023",
+        "path": "sample_scene//Products//LNG_AntiAging_Serum_MY2023"
+    }
+]

+ 3 - 3
03_blender/sd_blender/sample_scene/scene_info.json

@@ -5,7 +5,7 @@
                 "name": "LNG SleepingMask",
                 "type": "group",
                 "group_type": "product",
-                "id": "",
+                "id": "f8761a95-c28a-47ec-9254-e76cc3b693c7",
                 "properties": {
                     "transform": {
                         "position": [
@@ -31,7 +31,7 @@
                 "name": "LNG SleepingMask 2",
                 "type": "group",
                 "group_type": "product",
-                "id": "",
+                "id": "f8761a95-c28a-47ec-9254-e76cc3b693c7",
                 "properties": {
                     "transform": {
                         "position": [
@@ -57,7 +57,7 @@
                 "name": "LNG Serum",
                 "type": "group",
                 "group_type": "product",
-                "id": "",
+                "id": "15a314a1-8ba1-4e0e-ad0c-f605b06f89f8",
                 "properties": {
                     "transform": {
                         "position": [