Browse Source

update export glb function and add upload assets function

Sergiu 8 months ago
parent
commit
deb8b6d9ff

+ 28 - 0
01_backend/database/assets.json

@@ -0,0 +1,28 @@
+[
+    {
+        "id": 20011,
+        "name": "AP_DRLC_2024_0001",
+        "type": 0,
+        "brandId": 2001,
+        "categoryId": 0,
+        "en_name": "Dual Repair Lift Cream",
+        "cn_name": "",
+        "thumb": "https://zixelise.sgp1.cdn.digitaloceanspaces.com/canvas-creation-module/2001/Products/AP_DRLC_2024_0001/PNG/AP_DRLC_2024_ALL_DT_IMG_0001.png",
+        "canvasGlbUrl": "https://zixelise.sgp1.cdn.digitaloceanspaces.com/canvas-creation-module/2001/Products/AP_DRLC_2024_0001/WEB/AP_DRLC_2024_ALL_DT_MAT_0001.glb",
+        "isFavorite": 1,
+        "folderUrl": "https://zixelise.sgp1.cdn.digitaloceanspaces.com/canvas-creation-module/2001/Products/AP_DRLC_2024_0001"
+    },
+    {
+        "id": 20012,
+        "name": "AP_LRDSPE_2024_0001",
+        "type": 0,
+        "brandId": 2001,
+        "categoryId": 0,
+        "en_name": "Lift and Renew Double Shot Program",
+        "cn_name": "",
+        "thumb": "https://zixelise.sgp1.cdn.digitaloceanspaces.com/canvas-creation-module/2001/Products/AP_LRDSPE_2024_0001/PNG/AP_LRDSPE_2024_ALL_DT_IMG_0001.png",
+        "canvasGlbUrl": "https://zixelise.sgp1.cdn.digitaloceanspaces.com/canvas-creation-module/2001/Products/AP_LRDSPE_2024_0001/WEB/AP_LRDSPE_2024_ALL_DT_MAT_0001.glb",
+        "isFavorite": 1,
+        "folderUrl": "https://zixelise.sgp1.cdn.digitaloceanspaces.com/canvas-creation-module/2001/Products/AP_LRDSPE_2024_0001"
+    }
+]

+ 37 - 0
01_backend/database/upload_assets.py

@@ -0,0 +1,37 @@
+import requests
+import json
+
+
+api_url = "https://canvas-api-test.anvil.app/_/api"
+
+
+def upload_assets(json_file_path, api_url):
+    # Open and read the JSON file
+    with open(json_file_path, "r") as file:
+        assets_data = json.load(file)
+
+    payload = {"assets": assets_data}
+    print(payload)
+
+    # Make the POST request to the API endpoint
+    url = f"{api_url}/management/uploadAssets"
+    response = requests.post(url, json=payload)
+
+    # Check the response
+    if response.status_code == 200:
+        print("Assets uploaded successfully")
+    else:
+        print(f"Failed to upload assets: {response.status_code}")
+        print(response.json())
+
+
+if __name__ == "__main__":
+    # Path to the JSON file containing the assets
+    json_file_path = "D:/Git/ap-canvas-creation-module/01_backend/database/assets.json"
+
+    # Application ID and sign (replace with actual values)
+    app_id = "your_app_id"
+    sign = "your_sign"
+
+    # Call the upload function
+    upload_assets(json_file_path, api_url)

+ 36 - 4
03_blender/prepare_folder_for export.py

@@ -3,8 +3,15 @@ import shutil
 
 
 def copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id):
-    # Copy the source folder to the target folder
+    # Construct the path for the copied folder
     copied_folder_path = os.path.join(target_folder, new_folder_name)
+
+    # Check if the target folder already exists
+    if os.path.exists(copied_folder_path):
+        print(f"Folder '{copied_folder_path}' already exists. Skipping.")
+        return
+
+    # Copy the source folder to the target folder
     shutil.copytree(source_folder, copied_folder_path)
 
     # Extract components from the new folder name
