From b3c87085dea855eff0bb4daeeedc2da3a2edf899 Mon Sep 17 00:00:00 2001 From: "kaosat.dev" Date: Thu, 25 Jul 2024 00:06:20 +0200 Subject: [PATCH] feat(Blenvy:Blender): fixed the most obvious issues with current implementation of materials handling * changed blenvy.component_add operator to be useable in scripts * experimented with using the above to add MaterialInfos as normal components * cleaned up & changed info output from auto_export steps * minor tweaks --- TODO.md | 4 ++ .../add_ons/auto_export/common/auto_export.py | 19 +++++----- .../auto_export/materials/export_materials.py | 1 + .../bevy_components/components/metadata.py | 6 ++- .../bevy_components/components/operators.py | 37 ++++++++++++++++--- tools/blenvy/materials/materials_helpers.py | 12 +++++- 6 files changed, 62 insertions(+), 17 deletions(-) diff --git a/TODO.md b/TODO.md index 998ab0c..7abc7be 100644 --- a/TODO.md +++ b/TODO.md @@ -222,6 +222,10 @@ Blender side: - [ ] materials fixes & upgrades - [x] materials do not get exported again if the files are missing, until you change a material - [x] materials do not get exported when a material is added ? + - [x] add_material_info_to_objects is now called AFTER the blueprint export, making it useless, needs change ! + - [ ] materials_path custom property should be ignored both in the list of fixable component AND on export + - [ ] if we want to add material_infos & others as normal components they should not be editable, so we need another attribute, and adapt the UI for that + - [ ] if material library is toggled, then changes to materials should not change the blueprints that are using them ? - [ ] material assets seem to be added to list regardless of whether material exports are enabled or not - [ ] review & upgrade overall logic of material libraries, their names & output path diff --git a/tools/blenvy/add_ons/auto_export/common/auto_export.py b/tools/blenvy/add_ons/auto_export/common/auto_export.py index 157711b..4fad23b 100644 --- a/tools/blenvy/add_ons/auto_export/common/auto_export.py +++ b/tools/blenvy/add_ons/auto_export/common/auto_export.py @@ -101,23 +101,24 @@ def auto_export(changes_per_scene, changes_per_collection, changes_per_material, # backup current selections old_selections = bpy.context.selected_objects - # first export any level/world scenes + # deal with materials + if export_materials_library: + print("export MATERIALS") + export_materials(materials_to_export, settings, blueprints_data) + + # export any level/world scenes if len(level_scenes_to_export) > 0: - print("export MAIN scenes") + print("export LEVELS") for scene_name in level_scenes_to_export: print(" exporting scene:", scene_name) export_level_scene(bpy.data.scenes[scene_name], settings, blueprints_data) # now deal with blueprints/collections - do_export_library_scene = not change_detection or changed_export_parameters or len(blueprints_to_export) > 0 - if do_export_library_scene: - print("export LIBRARY") + do_export_blueprints = not change_detection or changed_export_parameters or len(blueprints_to_export) > 0 + if do_export_blueprints: + print("export BLUEPRINTS") export_blueprints(blueprints_to_export, settings, blueprints_data) - # then deal with materials - if export_materials_library: - export_materials(materials_to_export, settings, blueprints_data)#blueprints_data.blueprint_names, settings.library_scenes, settings) - # reset current scene from backup bpy.context.window.scene = old_current_scene diff --git a/tools/blenvy/add_ons/auto_export/materials/export_materials.py b/tools/blenvy/add_ons/auto_export/materials/export_materials.py index 71da1dd..866b499 100644 --- a/tools/blenvy/add_ons/auto_export/materials/export_materials.py +++ b/tools/blenvy/add_ons/auto_export/materials/export_materials.py @@ -15,6 +15,7 @@ from ..common.export_gltf import (generate_gltf_export_settings) # def clear_material_info(collection_names, library_scenes): + pass for scene in library_scenes: root_collection = scene.collection for cur_collection in traverse_tree(root_collection): diff --git a/tools/blenvy/add_ons/bevy_components/components/metadata.py b/tools/blenvy/add_ons/bevy_components/components/metadata.py index da56226..e8d63c8 100644 --- a/tools/blenvy/add_ons/bevy_components/components/metadata.py +++ b/tools/blenvy/add_ons/bevy_components/components/metadata.py @@ -170,7 +170,11 @@ def add_metadata_to_components_without_metadata(item): if component_name == "components_meta": continue upsert_component_in_item(item, component_name, registry) - + +# adds a component to an item (including metadata) WITHOUT a component definition +def add_component_to_item_without_registry(): + pass + # adds a component to an item (including metadata) using the provided component definition & optional value def add_component_to_item(item, component_definition, value=None): warnings = [] diff --git a/tools/blenvy/add_ons/bevy_components/components/operators.py b/tools/blenvy/add_ons/bevy_components/components/operators.py index 165a7b3..b39f0da 100644 --- a/tools/blenvy/add_ons/bevy_components/components/operators.py +++ b/tools/blenvy/add_ons/bevy_components/components/operators.py @@ -19,15 +19,42 @@ class BLENVY_OT_component_add(Operator): description="component type to add", ) # type: ignore + component_value: StringProperty( + name="component_value", + description="value of the newly added component" + ) # type: ignore + + target_item_name: StringProperty( + name="target item name", + description="name of the object/collection/mesh/material to add the component to", + ) # type: ignore + + target_item_type: EnumProperty( + name="target item type", + description="type of the object/collection/mesh/material to add the component to", + items=( + ('OBJECT', "Object", ""), + ('COLLECTION', "Collection", ""), + ('MESH', "Mesh", ""), + ('MATERIAL', "Material", ""), + ), + default="OBJECT" + ) # type: ignore + def execute(self, context): - target = get_selected_item(context) - print("adding component ", self.component_type, "to target '"+target.name+"'") - + if self.target_item_name == "" or self.target_item_type == "": + target_item = get_selected_item(context) + print("adding component ", self.component_type, "to target '"+target_item.name+"'") + else: + target_item = get_item_by_type(self.target_item_type, self.target_item_name) + print("adding component ", self.component_type, "to target '"+target_item.name+"'") + has_component_type = self.component_type != "" - if has_component_type and target is not None: + if has_component_type and target_item is not None: type_infos = context.window_manager.components_registry.type_infos component_definition = type_infos[self.component_type] - add_component_to_item(target, component_definition) + component_value = self.component_value if self.component_value != "" else None + add_component_to_item(target_item, component_definition, value=component_value) return {'FINISHED'} diff --git a/tools/blenvy/materials/materials_helpers.py b/tools/blenvy/materials/materials_helpers.py index 3d93b92..39478ac 100644 --- a/tools/blenvy/materials/materials_helpers.py +++ b/tools/blenvy/materials/materials_helpers.py @@ -2,6 +2,7 @@ import os import posixpath import bpy from pathlib import Path + from ..core.helpers_collections import (traverse_tree) def find_materials_not_on_disk(materials, materials_path_full, extension): @@ -65,11 +66,18 @@ def add_material_info_to_objects(materials_per_object, settings): current_project_name = Path(bpy.context.blend_data.filepath).stem materials_library_name = f"{current_project_name}_materials" materials_exported_path = posixpath.join(materials_path, f"{materials_library_name}{export_gltf_extension}") + #print("ADDING MAERIAL INFOS") for object in materials_per_object.keys(): material = materials_per_object[object] - # TODO: switch to using actual components ? + + # 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 + component_value = 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'] = f'(name: "{material.name}", path: "{materials_exported_path}")' + object['MaterialInfo'] = component_value + print("adding materialInfo to object", object, "material info", component_value) # get all the materials of all objects in a given scene