feat(auto_export): more work on correct parameter change detection from gltf & auto export

* forcing depsgraph update when applying changes to gltf export settings
 * lots of additional boilerplate for params change detection
 * further updates to get information about changed parameters & objects in tracker
 * a lot of related tweaks & changes
chore(bevy_components): removed annoying verbose info messages
This commit is contained in:
kaosat.dev 2024-04-12 12:29:32 +02:00
parent 6a1594188e
commit 98930af5f5
12 changed files with 134 additions and 33 deletions

View File

@ -248,18 +248,15 @@ def apply_propertyGroup_values_to_object_customProperties(object):
# apply component value(s) to custom property of a single component
def apply_propertyGroup_values_to_object_customProperties_for_component(object, component_name):
registry = bpy.context.window_manager.components_registry
print("yallah", component_name)
(_, propertyGroup) = upsert_component_in_object(object, component_name, registry)
component_definition = find_component_definition_from_short_name(component_name)
if component_definition != None:
print("merde")
value = property_group_value_to_custom_property_value(propertyGroup, component_definition, registry, None)
object[component_name] = value
components_metadata = object.components_meta.components
componentMeta = next(filter(lambda component: component["name"] == component_name, components_metadata), None)
if componentMeta:
print("here")
componentMeta.invalid = False
componentMeta.invalid_details = ""

View File

@ -18,12 +18,11 @@ class MissingBevyType(bpy.types.PropertyGroup):
# helper function to deal with timer
def toggle_watcher(self, context):
print("toggling watcher", self.watcher_enabled, watch_schema, self, bpy.app.timers)
#print("toggling watcher", self.watcher_enabled, watch_schema, self, bpy.app.timers)
if not self.watcher_enabled:
try:
bpy.app.timers.unregister(watch_schema)
except Exception as error:
print("failed to unregister", error)
pass
else:
self.watcher_active = True
@ -236,7 +235,6 @@ class ComponentsRegistry(PropertyGroup):
try:
bpy.app.timers.unregister(watch_schema)
except Exception as error:
print("failed to unregister", error)
pass
del bpy.types.WindowManager.components_registry

View File

@ -18,6 +18,7 @@ from bpy.types import Context
from bpy.props import (StringProperty, BoolProperty, IntProperty, PointerProperty)
import rna_prop_ui
# from .extension import ExampleExtensionProperties, GLTF_PT_UserExtensionPanel, unregister_panel
from .auto_export.operators import AutoExportGLTF
@ -41,6 +42,8 @@ from .ui.main import (GLTF_PT_auto_export_changes_list, GLTF_PT_auto_export_main
GLTF_PT_auto_export_SidePanel
)
from .ui.operators import (SCENES_LIST_OT_actions)
from .helpers.ping_depsgraph_update import ping_depsgraph_update
from .helpers.generate_complete_preferences_dict import generate_complete_preferences_dict_gltf
######################################################
@ -149,12 +152,18 @@ def glTF2_post_export_callback(data):
# get the parameters
scene = bpy.context.scene
print(dict(scene))
if "glTF2ExportSettings" in scene:
print("write gltf settings")
settings = scene["glTF2ExportSettings"]
export_settings = bpy.data.texts[".gltf_auto_export_gltf_settings"] if ".gltf_auto_export_gltf_settings" in bpy.data.texts else bpy.data.texts.new(".gltf_auto_export_gltf_settings")
# now write new settings
export_settings.clear()
export_settings.write(json.dumps(dict(settings)))
current_gltf_settings = generate_complete_preferences_dict_gltf(dict(settings))
print("current_gltf_settings", current_gltf_settings)
export_settings.write(json.dumps(current_gltf_settings))
print("done writing")
# now reset the original gltf_settings
if gltf_settings_backup != "":
scene["glTF2ExportSettings"] = json.loads(gltf_settings_backup)
@ -168,6 +177,10 @@ def glTF2_post_export_callback(data):
last_operator.filepath = ""
last_operator.gltf_export_id = ""
# AGAIN, something that does not work withouth a timer
bpy.app.timers.register(ping_depsgraph_update, first_interval=0.1)
def menu_func_import(self, context):
self.layout.operator(AutoExportGLTF.bl_idname, text="glTF auto Export (.glb/gltf)")

