Skip to content

Commit

Permalink
Allow number substitution in the entire string
Browse files Browse the repository at this point in the history
  • Loading branch information
p2or authored Jul 11, 2021
1 parent 21c10ef commit 2f071e4
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions viewport-rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"name": "Viewport Rename",
"author": "Christian Brinkmann (p2or)",
"description": "Rename, find and select Objects directly in the Viewport",
"version": (0, 7),
"version": (0, 8),
"blender" : (2, 81, 0),
"location": "3D View > Ctrl+R",
"warning": "", # used for warning icon and text in addons panel
Expand All @@ -44,10 +44,11 @@ class VIEW3D_OT_viewport_rename(bpy.types.Operator):
bl_options = {'REGISTER', 'UNDO'}
bl_property = "new_name"

new_name : bpy.props.StringProperty(name="New Name")
substitute : bpy.props.StringProperty(name="Replace")
data_flag : bpy.props.BoolProperty(name="Rename Data-Block", default=False)
mode : bpy.props.EnumProperty(name="Mode", description="Set the Mode",
new_name: bpy.props.StringProperty(name="New Name")
start: bpy.props.IntProperty(name="Start", default=1)
substitute: bpy.props.StringProperty(name="Replace")
data_flag: bpy.props.BoolProperty(name="Rename Data-Block", default=False)
mode: bpy.props.EnumProperty(name="Mode", description="Set the Mode",
items = [('RENAME', "Rename", ""),
('RESEARCH', "Search & Replace", ""),
('SEARCH', "Search & Select", "")])
Expand All @@ -67,21 +68,22 @@ def execute(self, context):

if self.mode == 'RENAME':
reverse = False
if user_input.endswith("#r"):
if "#r" in user_input:
reverse = True
user_input = user_input[:-1]

suff = re.findall("#+$", user_input)
if user_input and suff:
number = ('%0'+str(len(suff[0]))+'d', len(suff[0]))
real_name = re.sub("#", '', user_input)
user_input = user_input.replace("#r", "#")

if "#" in user_input:
objs = context.selected_objects[::-1] if reverse else context.selected_objects
names_before = [n.name for n in objs]
for c, o in enumerate(objs, start=1):
o.name = (real_name + (number[0] % c))
hashes = user_input.count("#")

for c, o in enumerate(objs, start=self.start):
number = "{n:0{digits}d}".format(n=c, digits=hashes)
o.name = user_input.replace("#"*hashes, number)

if self.data_flag and o.data is not None:
o.data.name = (real_name + (number[0] % c))
o.data.name = o.name

self.report({'INFO'}, "Renamed {}".format(", ".join(names_before)))
return {'FINISHED'}

Expand Down Expand Up @@ -151,28 +153,35 @@ def invoke(self, context, event):
self.new_name = context.active_object.name
return context.window_manager.invoke_props_dialog(self, width=450)

def check(self, context):
return True

def draw(self, context):
txt_name = "New Name" if self.mode == "RENAME" else "Search for"
txt_data = "Rename Data-Block" if self.mode != "SEARCH" else "Find Data-Block"

layout = self.layout
layout.row()
layout.prop(self, "mode", expand=True)
layout.row()
layout.prop(self, "new_name", text=txt_name)
layout.row().prop(self, "mode", expand=True)

spl = layout.split(factor=.75, align=True)
spl.prop(self, "new_name", text=txt_name)
col = spl.column(align=True)
col.prop(self, "start", text="Start at:")
col.active = "#" in self.new_name

if self.mode == "RESEARCH":
rep = layout.row()
rep.prop(self, "substitute", text="Replace with")

layout.row()
layout.prop(self, "data_flag", text=txt_data)
layout.row()


def draw_viewport_rename_obj_menu(self, context):
layout = self.layout
layout.separator()
layout.operator(VIEW3D_OT_viewport_rename.bl_idname, text="Seek and Rename", icon='FONTPREVIEW')
layout.operator(VIEW3D_OT_viewport_rename.bl_idname, text="Viewport Rename", icon='FONTPREVIEW')


# ------------------------------------------------------------------------
Expand Down

0 comments on commit 2f071e4

Please sign in to comment.