@@ -41,8 +48,33 @@ def copy_and_rename_folder(source_folder, target_folder, new_folder_name, conten
 source_folder = (
     r"Z:/01_Production_AP/01_Library/03_Elements/_Brand_AssetName_Year_AssetNumber"
 )
-target_folder = r"Z:/01_Production_AP/01_Library/03_Elements/AP/Products"
-new_folder_name = "AP_LRDSPE_2024_0001"
+target_folder = r"Z:/01_Production_AP/01_Library/03_Elements/IFR/Products"
+# new_folder_name = "AP_LRDSPE_2024_0001"
 content_id = "ALL_DT_MAT"
 
-copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id)
+folder_name_list = [
+    "IFR_BJL_2023_ALL_DT_MAT_0001",
+    "IFR_BJT_2023_ALL_DT_MAT_0001",
+    "IFR_BTPC_2023_ALL_DT_MAT_0001",
+    "IFR_BTE_2024_ALL_DT_MAT_0001",
+    "IFR_BTPE_2023_ALL_DT_MAT_0001",
+    "IFR_BTEC_2023_ALL_DT_MAT_0001",
+    "IFR_BTT_2024_ALL_DT_MAT_0001",
+    "IFR_CPAC_2024_ALL_DT_MAT_0001",
+    "IFR_CPAX_2023_ALL_DT_MAT_0002",
+    "IFR_CPE_2024_ALL_DT_MAT_0001",
+    "IFR_CPT_2024_ALL_DT_MAT_0001",
+    "IFR_GTAF_2023_ALL_DT_MAT_0001",
+    "IFR_GTC_2023_ALL_DT_MAT_0001",
+    "IFR_GTL_2023_ALL_DT_MAT_0001",
+    "IFR_GTHS_2023_ALL_DT_MAT_0001",
+    "IFR_GTS_2023_ALL_DT_MAT_0001",
+    "IFR_NSMP_2023_ALL_DT_MAT_0001",
+]
+
+# loop through list, and remove _ALL_DT_MAT from the names
+for folder_name in folder_name_list:
+    new_folder_name = folder_name.replace("_ALL_DT_MAT", "")
+    copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id)
+
+# copy_and_rename_folder(source_folder, target_folder, new_folder_name, content_id)

+ 60 - 48
03_blender/sd_blender/__init__.py

@@ -580,6 +580,14 @@ def set_cryptomatte_objects(collection_name, node_name):
 # -------------------------------------------------------------------
 # Utilities
 # -------------------------------------------------------------------
+def get_all_objects_in_collection(collection):
+    """Recursively get all objects in the given collection and its subcollections."""
+    objects = []
+    for obj in collection.objects:
+        objects.append(obj)
+    for subcollection in collection.children:
+        objects.extend(get_all_objects_in_collection(subcollection))
+    return objects
 
 
 def remove_collection_and_objects(collection_name):
@@ -867,60 +875,60 @@ def export_scene_to_glb(self):
     bpy.ops.object.select_all(action="DESELECT")
 
     # Select all objects in the NonConfigurable collection and its subcollections