View File

@ -48,12 +48,11 @@ def auto_export(changes_per_scene, changed_export_parameters, addon_prefs):
# here we do a bit of workaround by creating an override # TODO: do this at the "UI" level
export_blueprints_path = os.path.join(folder_path, export_output_folder, getattr(addon_prefs,"export_blueprints_path")) if getattr(addon_prefs,"export_blueprints_path") != '' else folder_path
#print('addon_prefs', AutoExportGltfAddonPreferences.__annotations__)#)addon_prefs.__annotations__)
if hasattr(addon_prefs, "__annotations__") :
tmp = {}
for k in AutoExportGltfAddonPreferences.__annotations__:
item = AutoExportGltfAddonPreferences.__annotations__[k]
print("tutu",k, item.keywords.get('default', None) )
#print("tutu",k, item.keywords.get('default', None) )
default = item.keywords.get('default', None)
tmp[k] = default

View File

@ -0,0 +1,39 @@
import json
import bpy
"""
This should ONLY be run when actually doing exports/aka calling auto_export function, because we only care about the difference in settings between EXPORTS
"""
def did_export_settings_change():
# compare both the auto export settings & the gltf settings
previous_auto_settings = bpy.data.texts[".gltf_auto_export_settings_previous"] if ".gltf_auto_export_settings_previous" in bpy.data.texts else None
previous_gltf_settings = bpy.data.texts[".gltf_auto_export_gltf_settings_previous"] if ".gltf_auto_export_gltf_settings_previous" in bpy.data.texts else None
current_auto_settings = bpy.data.texts[".gltf_auto_export_settings"] if ".gltf_auto_export_settings" in bpy.data.texts else None
current_gltf_settings = bpy.data.texts[".gltf_auto_export_gltf_settings"] if ".gltf_auto_export_gltf_settings" in bpy.data.texts else None
#check if params have changed
# if there were no setting before, it is new, we need export
changed = False
if previous_auto_settings == None:
print("previous settings missing, exporting")
changed = True
elif previous_gltf_settings == None:
print("previous gltf settings missing, exporting")
changed = True
else:
auto_settings_changed = sorted(json.loads(previous_auto_settings.as_string()).items()) != sorted(json.loads(current_auto_settings.as_string()).items()) if current_auto_settings != None else False
gltf_settings_changed = sorted(json.loads(previous_gltf_settings.as_string()).items()) != sorted(json.loads(current_gltf_settings.as_string()).items()) if current_gltf_settings != None else False
"""print("auto settings previous", sorted(json.loads(previous_auto_settings.as_string()).items()))
print("auto settings current", sorted(json.loads(current_auto_settings.as_string()).items()))"""
print("auto_settings_changed", auto_settings_changed)
"""print("gltf settings previous", sorted(json.loads(previous_gltf_settings.as_string()).items()))
print("gltf settings current", sorted(json.loads(current_gltf_settings.as_string()).items()))"""
print("gltf_settings_changed", gltf_settings_changed)
changed = auto_settings_changed or gltf_settings_changed
return changed

View File

