2024-05-16 12:19:45 +00:00
|
|
|
import os
|
2024-07-21 22:29:41 +00:00
|
|
|
import posixpath
|
2024-05-16 12:19:45 +00:00
|
|
|
import bpy
|
|
|
|
from pathlib import Path
|
2024-07-24 22:06:20 +00:00
|
|
|
|
2024-05-16 12:19:45 +00:00
|
|
|
from ..core.helpers_collections import (traverse_tree)
|
|
|
|
|
2024-07-23 21:35:30 +00:00
|
|
|
def find_materials_not_on_disk(materials, materials_path_full, extension):
|
2024-07-21 22:29:41 +00:00
|
|
|
not_found_materials = []
|
2024-07-23 21:35:30 +00:00
|
|
|
|
|
|
|
current_project_name = Path(bpy.context.blend_data.filepath).stem
|
|
|
|
materials_library_name = f"{current_project_name}_materials"
|
|
|
|
materials_exported_path = os.path.join(materials_path_full, f"{materials_library_name}{extension}")
|
|
|
|
|
|
|
|
found = os.path.exists(materials_exported_path) and os.path.isfile(materials_exported_path)
|
2024-07-21 22:29:41 +00:00
|
|
|
for material in materials:
|
2024-07-23 21:35:30 +00:00
|
|
|
if not found:
|
|
|
|
not_found_materials.append(material)
|
|
|
|
|
|
|
|
"""for material in materials:
|
|
|
|
gltf_output_path = os.path.join(materials_path_full, material.name + extension)
|
2024-07-21 22:29:41 +00:00
|
|
|
# print("gltf_output_path", gltf_output_path)
|
|
|
|
found = os.path.exists(gltf_output_path) and os.path.isfile(gltf_output_path)
|
|
|
|
if not found:
|
2024-07-23 21:35:30 +00:00
|
|
|
not_found_materials.append(material)"""
|
2024-07-21 22:29:41 +00:00
|
|
|
return not_found_materials
|
|
|
|
|
|
|
|
def check_if_material_on_disk(scene_name, folder_path, extension):
|
|
|
|
gltf_output_path = os.path.join(folder_path, scene_name + extension)
|
|
|
|
found = os.path.exists(gltf_output_path) and os.path.isfile(gltf_output_path)
|
|
|
|
return found
|
|
|
|
|
2024-05-16 12:19:45 +00:00
|
|
|
# get materials per object, and injects the materialInfo component
|
2024-06-09 22:31:23 +00:00
|
|
|
def get_materials(object, materials_per_object):
|
2024-05-16 12:19:45 +00:00
|
|
|
material_slots = object.material_slots
|
|
|
|
used_materials_names = []
|
|
|
|
|
|
|
|
for m in material_slots:
|
|
|
|
material = m.material
|
|
|
|
# print(" slot", m, "material", material)
|
|
|
|
used_materials_names.append(material.name)
|
|
|
|
# TODO:, also respect slots & export multiple materials if applicable !
|
2024-07-27 10:48:47 +00:00
|
|
|
if not object in materials_per_object:
|
|
|
|
materials_per_object[object] = []
|
|
|
|
materials_per_object[object].append(material)
|
2024-05-16 12:19:45 +00:00
|
|
|
return used_materials_names
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_materials(collection_names, library_scenes):
|
|
|
|
used_material_names = []
|
2024-06-09 22:31:23 +00:00
|
|
|
materials_per_object = {}
|
|
|
|
|
2024-05-16 12:19:45 +00:00
|
|
|
for scene in library_scenes:
|
|
|
|
root_collection = scene.collection
|
|
|
|
for cur_collection in traverse_tree(root_collection):
|
|
|
|
if cur_collection.name in collection_names:
|
|
|
|
for object in cur_collection.all_objects:
|
2024-06-09 22:31:23 +00:00
|
|
|
used_material_names = used_material_names + get_materials(object, materials_per_object)
|
2024-05-16 12:19:45 +00:00
|
|
|
|
|
|
|
# we only want unique names
|
|
|
|
used_material_names = list(set(used_material_names))
|
2024-06-09 22:31:23 +00:00
|
|
|
return (used_material_names, materials_per_object)
|
|
|
|
|
|
|
|
def add_material_info_to_objects(materials_per_object, settings):
|
|
|
|
materials_path = getattr(settings, "materials_path")
|
2024-07-07 20:06:16 +00:00
|
|
|
export_gltf_extension = getattr(settings, "export_gltf_extension", ".glb")
|
2024-06-09 22:31:23 +00:00
|
|
|
|
|
|
|
current_project_name = Path(bpy.context.blend_data.filepath).stem
|
|
|
|
materials_library_name = f"{current_project_name}_materials"
|
2024-07-21 22:29:41 +00:00
|
|
|
materials_exported_path = posixpath.join(materials_path, f"{materials_library_name}{export_gltf_extension}")
|
2024-07-24 22:06:20 +00:00
|
|
|
#print("ADDING MAERIAL INFOS")
|
2024-06-09 22:31:23 +00:00
|
|
|
for object in materials_per_object.keys():
|
2024-07-27 10:48:47 +00:00
|
|
|
material_infos = []
|
|
|
|
for material in materials_per_object[object]:
|
|
|
|
# problem with using actual components: you NEED the type registry/component infos, so if there is none , or it is not loaded yet, it does not work
|
|
|
|
# for a few components we could hardcode this
|
|
|
|
material_info = f'(name: "{material.name}", path: "{materials_exported_path}")'
|
|
|
|
#bpy.ops.blenvy.component_add(target_item_name=object.name, target_item_type="OBJECT", component_type="blenvy::blueprints::materials::MaterialInfo", component_value=component_value)
|
|
|
|
|
|
|
|
materials_exported_path = posixpath.join(materials_path, f"{materials_library_name}{export_gltf_extension}")
|
|
|
|
#object['MaterialInfo'] = component_value
|
|
|
|
material_infos.append(material_info)
|
|
|
|
object['MaterialInfos'] = f"({material_infos})".replace("'","")
|
|
|
|
print("adding materialInfos to object", object, "material infos", material_infos)
|
2024-06-10 11:58:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
# get all the materials of all objects in a given scene
|
|
|
|
def get_scene_materials(scene):
|
|
|
|
used_material_names = []
|
|
|
|
materials_per_object = {}
|
|
|
|
|
|
|
|
root_collection = scene.collection
|
|
|
|
for cur_collection in traverse_tree(root_collection):
|
|
|
|
for object in cur_collection.all_objects:
|
|
|
|
used_material_names = used_material_names + get_materials(object, materials_per_object)
|
|
|
|
|
|
|
|
# we only want unique names
|
|
|
|
used_material_names = list(set(used_material_names))
|
|
|
|
return (used_material_names, materials_per_object)
|
|
|
|
|
|
|
|
# get all the materials of all objects used by a given blueprint
|
|
|
|
def get_blueprint_materials(blueprint):
|
|
|
|
materials_per_object = {}
|
|
|
|
used_material_names = []
|
|
|
|
|
|
|
|
for object in blueprint.collection.all_objects:
|
|
|
|
used_material_names = used_material_names + get_materials(object, materials_per_object)
|
|
|
|
|
|
|
|
# we only want unique names
|
|
|
|
used_material_names = list(set(used_material_names))
|
|
|
|
return (used_material_names, materials_per_object)
|