-    collection_name = "WebGL"
-    if collection_name in bpy.data.collections:
-        collection = bpy.data.collections[collection_name]
-        select_objects_in_collection(collection)
+    collection_name = "Web"
+    for collection in bpy.data.collections:
+        if collection_name == collection.name:
+            select_objects_in_collection(collection)
+            if not check_if_selected_objects_have_parent(self):
+                return
+            # check if all objects selected have a parent, if not return
+            if not add_rig_controller_to_selection(self):
+                return
+
+            # # for each selected objects, check if the the material is principled, if not return
+            # for obj in bpy.context.selected_objects:
+            #     if not check_if_object_has_principled_material(obj):
+            #         message = f"Object {obj.name} has no principled material"
+            #         self.report({"ERROR"}, message)
+            #         return
+
+            # Export selected objects to GLB
+            bpy.ops.export_scene.gltf(
+                filepath=glb_export_path,
+                export_format="GLB",
+                use_selection=True,
+                export_apply=True,
+                export_animations=False,
+                export_yup=True,
+                export_cameras=False,
+                export_lights=False,
+                export_materials="EXPORT",
+                export_normals=True,
+                export_tangents=True,
+                export_morph=False,
+                export_skins=False,
+                export_draco_mesh_compression_enable=False,
+                export_draco_mesh_compression_level=6,
+                export_draco_position_quantization=14,
+                export_draco_normal_quantization=10,
+                export_draco_texcoord_quantization=12,
+                export_draco_color_quantization=10,
+                export_draco_generic_quantization=12,
+                export_keep_originals=False,
+                export_texture_dir="",
+            )
+
+            print(f"Exported to {glb_export_path}")
+
     else:
         print(f"Collection '{collection_name}' not found.")
         return
 
-    if not check_if_selected_objects_have_parent(self):
-        return
-    # check if all objects selected have a parent, if not return
-    if not add_rig_controller_to_selection(self):
-        return
-
-    # # for each selected objects, check if the the material is principled, if not return
-    # for obj in bpy.context.selected_objects:
-    #     if not check_if_object_has_principled_material(obj):
-    #         message = f"Object {obj.name} has no principled material"
-    #         self.report({"ERROR"}, message)
-    #         return
-
-    # Export selected objects to GLB
-    bpy.ops.export_scene.gltf(
-        filepath=glb_export_path,
-        export_format="GLB",
-        use_selection=True,
-        export_apply=True,
-        export_animations=False,
-        export_yup=True,
-        export_cameras=False,
-        export_lights=False,
-        export_materials="EXPORT",
-        export_normals=True,
-        export_tangents=True,
-        export_morph=False,
-        export_skins=False,
-        export_draco_mesh_compression_enable=False,
-        export_draco_mesh_compression_level=6,
-        export_draco_position_quantization=14,
-        export_draco_normal_quantization=10,
-        export_draco_texcoord_quantization=12,
-        export_draco_color_quantization=10,
-        export_draco_generic_quantization=12,
-        export_keep_originals=False,
-        export_texture_dir="",
-    )
-
-    print(f"Exported to {glb_export_path}")
-
 
 def copy_and_relink_textures(collection_name):
     # Ensure the target directory exists
     target_directory = bpy.path.abspath("//..")
-    textures_directory = os.path.join(target_directory, "0_Textures")
+    textures_directory = os.path.join(target_directory, "01_Textures")
     os.makedirs(textures_directory, exist_ok=True)
 
     # Get the collection
@@ -930,7 +938,8 @@ def copy_and_relink_textures(collection_name):
         return
 
     # Iterate through each object in the collection
-    for obj in collection.objects:
+    objects = get_all_objects_in_collection(collection)
+    for obj in objects:
         if obj.type != "MESH":
             continue
 
@@ -956,6 +965,9 @@ def copy_and_relink_textures(collection_name):
                                 # Check if the file already exists in the destination directory
                                 if not os.path.exists(dest_path):
                                     shutil.copy(src_path, dest_path)
+                                    print(
+                                        f"File '{filename}' copied to '{textures_directory}'"
+                                    )
                                 else:
                                     print(
                                         f"File '{filename}' already exists in '{textures_directory}', relinking."
@@ -1040,7 +1052,7 @@ class ZSSD_OT_ExportAssets(bpy.types.Operator):
 
     def execute(self, context):
         copy_and_relink_textures("NonConfigurable")
-        copy_and_relink_textures("WebGL")
+        copy_and_relink_textures("Web")
         set_assets_render_output_paths()
         export_scene_to_fbx(self)
         export_scene_to_glb(self)

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