2024-05-15 21:32:01 +00:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import bpy
|
|
|
|
|
2024-05-16 12:09:40 +00:00
|
|
|
from .asset_helpers import does_asset_exist, get_user_assets, get_user_assets_as_list
|
2024-05-15 21:32:01 +00:00
|
|
|
|
2024-05-25 08:46:14 +00:00
|
|
|
def scan_assets(scene, blueprints_data, settings):
|
|
|
|
project_root_path = getattr(settings, "project_root_path")
|
|
|
|
export_output_folder = getattr(settings,"export_output_folder")
|
|
|
|
levels_path = getattr(settings,"levels_path")
|
|
|
|
blueprints_path = getattr(settings, "blueprints_path")
|
|
|
|
export_gltf_extension = getattr(settings, "export_gltf_extension")
|
2024-05-15 21:32:01 +00:00
|
|
|
|
2024-05-17 12:30:15 +00:00
|
|
|
relative_blueprints_path = os.path.relpath(blueprints_path, project_root_path)
|
2024-05-15 21:32:01 +00:00
|
|
|
blueprint_instance_names_for_scene = blueprints_data.blueprint_instances_per_main_scene.get(scene.name, None)
|
|
|
|
|
|
|
|
blueprint_assets_list = []
|
|
|
|
if blueprint_instance_names_for_scene:
|
|
|
|
for blueprint_name in blueprint_instance_names_for_scene:
|
|
|
|
blueprint = blueprints_data.blueprints_per_name.get(blueprint_name, None)
|
|
|
|
if blueprint is not None:
|
2024-06-25 16:27:52 +00:00
|
|
|
#print("BLUEPRINT", blueprint)
|
2024-05-15 21:32:01 +00:00
|
|
|
blueprint_exported_path = None
|
|
|
|
if blueprint.local:
|
|
|
|
blueprint_exported_path = os.path.join(relative_blueprints_path, f"{blueprint.name}{export_gltf_extension}")
|
|
|
|
else:
|
|
|
|
# get the injected path of the external blueprints
|
|
|
|
blueprint_exported_path = blueprint.collection['Export_path'] if 'Export_path' in blueprint.collection else None
|
|
|
|
if blueprint_exported_path is not None:
|
|
|
|
blueprint_assets_list.append({"name": blueprint.name, "path": blueprint_exported_path})
|
|
|
|
|
|
|
|
|
|
|
|
# fetch images/textures
|
|
|
|
# see https://blender.stackexchange.com/questions/139859/how-to-get-absolute-file-path-for-linked-texture-image
|
|
|
|
textures = []
|
|
|
|
for ob in bpy.data.objects:
|
|
|
|
if ob.type == "MESH":
|
|
|
|
for mat_slot in ob.material_slots:
|
|
|
|
if mat_slot.material:
|
|
|
|
if mat_slot.material.node_tree:
|
|
|
|
textures.extend([x.image.filepath for x in mat_slot.material.node_tree.nodes if x.type=='TEX_IMAGE'])
|
|
|
|
print("textures", textures)
|
|
|
|
|
|
|
|
assets_list_name = f"assets_{scene.name}"
|
|
|
|
assets_list_data = {"blueprints": json.dumps(blueprint_assets_list), "sounds":[], "images":[]}
|
|
|
|
|
2024-06-25 16:27:52 +00:00
|
|
|
#print("blueprint assets", blueprint_assets_list)
|
2024-05-15 21:32:01 +00:00
|
|
|
|
|
|
|
|
2024-05-16 12:09:40 +00:00
|
|
|
def get_userTextures():
|
|
|
|
# TODO: limit this to the ones actually in use
|
|
|
|
# fetch images/textures
|
|
|
|
# see https://blender.stackexchange.com/questions/139859/how-to-get-absolute-file-path-for-linked-texture-image
|
|
|
|
textures = []
|
|
|
|
for ob in bpy.data.objects:
|
|
|
|
if ob.type == "MESH":
|
|
|
|
for mat_slot in ob.material_slots:
|
|
|
|
if mat_slot.material:
|
|
|
|
if mat_slot.material.node_tree:
|
|
|
|
textures.extend([x.image.filepath for x in mat_slot.material.node_tree.nodes if x.type=='TEX_IMAGE'])
|
|
|
|
print("textures", textures)
|
|
|
|
|
2024-05-25 08:46:14 +00:00
|
|
|
def get_blueprint_assets_tree(blueprint, blueprints_data, parent, settings):
|
|
|
|
blueprints_path = getattr(settings, "blueprints_path")
|
2024-07-07 20:06:16 +00:00
|
|
|
export_gltf_extension = getattr(settings, "export_gltf_extension", ".glb")
|
2024-05-15 21:32:01 +00:00
|
|
|
assets_list = []
|
|
|
|
|
|
|
|
|
|
|
|
for blueprint_name in blueprint.nested_blueprints:
|
|
|
|
child_blueprint = blueprints_data.blueprints_per_name.get(blueprint_name, None)
|
|
|
|
if child_blueprint:
|
|
|
|
blueprint_exported_path = None
|
|
|
|
if blueprint.local:
|
2024-05-17 12:30:15 +00:00
|
|
|
blueprint_exported_path = os.path.join(blueprints_path, f"{child_blueprint.name}{export_gltf_extension}")
|
2024-05-15 21:32:01 +00:00
|
|
|
else:
|
|
|
|
# get the injected path of the external blueprints
|
|
|
|
blueprint_exported_path = child_blueprint.collection['export_path'] if 'export_path' in child_blueprint.collection else None
|
|
|
|
if blueprint_exported_path is not None:
|
2024-05-16 12:09:40 +00:00
|
|
|
assets_list.append({"name": child_blueprint.name, "path": blueprint_exported_path, "type": "MODEL", "generated": True,"internal":blueprint.local, "parent": blueprint.name})
|
2024-05-15 21:32:01 +00:00
|
|
|
|
|
|
|
# and add sub stuff
|
2024-05-25 08:46:14 +00:00
|
|
|
sub_assets_lists = get_blueprint_assets_tree(child_blueprint, blueprints_data, parent=child_blueprint.name, settings=settings)
|
2024-05-15 21:32:01 +00:00
|
|
|
assets_list += sub_assets_lists
|
|
|
|
|
2024-05-16 12:09:40 +00:00
|
|
|
direct_assets = get_user_assets_as_list(blueprint.collection)
|
2024-05-15 21:32:01 +00:00
|
|
|
for asset in direct_assets:
|
|
|
|
asset["parent"] = parent
|
2024-05-16 12:09:40 +00:00
|
|
|
asset["internal"] = blueprint.local
|
2024-05-15 21:32:01 +00:00
|
|
|
assets_list += direct_assets
|
|
|
|
return assets_list
|
|
|
|
|
2024-05-25 08:46:14 +00:00
|
|
|
def get_main_scene_assets_tree(main_scene, blueprints_data, settings):
|
|
|
|
blueprints_path = getattr(settings, "blueprints_path")
|
2024-07-07 20:06:16 +00:00
|
|
|
export_gltf_extension = getattr(settings, "export_gltf_extension", ".glb")
|
2024-05-15 21:32:01 +00:00
|
|
|
blueprint_instance_names_for_scene = blueprints_data.blueprint_instances_per_main_scene.get(main_scene.name, None)
|
|
|
|
|
2024-05-16 12:09:40 +00:00
|
|
|
assets_list = get_user_assets_as_list(main_scene)
|
2024-05-15 21:32:01 +00:00
|
|
|
if blueprint_instance_names_for_scene:
|
|
|
|
for blueprint_name in blueprint_instance_names_for_scene:
|
|
|
|
blueprint = blueprints_data.blueprints_per_name.get(blueprint_name, None)
|
|
|
|
if blueprint is not None:
|
|
|
|
blueprint_exported_path = None
|
|
|
|
if blueprint.local:
|
2024-05-17 12:30:15 +00:00
|
|
|
blueprint_exported_path = os.path.join(blueprints_path, f"{blueprint.name}{export_gltf_extension}")
|
2024-05-15 21:32:01 +00:00
|
|
|
else:
|
|
|
|
# get the injected path of the external blueprints
|
|
|
|
blueprint_exported_path = blueprint.collection['export_path'] if 'export_path' in blueprint.collection else None
|
|
|
|
if blueprint_exported_path is not None and not does_asset_exist(assets_list, blueprint_exported_path):
|
2024-05-16 12:09:40 +00:00
|
|
|
assets_list.append({"name": blueprint.name, "path": blueprint_exported_path, "type": "MODEL", "generated": True, "internal":blueprint.local, "parent": None})
|
2024-05-15 21:32:01 +00:00
|
|
|
|
2024-05-25 08:46:14 +00:00
|
|
|
assets_list += get_blueprint_assets_tree(blueprint, blueprints_data, parent=blueprint.name, settings=settings)
|
2024-05-15 21:32:01 +00:00
|
|
|
|
|
|
|
print("TOTAL ASSETS", assets_list)
|
2024-06-05 22:12:17 +00:00
|
|
|
# FIXME: do not do it here !!
|
|
|
|
scene = bpy.data.scenes[main_scene.name]
|
|
|
|
scene.generated_assets.clear()
|
|
|
|
for asset in assets_list:
|
|
|
|
if asset.get("generated", False):
|
|
|
|
added_asset = scene.generated_assets.add()
|
|
|
|
added_asset.name = asset["name"]
|
|
|
|
added_asset.path = asset["path"]
|
2024-05-15 21:32:01 +00:00
|
|
|
return assets_list
|
2024-06-23 17:14:00 +00:00
|
|
|
|
2024-07-10 23:06:27 +00:00
|
|
|
# same as the above, withouth the clutter below : TODO: unify
|
|
|
|
def get_main_scene_assets_tree2(main_scene, blueprints_data, settings):
|
|
|
|
blueprints_path = getattr(settings, "blueprints_path")
|
|
|
|
export_gltf_extension = getattr(settings, "export_gltf_extension", ".glb")
|
|
|
|
blueprint_instance_names_for_scene = blueprints_data.blueprint_instances_per_main_scene.get(main_scene.name, None)
|
|
|
|
|
|
|
|
assets_list = get_user_assets_as_list(main_scene)
|
|
|
|
if blueprint_instance_names_for_scene:
|
|
|
|
for blueprint_name in blueprint_instance_names_for_scene:
|
|
|
|
blueprint = blueprints_data.blueprints_per_name.get(blueprint_name, None)
|
|
|
|
if blueprint is not None:
|
|
|
|
blueprint_exported_path = None
|
|
|
|
if blueprint.local:
|
|
|
|
blueprint_exported_path = os.path.join(blueprints_path, f"{blueprint.name}{export_gltf_extension}")
|
|
|
|
else:
|
|
|
|
# get the injected path of the external blueprints
|
|
|
|
blueprint_exported_path = blueprint.collection['export_path'] if 'export_path' in blueprint.collection else None
|
|
|
|
if blueprint_exported_path is not None and not does_asset_exist(assets_list, blueprint_exported_path):
|
|
|
|
assets_list.append({"name": blueprint.name, "path": blueprint_exported_path, "type": "MODEL", "generated": True, "internal":blueprint.local, "parent": None})
|
|
|
|
|
|
|
|
assets_list += get_blueprint_assets_tree(blueprint, blueprints_data, parent=blueprint.name, settings=settings)
|
|
|
|
|
|
|
|
return assets_list
|
|
|
|
|
2024-06-23 17:14:00 +00:00
|
|
|
def get_blueprint_asset_tree(blueprint, blueprints_data, settings):
|
|
|
|
blueprints_path = getattr(settings, "blueprints_path")
|
2024-07-07 20:06:16 +00:00
|
|
|
export_gltf_extension = getattr(settings, "export_gltf_extension", ".glb")
|
2024-06-23 17:14:00 +00:00
|
|
|
|
|
|
|
assets_list = get_user_assets_as_list(blueprint.collection)
|
|
|
|
|
|
|
|
for blueprint_name in blueprint.nested_blueprints:
|
|
|
|
sub_blueprint = blueprints_data.blueprints_per_name.get(blueprint_name, None)
|
|
|
|
if sub_blueprint is not None:
|
|
|
|
sub_blueprint_exported_path = None
|
|
|
|
if sub_blueprint.local:
|
|
|
|
sub_blueprint_exported_path = os.path.join(blueprints_path, f"{sub_blueprint.name}{export_gltf_extension}")
|
|
|
|
else:
|
|
|
|
# get the injected path of the external blueprints
|
|
|
|
sub_blueprint_exported_path = sub_blueprint.collection['export_path'] if 'export_path' in sub_blueprint.collection else None
|
|
|
|
|
|
|
|
if sub_blueprint_exported_path is not None and not does_asset_exist(assets_list, sub_blueprint_exported_path):
|
|
|
|
assets_list.append({"name": sub_blueprint.name, "path": sub_blueprint_exported_path, "type": "MODEL", "generated": True, "internal": sub_blueprint.local, "parent": None})
|
|
|
|
|
|
|
|
assets_list += get_blueprint_assets_tree(sub_blueprint, blueprints_data, parent=sub_blueprint.name, settings=settings)
|
|
|
|
|
|
|
|
return assets_list
|