asset_library/operators.py

151 lines
4.3 KiB
Python
Raw Normal View History

2022-12-24 15:30:32 +01:00
import importlib
import bpy
2024-05-27 17:22:45 +02:00
from bpy.types import Operator
from bpy.props import (BoolProperty, EnumProperty, StringProperty, IntProperty)
from .core.bl_utils import get_addon_prefs, unique_name
2023-01-03 13:53:01 +01:00
2024-05-27 17:22:45 +02:00
class ASSETLIB_OT_reload_addon(Operator):
bl_idname = "assetlibrary.reload_addon"
bl_options = {"UNDO"}
bl_label = 'Reload Asset Library Addon'
bl_description = 'Reload The Asset Library Addon and the addapters'
2023-01-03 13:53:01 +01:00
2024-05-27 17:22:45 +02:00
def execute(self, context):
2022-12-24 15:30:32 +01:00
2024-05-27 17:22:45 +02:00
print('Execute reload', __package__)
2022-12-24 15:30:32 +01:00
2024-05-27 17:22:45 +02:00
addon = importlib.import_module(__package__)
2022-12-24 15:30:32 +01:00
2024-05-27 17:22:45 +02:00
addon.unregister()
importlib.reload(addon)
for mod in addon.modules:
importlib.reload(mod)
addon.register()
2022-12-24 15:30:32 +01:00
2024-05-27 17:22:45 +02:00
return {'FINISHED'}
2022-12-24 15:30:32 +01:00
2024-05-27 17:22:45 +02:00
class ASSETLIB_OT_remove_library(Operator):
bl_idname = "assetlibrary.remove_library"
2022-12-24 15:30:32 +01:00
bl_options = {"REGISTER", "UNDO"}
2024-05-27 17:22:45 +02:00
bl_label = 'Remove Library'
bl_description = 'Remove Library'
2022-12-24 15:30:32 +01:00
index : IntProperty(default=-1)
2024-05-27 17:22:45 +02:00
def execute(self, context):
2022-12-24 15:30:32 +01:00
prefs = get_addon_prefs()
2024-05-27 17:22:45 +02:00
addon_lib = prefs.libraries[self.index]
2022-12-24 15:30:32 +01:00
2024-05-27 17:22:45 +02:00
bl_libs = context.preferences.filepaths.asset_libraries
if (bl_lib := bl_libs.get(addon_lib.name)) and bl_lib.path == addon_lib.path:
index = list(bl_libs).index(bl_lib)
bpy.ops.preferences.asset_library_remove(index=index)
prefs.libraries.remove(self.index)
2022-12-24 15:30:32 +01:00
return {'FINISHED'}
2024-05-27 17:22:45 +02:00
class ASSETLIB_OT_add_library(Operator):
bl_idname = "assetlibrary.add_library"
2022-12-24 15:30:32 +01:00
bl_options = {"REGISTER", "UNDO"}
2024-05-27 17:22:45 +02:00
bl_label = 'Add Library'
bl_description = 'Add Library'
2022-12-24 15:30:32 +01:00
2024-05-27 17:22:45 +02:00
def execute(self, context):
2022-12-24 15:30:32 +01:00
prefs = get_addon_prefs()
2024-05-27 17:22:45 +02:00
lib = prefs.libraries.add()
2022-12-24 15:30:32 +01:00
lib.expand = True
2024-05-27 17:22:45 +02:00
lib.name = unique_name('Asset Library', [l.name for l in prefs.libraries])
2022-12-24 15:30:32 +01:00
return {'FINISHED'}
2024-05-27 17:22:45 +02:00
class ASSETLIB_OT_synchronize(Operator):
bl_idname = "assetlibrary.synchronize"
2022-12-24 15:30:32 +01:00
bl_options = {"REGISTER", "UNDO"}
2024-05-27 17:22:45 +02:00
bl_label = 'Synchronize'
bl_description = 'Synchronize Action Lib to Local Directory'
2022-12-24 15:30:32 +01:00
name : StringProperty()
diff : StringProperty()
blocking : BoolProperty(default=False)
mode : EnumProperty(items=[(i.replace(' ', '_').upper(), i, '') for i in ('None', 'All', 'Auto Bundle')], default='NONE')
directory : StringProperty(subtype='DIR_PATH')
#conform : BoolProperty(default=False)
2022-12-24 15:30:32 +01:00
#def refresh(self):
# for area in suitable_areas(bpy.context.screen):
# bpy.ops.asset.library_refresh({"area": area, 'region': area.regions[3]})
#space_data.activate_asset_by_id(asset, deferred=deferred)
2024-05-27 17:22:45 +02:00
def execute(self, context):
2022-12-24 15:30:32 +01:00
prefs = get_addon_prefs()
libs = []
if self.name:
libs += [prefs.libraries[self.name]]
if self.mode == 'ALL':
libs += prefs.libraries.values()
elif self.mode == 'AUTO_BUNDLE':
2022-12-25 02:54:50 +01:00
libs += [l for l in prefs.libraries if l.auto_bundle]
if not libs:
return {"CANCELLED"}
2022-12-24 15:30:32 +01:00
lib_datas = [l.to_dict() for l in libs]
print(f'Bundle Libraries: {[l.name for l in libs]}')
script_code = dedent(f"""
import bpy
prefs = bpy.context.preferences.addons["asset_library"].preferences
for lib_data in {lib_datas}:
lib = prefs.env_libraries.add()
lib.set_dict(lib_data)
2024-05-27 17:22:45 +02:00
lib.plugin.bundle(cache_diff='{self.diff}')
2022-12-25 02:54:50 +01:00
bpy.ops.wm.quit_blender()
2022-12-24 15:30:32 +01:00
""")
2022-12-25 02:54:50 +01:00
script_path = Path(bpy.app.tempdir) / 'bundle_library.py'
2022-12-24 15:30:32 +01:00
script_path.write_text(script_code)
2022-12-25 02:54:50 +01:00
print(script_code)
2022-12-24 15:30:32 +01:00
#raise Exception()
cmd = get_bl_cmd(script=str(script_path), background=True)
#print(cmd)
if self.blocking:
subprocess.call(cmd)
bpy.app.timers.register(refresh_asset_browsers, first_interval=0.2)
else:
subprocess.Popen(cmd)
return {'FINISHED'}
classes = (
2023-01-17 18:05:22 +01:00
ASSETLIB_OT_reload_addon,
2024-05-27 17:22:45 +02:00
ASSETLIB_OT_add_library,
ASSETLIB_OT_remove_library,
ASSETLIB_OT_synchronize
2022-12-24 15:30:32 +01:00
)
2024-05-27 17:22:45 +02:00
def register():
2022-12-24 15:30:32 +01:00
for cls in classes:
bpy.utils.register_class(cls)
2024-05-27 17:22:45 +02:00
2022-12-24 15:30:32 +01:00
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)