@ -50,7 +50,6 @@ def generate_gltf_export_preferences(addon_prefs):
standard_gltf_exporter_settings = get_standard_exporter_settings()
#print("standard settings", standard_gltf_exporter_settings)
constant_keys = [
'use_selection',

View File

@ -16,7 +16,7 @@ def get_collections_to_export(changes_per_scene, changed_export_parameters, addo
(collections, blueprint_hierarchy) = get_exportable_collections(level_scenes, library_scenes, addon_prefs)
collections_to_export = collections # just for clarity
print("export_change_detection", export_change_detection, export_gltf_extension, export_blueprints_path)
print("export_change_detection", export_change_detection, export_gltf_extension, export_blueprints_path, changed_export_parameters, changes_per_scene)
# if the export parameters have changed, bail out early
# we need to re_export everything if the export parameters have been changed

View File

@ -6,6 +6,7 @@ import bpy
from bpy.types import (PropertyGroup)
from bpy.props import (PointerProperty, IntProperty, StringProperty)
from .did_export_settings_change import did_export_settings_change
from .get_collections_to_export import get_collections_to_export
from ..constants import TEMPSCENE_PREFIX
@ -117,7 +118,7 @@ class AutoExportTracker(PropertyGroup):
if isinstance(obj.id, bpy.types.Object):
# get the actual object
object = bpy.data.objects[obj.id.name]
# print(" changed object", obj.id.name,"transforms", obj.is_updated_transform, "geometry", obj.is_updated_geometry)
print(" changed object", obj.id.name, "changes", obj, "transforms", obj.is_updated_transform, "geometry", obj.is_updated_geometry)
cls.changed_objects_per_scene[scene.name][obj.id.name] = object
elif isinstance(obj.id, bpy.types.Material): # or isinstance(obj.id, bpy.types.ShaderNodeTree):
# print(" changed material", obj.id, "scene", scene.name,)
@ -140,16 +141,16 @@ class AutoExportTracker(PropertyGroup):
# get a list of exportable collections for display
# keep it simple, just use Simplenamespace for compatibility with the rest of our code
# TODO: debounce
export_settings_changed = did_export_settings_change()
tmp = {}
for k in AutoExportGltfAddonPreferences.__annotations__:
item = AutoExportGltfAddonPreferences.__annotations__[k]
print("tutu",k, item.keywords.get('default', None) )
default = item.keywords.get('default', None)
tmp[k] = default
auto_settings = get_auto_exporter_settings()
for k in auto_settings:
print("k", k, auto_settings[k])
tmp[k] = auto_settings[k]
tmp['__annotations__'] = tmp
@ -164,14 +165,7 @@ class AutoExportTracker(PropertyGroup):
tmp["export_models_path"] = export_models_path
addon_prefs = SimpleNamespace(**tmp)
#
#addon_prefs.export_blueprints_path = export_blueprints_path
#addon_prefs.export_gltf_extension = gltf_extension
#addon_prefs.export_models_path = export_models_path
(collections, collections_to_export, library_collections, collections_per_scene) = get_collections_to_export(cls.changed_objects_per_scene, False, addon_prefs)
(collections, collections_to_export, library_collections, collections_per_scene) = get_collections_to_export(cls.changed_objects_per_scene, export_settings_changed, addon_prefs)
print("collections to export", collections_to_export)
try:
# we save this list of collections in the context

View File

@ -0,0 +1,38 @@
from ..auto_export.preferences import AutoExportGltfAddonPreferences
from io_scene_gltf2 import (ExportGLTF2, GLTF_PT_export_main,ExportGLTF2_Base, GLTF_PT_export_include)
def generate_complete_preferences_dict_gltf(settings):
complete_preferences = {}
defaults = {}
gltf_parameters_to_ignore = ["use_active_collection", "use_active_collection_with_nested", "use_active_scene", "use_selection", "will_save_settings", "gltf_export_id"]
def filter_out(pair):
key, value = pair
if key in gltf_parameters_to_ignore:
return False
return True
for k in ExportGLTF2_Base.__annotations__: # we use parameters from the base class of the standard gltf exporter, that contains all relevant parameters
item = ExportGLTF2_Base.__annotations__[k]
#print("item", item)
default = item.keywords.get('default', None)
#complete_preferences[k] = default
defaults[k] = default
for key in list(settings.keys()):
if key in defaults and settings[key] != defaults[key]: # only write out values different from defaults
complete_preferences[key] = settings[key]
complete_preferences = dict(filter(filter_out, dict(complete_preferences).items()))
return complete_preferences
def generate_complete_preferences_dict_auto(settings):
complete_preferences = {}
for k in AutoExportGltfAddonPreferences.__annotations__:
item = AutoExportGltfAddonPreferences.__annotations__[k]
default = item.keywords.get('default', None)
complete_preferences[k] = default
for key in list(settings.keys()):
complete_preferences[key] = settings[key]
return complete_preferences

View File

@ -8,7 +8,7 @@ from .object_makers import (make_empty)
custom_properties_to_filter_out = ['_combine', 'template', 'components_meta']
def is_component_valid(object, component_name):
if "components_meta" in object:
if "components_meta" in object or hasattr(object, "components_meta"):
target_components_metadata = object.components_meta.components
component_meta = next(filter(lambda component: component["name"] == component_name, target_components_metadata), None)
if component_meta != None:
@ -17,7 +17,8 @@ def is_component_valid(object, component_name):
def remove_unwanted_custom_properties(object):
to_remove = []
for component_name in object.keys():
component_names = list(object.keys()) # to avoid 'IDPropertyGroup changed size during iteration' issues
for component_name in component_names:
if not is_component_valid(object, component_name):
to_remove.append(component_name)
for cp in custom_properties_to_filter_out + to_remove:
@ -27,7 +28,7 @@ def remove_unwanted_custom_properties(object):
# TODO: rename actions ?
# reference https://github.com/KhronosGroup/glTF-Blender-IO/blob/main/addons/io_scene_gltf2/blender/exp/animation/gltf2_blender_gather_action.py#L481
def copy_animation_data(source, target):
if source.animation_data and source.animation_data:
if source.animation_data:
ad = source.animation_data
blender_actions = []
@ -65,8 +66,27 @@ def copy_animation_data(source, target):
target.animation_data_create()
target.animation_data.action = source.animation_data.action.copy()"""
# alternative method, using the built-in link animation operator
#
#previous_active_object = bpy.context.view_layer.objects.active
"""bpy.context.view_layer.objects.active = source
bpy.ops.object.select_all(action='DESELECT')
#Transfer data from active object to selected objects
target.select_set(True) """
with bpy.context.temp_override(active_object=source, selected_editable_objects=[target]):
bpy.ops.object.make_links_data(type='ANIMATION')
"""if target.animation_data == None:
target.animation_data_create()
print("copying animation data for", source.name, target.animation_data)
properties = [p.identifier for p in source.animation_data.bl_rna.properties if not p.is_readonly]
for prop in properties:
print("copying stuff", prop)
setattr(target.animation_data, prop, getattr(source.animation_data, prop))"""
# we add an "AnimationInfos" component
target['AnimationInfos'] = f'(animations: {animations_infos})'.replace("'","")
@ -81,11 +101,7 @@ def copy_animation_data(source, target):
markers_formated += '}'
target["AnimationMarkers"] = f'( {markers_formated} )'
"""print("copying animation data for", source.name, target.animation_data)
properties = [p.identifier for p in source.animation_data.bl_rna.properties if not p.is_readonly]
for prop in properties:
print("copying stuff", prop)
setattr(target.animation_data, prop, getattr(source.animation_data, prop))"""

View File

@ -0,0 +1,10 @@
import bpy
import rna_prop_ui
# fake way to make our operator's changes be visible to the change/depsgraph update handler in gltf_auto_export
def ping_depsgraph_update(object=None):
if object == None:
object = bpy.data.scenes[0]
rna_prop_ui.rna_idprop_ui_create(object, "________temp", default=0)
rna_prop_ui.rna_idprop_ui_prop_clear(object, "________temp")
return None

View File

@ -60,8 +60,6 @@ def make_material_object(name, location=[0,0,0], rotation=[0,0,0], scale=[1,1,1]
else:
# no slots
object.data.materials.append(material)
#bpy.context.view_layer.objects.active = original_active_object
return object