[Blender_Kitsu] Add Operators to cleanup Animation Files #38
@ -233,6 +233,99 @@ class KITSU_OT_anim_check_action_names(bpy.types.Operator):
|
||||
row.label(text="", icon="FORWARD")
|
||||
row.label(text=name)
|
||||
|
||||
class KITSU_OT_anim_enforce_naming_convention(bpy.types.Operator):
|
||||
bl_idname = "kitsu.anim_enforce_naming_convention"
|
||||
bl_label = "Set Name Conventions"
|
||||
bl_options = {"REGISTER", "UNDO"}
|
||||
bl_description = ("Fix Naming of Scene, Output Collection, Actions, and optionally F`ind and Replace`.")
|
||||
|
||||
remove_str: bpy.props.StringProperty(name="Find and Replace", default="",)
|
||||
rename_scene: bpy.props.BoolProperty(name="Rename Scene", default=True)
|
||||
rename_output_col: bpy.props.BoolProperty(name="Rename Output Collection", default=True)
|
||||
rename_actions: bpy.props.BoolProperty(name="Rename Actions", default=True)
|
||||
find_replace: bpy.props.BoolProperty(name="Find and Replace", default=False)
|
||||
|
||||
|
||||
def invoke(self, context, event):
|
||||
return context.window_manager.invoke_props_dialog(self, width=500)
|
||||
|
||||
def draw(self, context):
|
||||
layout = self.layout
|
||||
layout.prop(self, "rename_scene")
|
||||
layout.prop(self, "rename_actions")
|
||||
layout.prop(self, "rename_output_col")
|
||||
layout.prop(self, "find_replace")
|
||||
if self.find_replace:
|
||||
layout.prop(self, "remove_str")
|
||||
|
||||
def rename_datablock(self, data_block, replace:str):
|
||||
if data_block is None:
|
||||
return
|
||||
if replace in data_block.name:
|
||||
data_block.name = data_block.name.replace(replace,"")
|
||||
return (data_block.name)
|
||||
|
||||
def get_version_from_string(self, string:str):
|
||||
try:
|
||||
numbers = string.split(
|
||||
".v")[-1]
|
||||
if ".001" in numbers:
|
||||
numbers = int(numbers.replace(".001", ""))+1
|
||||
int_numbers = int(numbers)
|
||||
return str(int_numbers)
|
||||
except AttributeError:
|
||||
return "001"
|
||||
|
||||
def execute(self, context:bpy.types.Context):
|
||||
shot_base_name = bpy.path.basename(bpy.data.filepath).replace(".anim.blend","")
|
||||
active_shot = cache.shot_active_get()
|
||||
addon_prefs = bpy.context.preferences.addons["blender_kitsu"].preferences
|
||||
scene_col = context.scene.collection
|
||||
anim_suffix = "anim.output"
|
||||
|
||||
if self.find_replace:
|
||||
for col in scene_col.children_recursive:
|
||||
self.rename_datablock(col, self.remove_str)
|
||||
for obj in col.objects:
|
||||
self.rename_datablock(obj, self.remove_str)
|
||||
self.rename_datablock(obj.data, self.remove_str)
|
||||
|
||||
if self.rename_output_col:
|
||||
output_cols = [col for col in context.scene.collection.children_recursive if anim_suffix in col.name]
|
||||
if len(output_cols) != 1:
|
||||
self.report(
|
||||
{"INFO"},
|
||||
f"Animation Output Collection could not be found",
|
||||
)
|
||||
|
||||
return {"CANCELLED"}
|
||||
output_col = output_cols[0]
|
||||
output_col.name = f"{shot_base_name}.{anim_suffix}"
|
||||
|
||||
# Rename Scene
|
||||
if self.rename_scene:
|
||||
context.scene.name = f"{shot_base_name}.anim"
|
||||
|
||||
# Rename Actions
|
||||
if self.rename_actions:
|
||||
for obj in [obj for obj in bpy.data.objects if obj.type == "ARMATURE"]:
|
||||
base_name = obj.name.split(
|
||||
addon_prefs.shot_builder_armature_prefix)[-1]
|
||||
# Cerate Action if None Exists
|
||||
if obj.animation_data is None or obj.animation_data.action is None:
|
||||
new_action = bpy.data.actions.new(
|
||||
f"{addon_prefs.shot_builder_action_prefix}{base_name}.{active_shot.name}.v001")
|
||||
new_action.use_fake_user = True
|
||||
obj.animation_data_create()
|
||||
obj.animation_data.action = new_action
|
||||
obj.animation_data.action.name = f"{addon_prefs.shot_builder_action_prefix}{base_name}.{active_shot.name}.v001"
|
||||
bpy.ops.kitsu.anim_check_action_names()
|
||||
self.report(
|
||||
{"INFO"},
|
||||
f"Naming Conventions Enforced",
|
||||
)
|
||||
return {"FINISHED"}
|
||||
|
||||
|
||||
class KITSU_OT_anim_update_output_coll(bpy.types.Operator):
|
||||
bl_idname = "kitsu.anim_update_output_coll"
|
||||
@ -295,6 +388,7 @@ classes = [
|
||||
KITSU_OT_anim_quick_duplicate,
|
||||
KITSU_OT_anim_check_action_names,
|
||||
KITSU_OT_anim_update_output_coll,
|
||||
KITSU_OT_anim_enforce_naming_convention,
|
||||
]
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user