diff --git a/leenkx/blender/lnx/make.py b/leenkx/blender/lnx/make.py index fdcb0e9..b938eaa 100644 --- a/leenkx/blender/lnx/make.py +++ b/leenkx/blender/lnx/make.py @@ -116,7 +116,73 @@ def remove_readonly(func, path, excinfo): os.chmod(path, stat.S_IWRITE) func(path) + +appended_scenes = [] + +def load_external_blends(): + global appended_scenes + + wrd = bpy.data.worlds['Lnx'] + if not hasattr(wrd, 'lnx_external_blends_path'): + return + + external_path = getattr(wrd, 'lnx_external_blends_path', '') + if not external_path or not external_path.strip(): + return + + abs_path = bpy.path.abspath(external_path.strip()) + if not os.path.exists(abs_path): + return + + # Walk recursively through all subdirs + for root, dirs, files in os.walk(abs_path): + for filename in files: + if not filename.endswith(".blend"): + continue + + blend_path = os.path.join(root, filename) + try: + with bpy.data.libraries.load(blend_path, link=True) as (data_from, data_to): + data_to.scenes = list(data_from.scenes) + + for scn in data_to.scenes: + if scn is not None and scn not in appended_scenes: + # make name unique with file name + scn.name += "_" + filename.replace(".blend", "") + appended_scenes.append(scn) + + log.info(f"Loaded external blend: {blend_path}") + except Exception as e: + log.error(f"Failed to load external blend {blend_path}: {e}") + +def clear_external_scenes(): + global appended_scenes + if not appended_scenes: + return + + for scn in appended_scenes: + try: + bpy.data.scenes.remove(scn, do_unlink=True) + except Exception as e: + log.error(f"Failed to remove scene {scn.name}: {e}") + + for lib in list(bpy.data.libraries): + try: + if lib.users == 0: + bpy.data.libraries.remove(lib) + except Exception as e: + log.error(f"Failed to remove library {lib.name}: {e}") + + try: + bpy.ops.outliner.orphans_purge(do_local_ids=True, do_linked_ids=True, do_recursive=True) + except Exception as e: + log.error(f"Failed to purge orphan data: {e}") + + appended_scenes = [] + def export_data(fp, sdk_path): + load_external_blends() + wrd = bpy.data.worlds['Lnx'] rpdat = lnx.utils.get_rp() @@ -323,6 +389,8 @@ def export_data(fp, sdk_path): state.last_resy = resy state.last_scene = scene_name + clear_external_scenes() + def compile(assets_only=False): wrd = bpy.data.worlds['Lnx'] fp = lnx.utils.get_fp()