|
@@ -24,6 +24,7 @@ else:
|
|
|
import os
|
|
|
import base64
|
|
|
import numpy as np
|
|
|
+ import shutil
|
|
|
|
|
|
# from . import zs_renderscene as zsrs # noqa
|
|
|
|
|
@@ -519,7 +520,12 @@ def create_cameras(scene_data):
|
|
|
|
|
|
# Add the camera to the 05_Cameras collection
|
|
|
collection.objects.link(camera)
|
|
|
- bpy.context.scene.collection.objects.unlink(camera)
|
|
|
+
|
|
|
+ # Unlink the camera from the scene collection
|
|
|
+ # check all objects in scene collection, if camera is there, unlink it
|
|
|
+ for obj in bpy.context.scene.collection.objects:
|
|
|
+ if obj.name == camera.name:
|
|
|
+ bpy.context.scene.collection.objects.unlink(camera)
|
|
|
|
|
|
# Set the camera as the active camera if "active" is true
|
|
|
if camera_data["properties"]["active"]:
|
|
@@ -911,6 +917,107 @@ def export_scene_to_glb(self):
|
|
|
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")
|
|
|
+ os.makedirs(textures_directory, exist_ok=True)
|
|
|
+
|
|
|
+ # Get the collection
|
|
|
+ collection = bpy.data.collections.get(collection_name)
|
|
|
+ if not collection:
|
|
|
+ print(f"Collection '{collection_name}' not found.")
|
|
|
+ return
|
|
|
+
|
|
|
+ # Iterate through each object in the collection
|
|
|
+ for obj in collection.objects:
|
|
|
+ if obj.type != "MESH":
|
|
|
+ continue
|
|
|
+
|
|
|
+ # Iterate through each material of the object
|
|
|
+ for mat_slot in obj.material_slots:
|
|
|
+ material = mat_slot.material
|
|
|
+ if not material:
|
|
|
+ continue
|
|
|
+
|
|
|
+ # Check if the material contains any image textures
|
|
|
+ if material.use_nodes:
|
|
|
+ for node in material.node_tree.nodes:
|
|
|
+ if node.type == "TEX_IMAGE":
|
|
|
+ image = node.image
|
|
|
+ if image:
|
|
|
+ # Copy the image texture to the target directory
|
|
|
+ src_path = bpy.path.abspath(image.filepath)
|
|
|
+ filename = os.path.basename(src_path)
|
|
|
+ dest_path = os.path.join(textures_directory, filename)
|
|
|
+
|
|
|
+ # Check if the source and destination paths are the same
|
|
|
+ if os.path.abspath(src_path) != os.path.abspath(dest_path):
|
|
|
+ # Check if the file already exists in the destination directory
|
|
|
+ if not os.path.exists(dest_path):
|
|
|
+ shutil.copy(src_path, dest_path)
|
|
|
+ else:
|
|
|
+ print(
|
|
|
+ f"File '{filename}' already exists in '{textures_directory}', relinking."
|
|
|
+ )
|
|
|
+
|
|
|
+ # Relink the image texture to the new location
|
|
|
+ image.filepath = bpy.path.relpath(dest_path)
|
|
|
+ image.reload()
|
|
|
+
|
|
|
+ print(f"Textures copied and relinked to '{textures_directory}'.")
|
|
|
+
|
|
|
+
|
|
|
+def set_assets_render_output_paths():
|
|
|
+ # Get the current scene
|
|
|
+ scene = bpy.context.scene
|
|
|
+ target_directory = bpy.path.abspath("//..")
|
|
|
+ custom_folder = os.path.join(target_directory, "PNG")
|
|
|
+ blender_file_name = os.path.splitext(bpy.path.basename(bpy.data.filepath))[0]
|
|
|
+
|
|
|
+ components = blender_file_name.split("_")
|
|
|
+ if len(components) != 7:
|
|
|
+ raise ValueError(
|
|
|
+ "Blender file name must be in the format 'Brand_AssetName_Year_ContentUsage_ContentType_AssetType_AssetNumber'"
|
|
|
+ )
|
|
|
+
|
|
|
+ brand = components[0]
|
|
|
+ asset_name = components[1]
|
|
|
+ year = components[2]
|
|
|
+ content_usage = components[3]
|
|
|
+ content_type = components[4]
|
|
|
+ asset_type = components[5]
|
|
|
+ asset_number = components[6]
|
|
|
+
|
|
|
+ imag_asset_type = "IMG"
|
|
|
+
|
|
|
+ # Construct the new .blend file name
|
|
|
+ image_name = f"{brand}_{asset_name}_{year}_{content_usage}_{content_type}_{imag_asset_type}_{asset_number}_"
|
|
|
+
|
|
|
+ # Ensure the scene has a compositor node tree
|
|
|
+ if not scene.use_nodes:
|
|
|
+ print("Scene does not use nodes.")
|
|
|
+ return
|
|
|
+
|
|
|
+ node_tree = scene.node_tree
|
|
|
+
|
|
|
+ # Search for the ZS_Canvas_Output group node
|
|
|
+ for node in node_tree.nodes:
|
|
|
+ if node.type == "GROUP" and node.node_tree.name == "ZS_Canvas_Output":
|
|
|
+ # Check inside the group for all file output nodes
|
|
|
+ for sub_node in node.node_tree.nodes:
|
|
|
+ if sub_node.type == "OUTPUT_FILE":
|
|
|
+ # Set the base_path to the custom folder
|
|
|
+ sub_node.base_path = custom_folder
|
|
|
+
|
|
|
+ # Set the path for each file output slot to the Blender file name
|
|
|
+
|
|
|
+ for output in sub_node.file_slots:
|
|
|
+ output.path = image_name
|
|
|
+
|
|
|
+ print("Output paths set.")
|
|
|
+
|
|
|
+
|
|
|
# -------------------------------------------------------------------
|
|
|
# Operators
|
|
|
# -------------------------------------------------------------------
|
|
@@ -932,6 +1039,9 @@ class ZSSD_OT_ExportAssets(bpy.types.Operator):
|
|
|
bl_description = "Export Scene Assets to FBX and GLB"
|
|
|
|
|
|
def execute(self, context):
|
|
|
+ copy_and_relink_textures("NonConfigurable")
|
|
|
+ copy_and_relink_textures("WebGL")
|
|
|
+ set_assets_render_output_paths()
|
|
|
export_scene_to_fbx(self)
|
|
|
export_scene_to_glb(self)
|
|
|
return {"FINISHED"}
|