So what I want basically is as follows:-
Basically I have two objects with different names. So lets say I select both of the Objects named "First" and "Second". So I make one of them named (First) as active selection. I want a script or addon which can rename the non-active object(named- Second) have the name of the first one but with a changeable suffix like say "_HP". So after applying the script I will have something like this.
Don't mind the annotation/painted names those are so u can easily see which object is named which. Thank you!
-
$\begingroup$ What if there are 3 or more objects in the scene? Like a light or camera? Or would that never happen? $\endgroup$– james_tCommented Jan 2, 2022 at 17:25
-
$\begingroup$ Nah mostly I use max two objects:- One is high poly and other one is Low poly, if there will be a third I will try to rename it manually. $\endgroup$– NamanDeepCommented Jan 3, 2022 at 12:18
2 Answers
You can use bpy.context.active_object
and bpy.context.selected_objects
. The first tells you the object that is active — the one you want to use as the source of the name. The second one tells you all of the selected objects, including the selected objects. The simplest script to do what you want in this case is
import bpy
name = bpy.context.active_object.name
suffix = "_toy"
for object in bpy.context.selected_objects:
if not object.name == name:
object.name = bpy.context.active_object.name + suffix
break
You can enter this in the text editor, change the suffix string to whatever you like, and use the run key to run it.
It does have a bug, though. If you have selected multiple objects, it will rename the first one in the list, and it might not be the one you want.
It's also inconvenient, in that you have to edit it every time you change the prefix.
You could turn it into an add-on with an object mode panel that you could use to set the suffix, of course:
bl_info = {
"name" : "renamer",
"description" : "rename the selected object",
"author" : "Marty",
"version" : (0, 0, 1),
"blender" : (2, 80, 0),
"location" : "View3D",
"warning" : "",
"support" : "COMMUNITY",
"doc_url" : "",
"category" : "3D View"
}
import bpy
from bpy.types import Operator
from bpy.props import StringProperty
from bpy.types import Panel
class RENAMER_OT_Message(Operator):
""" Renamer operator
Renames the other selected object to the same name as
the active object, with a suffix added.
"""
bl_idname = "renamer.rename"
bl_label = "rename"
bl_description = "rename the selected object"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return context.mode == "OBJECT"
def execute(self, context):
if not context.active_object:
self.report({'ERROR'}, "No active object")
return {'CANCELLED'}
if len(context.selected_objects) != 2:
self.report({'ERROR'}, "You must select two objects")
return {'CANCELLED'}
name = context.active_object.name
for object in context.selected_objects:
if not object.name == name:
object.name = context.active_object.name + context.object.suffix
break
return {'FINISHED'}
class RENAMER_PT_Panel(Panel):
""" Renamer panel
A panel to display the renamer operator
"""
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "renamer"
bl_idname = "renamer.panel"
bl_label = "rename selected"
def draw(self, context):
col = self.layout.column()
col.prop(context.object, "suffix", text = "Suffix")
col.operator("renamer.rename", text="rename")
classes = [
RENAMER_OT_Message,
RENAMER_PT_Panel,
]
def register():
for c in classes:
bpy.utils.register_class(c)
bpy.types.Object.suffix= bpy.props.StringProperty(
name="suffix",
description="Suffix to add to name",
default="_HP",
)
def unregister():
for c in classes:
bpy.utils.unregister_class(c)
del bpy.types.Object.suffix
if __name__ == '__main__':
register()
-
$\begingroup$ Oh wow, really that is almost what I wanted. Can I have one more change in this plz, Can you also make an another parameter, so the Active object can also have a suffix like "_LP". So after the rename script it will make two object : "First_LP"(active) and "First_HP"., $\endgroup$ Commented Jan 3, 2022 at 4:06
-
$\begingroup$ Thank you btw, I really appreciate you make a script and an addon too. $\endgroup$ Commented Jan 3, 2022 at 5:16
-
$\begingroup$ @NamanDeep Since you're apparently a new user to Blender StackExchange, you might want to read What should I do when someone answers my question? and perhaps accept my answer. $\endgroup$ Commented Jan 3, 2022 at 14:47
-
$\begingroup$ Your change request involves a lot more code than is reasonable for a StackExchange answer. Using what I provided you should be able to make the changes to add the second parameter. It's a good way to learn, and StackExchange is meant to teach you how to do things, not do them for you. $\endgroup$ Commented Jan 3, 2022 at 14:53
-
1$\begingroup$ Yeh, I actually did it, making some duplicate of the parameters and using the active object parameters to change its value and using that in execute funciton,. Thanks tho, I am still new to blender scripting, Although I have coding background but not experienced with blender's scripting, Thanks again $\endgroup$ Commented Jan 3, 2022 at 16:17
Here is one trivial script that will rename per your (incomplete?) spec, where I've allowed for non-mesh other items (light, camera):
import bpy
suffix='HP'
if bpy.context.object is None:
print('you need to select one object')
name_base=bpy.context.object.name
for obj in bpy.data.objects:
if (obj is bpy.context.object) or (obj.type != 'MESH'):
continue # skip selected
obj.name=name_base+'_'+suffix
break
The 'for all objects' loop breaks after renaming the first unselected object in the scene. Or one could have name objects, and use an incrementing index like
suffixIdx=0
.
.
.
obj.name=name_base+'_'+ suffixIdx
suffixIdx += 1
# break # remove the break statement
or gussie that up by formatting the suffixIdx so that the suffix is a three digit number preceeded by zeros like obj.name=name_base+'_'+suffix+'.{:03d}'.format(suffixIdx)
-
$\begingroup$ Thank you, really appreciate the help mate. :D Although Marty Fouts gave me exactly what I want. your solution is really good too. :) $\endgroup$ Commented Jan 3, 2022 at 4:14