Page MenuHome

RenameObjects_03.py

RenameObjects_03.py

import bpy
from bpy.types import Operator
class Object_OT_RenameObjects(Operator):
bl_idname = "object.renameobjects"
bl_label = "Rename Object(s)"
use_set_name: bpy.props.BoolProperty (name = "Set Name:", description = "Set Name")
set_name : bpy.props.StringProperty(name = "", description = "Base Name")
find_replace: bpy.props.BoolProperty (name = "Find / Replace",
description = "Find / Replace")
find : bpy.props.StringProperty(name = "", description = "Find this")
replace : bpy.props.StringProperty(name = "", description = "Replace with this")
clean_digs : bpy.props.BoolProperty (name = "Clean Digits",
description = "Clean Digits")
clean_first : bpy.props.IntProperty (name = "From First", min = 0,
description = "From First")
clean_last : bpy.props.IntProperty (name = "From Last", min = 0,
description = "From End")
prefixsuffix: bpy.props.BoolProperty (name = "Prefix / Suffix",
description = "Prefix / Suffix")
prefix : bpy.props.StringProperty(name = "", description = "Prefix")
suffix : bpy.props.StringProperty(name = "", description = "Suffix")
use_digits : bpy.props.BoolProperty (name = "Add Number:",
description = "Add Number")
from_no : bpy.props.IntProperty (name = "Start",min = 0,
description = "Start From number")
increment : bpy.props.IntProperty (name = "Increment",min = 1, default = 1,
description = "increment value")
digit_length: bpy.props.IntProperty (name = "Length", min = 0, max = 9, default = 3,
description = "e.g NewName_####")
items = []
def indexstr(self, count, index):
length = len(str(index))
string = ""
if length < count:
for i in range(length, count):
string += "0"
return (string + str(index))
def draw(self, ctx):
if len(self.items) > 1:
box = self.layout.box()
row = box.row(align = True)
row.prop(self, "use_set_name")
spl = row.split(factor = 2.2, align = False)
row = spl.row(align = True)
row.prop(self, "set_name")
row = box.row(align = True)
row.prop(self, "find_replace")
row.prop(self, "find")
row.prop(self, "replace")
row = box.row(align = True)
row.prop(self, "clean_digs")
row.prop(self, "clean_first")
row.prop(self, "clean_last")
row = box.row(align = True)
row.prop(self, "prefixsuffix")
row.prop(self, "prefix")
row.prop(self, "suffix")
row = box.row(align = True)
row.prop(self, "use_digits")
spl = row.split()
row = spl.row(align = True)
row.alignment = 'RIGHT'
row.prop(self, "from_no")
row.prop(self, "increment")
row.prop(self, "digit_length")
# this action replaced by
# bpy.ops.wm.call_panel(name = 'TOPBAR_PT_name', keep_open = False)
# in invoke()
#if len(self.items) == 1:
# box = self.layout.box()
# row = box.row()
# row.prop(self, "set_name")
def execute(self, context):
if len(self.items) > 1:
Index = self.from_no
for obj in self.items:
# Get Object Original Name #
NewName = obj.name
# Set the Base name #
if self.use_set_name:
NewName = self.set_name
# Remove First characters #
if self.clean_digs:
NewName = NewName[self.clean_first : self.clean_first + len(NewName)]
NewName = NewName[1 : len(NewName) - self.clean_last]
# Add Prefix and sufix to the new name #
if self.prefixsuffix:
NewName = self.prefix + NewName
NewName = NewName + self.suffix
# Add Digits to end of new name #
if self.use_digits:
NewName += self.indexstr(self.digit_length, Index)
Index += 1
# Find and Replace #
if self.find_replace:
NewName = NewName.replace(self.find, self.replace)
# Trim Left and Right
NewName = NewName.strip()
# Set the new name to the object #
obj.name = NewName
elif len(self.items) == 1:
self.items[0].name = self.set_name
return {'FINISHED'}
def invoke(self, context, event):
if bpy.context.mode == 'EDIT_ARMATURE':
self.items = bpy.context.selected_bones
if bpy.context.active_bone != None:
if bpy.context.active_bone in self.items:
self.items.remove(bpy.context.active_bone)
self.items.insert(0, bpy.context.active_bone)
else:
self.items = bpy.context.selected_objects
if bpy.context.active_object != None:
if bpy.context.active_object in self.items:
self.items.remove(bpy.context.active_object)
self.items.insert(0, bpy.context.active_object)
if len(self.items) == 1:
if bpy.context.mode == 'EDIT_ARMATURE':
if bpy.context.active_bone == None:
#TODO set self.items[0] as active bone
# when active bone is None
#bpy.context.active_bone = self.items[0]
pass
elif bpy.context.active_object == None:
bpy.context.view_layer.objects.active = self.items[0]
bpy.ops.wm.call_panel(name = 'TOPBAR_PT_name', keep_open = False)
elif len(self.items) > 1:
self.set_name = self.items[0].name
wm = context.window_manager
return wm.invoke_props_dialog(self, width = 400)
return {'FINISHED'}
def register():
bpy.utils.register_class(Object_OT_RenameObjects)
def unregister():
bpy.utils.unregister_class(Object_OT_RenameObjects)
if __name__ == "__main__":
register()

File Metadata

Mime Type
text/x-python
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
fb/12/43a6d4e9e18e239035bcdd1bc335

Event Timeline