Skip to content

Instantly share code, notes, and snippets.

@MrMagicPenguin
Created June 28, 2022 20:50
Show Gist options
  • Save MrMagicPenguin/3880fb9e4d3b9160caf2801483ea31f0 to your computer and use it in GitHub Desktop.
Save MrMagicPenguin/3880fb9e4d3b9160caf2801483ea31f0 to your computer and use it in GitHub Desktop.
UI Tool to batch rename objects within a Collection. Features field disabling, list filtered by Type
import bpy
from bpy.types import (PropertyGroup)
from bpy.props import (StringProperty, PointerProperty, CollectionProperty, BoolProperty)
C = bpy.context
D = bpy.data
class odalysProperties(PropertyGroup):
target_collection: PointerProperty(
name= "Collection",
type=(bpy.types.Collection),
)
new_name: StringProperty(
name = "New Name",
description = "Replacement name for objects in Collection.",
default="",
maxlen=1024,
)
use_custom_name: BoolProperty(
name = "Use custom name?",
description = "",
default = False,
)
# Operator
class OBJECT_OT_batch_rename(bpy.types.Operator):
bl_idname = "object.batch_rename"
bl_label = "Batch Rename"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
print("New Name: ", self.new_name)
return {'FINISHED'}
# Panel
class OBJECT_PT_batch_rename(bpy.types.Panel):
bl_idname = "OBJECT_PT_batch_rename"
bl_label = "Batch Rename"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Tool"
@classmethod
def poll(self,context):
return context.object is not None
def draw(self, context):
# Draw menu
layout = self.layout
scene = C.scene
props = scene.odalysProps
layout.prop(props, "target_collection")
layout.prop(props, "use_custom_name")
row = layout.row()
row.enabled = props.use_custom_name
row.prop(props, "new_name", text="Custom Name")
layout.separator()
layout.operator("object.batch_rename")
classes = (odalysProperties, OBJECT_OT_batch_rename, OBJECT_PT_batch_rename)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.Scene.odalysProps = PointerProperty(type=odalysProperties)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
del bpy.types.Scene.batchrename
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment