BallanceBlenderHelper/bbp_ng/OP_OBJECT_naming_convention.py

107 lines
4.3 KiB
Python
Raw Normal View History

2023-12-31 17:35:15 +08:00
import bpy
import typing
from . import UTIL_naming_convension, UTIL_functions, UTIL_icons_manager
class BBP_OT_regulate_objects_name(bpy.types.Operator):
"""Regulate Objects Name by Virtools Group and Naming Convention"""
bl_idname = "bbp.regulate_objects_name"
bl_label = "Regulate Objects Name"
bl_options = {'UNDO'}
bl_translation_context = 'BBP_OT_regulate_objects_name'
2023-12-31 17:35:15 +08:00
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_confirm(self, event)
def execute(self, context):
_rename_core(
UTIL_naming_convension.VirtoolsGroupConvention.parse_from_object,
UTIL_naming_convension.YYCToolchainConvention.set_to_object
)
return {'FINISHED'}
class BBP_OT_auto_grouping(bpy.types.Operator):
"""Auto Grouping Objects by Its Name and Name Convention"""
bl_idname = "bbp.auto_grouping"
bl_label = "Auto Grouping"
bl_options = {'UNDO'}
bl_translation_context = 'BBP_OT_auto_grouping'
2023-12-31 17:35:15 +08:00
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_confirm(self, event)
def execute(self, context):
_rename_core(
UTIL_naming_convension.YYCToolchainConvention.parse_from_object,
UTIL_naming_convension.VirtoolsGroupConvention.set_to_object
)
return {'FINISHED'}
class BBP_OT_convert_to_imengyu(bpy.types.Operator):
"""Convert Objects Name from YYC Convention to Imengyu Convention."""
bl_idname = "bbp.convert_to_imengyu"
bl_label = "Convert to Imengyu"
bl_options = {'UNDO'}
bl_translation_context = 'BBP_OT_convert_to_imengyu'
2023-12-31 17:35:15 +08:00
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_confirm(self, event)
def execute(self, context):
_rename_core(
UTIL_naming_convension.YYCToolchainConvention.parse_from_object,
UTIL_naming_convension.ImengyuConvention.set_to_object
)
return {'FINISHED'}
def _rename_core(
fct_get_info: typing.Callable[[bpy.types.Object, UTIL_naming_convension.RenameErrorReporter], UTIL_naming_convension.BallanceObjectInfo | None],
ftc_set_info: typing.Callable[[bpy.types.Object, UTIL_naming_convension.BallanceObjectInfo, UTIL_naming_convension.RenameErrorReporter], bool]
) -> None:
# get selected objects. allow nested collection
selected_objects: typing.Iterable[bpy.types.Object] = bpy.context.view_layer.active_layer_collection.collection.all_objects
# create reporter
with UTIL_naming_convension.RenameErrorReporter() as reporter:
# iterate objects
for obj in selected_objects:
reporter.enter_object(obj)
# try get info
info: UTIL_naming_convension.BallanceObjectInfo | None = fct_get_info(obj, reporter)
if info is not None:
# if info is valid, try assign it
if not ftc_set_info(obj, info, reporter):
reporter.add_error('Fail to set info to object.')
else:
reporter.add_error('Fail to get info from object.')
# end obj process
reporter.leave_object(obj)
# report data
tr_text_1: str = bpy.app.translations.pgettext_rpt('View console to get more detail', 'BBP/OP_OBJECT_naming_convention._rename_core()')
tr_text_2: str = bpy.app.translations.pgettext_rpt('All: {0}', 'BBP/OP_OBJECT_naming_convention._rename_core()')
tr_text_3: str = bpy.app.translations.pgettext_rpt('Failed: {0}', 'BBP/OP_OBJECT_naming_convention._rename_core()')
2023-12-31 17:35:15 +08:00
UTIL_functions.message_box(
(
tr_text_1,
tr_text_2.format(reporter.get_all_objs_count()),
tr_text_3.format(reporter.get_failed_objs_count())
2023-12-31 17:35:15 +08:00
),
'Rename System Report',
UTIL_icons_manager.BlenderPresetIcons.Info.value
)
def register() -> None:
2023-12-31 17:35:15 +08:00
bpy.utils.register_class(BBP_OT_regulate_objects_name)
bpy.utils.register_class(BBP_OT_auto_grouping)
bpy.utils.register_class(BBP_OT_convert_to_imengyu)
def unregister() -> None:
2023-12-31 17:35:15 +08:00
bpy.utils.unregister_class(BBP_OT_convert_to_imengyu)
bpy.utils.unregister_class(BBP_OT_auto_grouping)
bpy.utils.unregister_class(BBP_OT_regulate_objects_name)