renaming objects

Hi,

I’m experimenting with python in 2.49 and trying to rename an object. I have something like

print "The name of the object is: ",ob.getName()
>>> MyCone

but I want to rename it to … :confused:… say ‘Conical’, is that possible?

Thanks,

John

The following changes the name of 1 object to “Conical” or ALL selestec objects to the same name with numbers at the end:

def Ren_current_object(st):
    sce = Scene.GetCurrent()
    ob = Object.GetSelected()[0]
    ob.name = st
    return

def Ren_sel_objects(st):
    sce = Scene.GetCurrent()
    sel_objects = Object.GetSelected()
    for i in range(len(sel_objects)):
        ob = sel_objects[i]
        ob.name = st+"_"+str(i)
    return

editmode = Window.EditMode()
Window.EditMode(0)
st = "Conical"
Ren_current_object(st)
#Ren_sel_objects(st)
Window.EditMode(editmode)
Window.RedrawAll()

If you need to use the multi-renaming version, you need to uncomment it first, i.e. remove the # before Ren_sel_objects(st).

Both procs suppose there is NO such name (or names) prior to applying the new names. Otherwise, you will get something like “Conical.001”. In such a case you will need to (1) identify there is already an object with such a name; (2) decide what to do in this situation, i.e. remove that name to something else, for example; (3) apply your new name.

Regards;

Why not using Blender 2.53???
There you code like this.
obj = … (eg. bpy.context.active_object)
obj.name = “new name”
The name of an object is read/writable :wink:

Can you explain that better for 2.53?

Question to ???

If you go to the Scripting possibility and look there at the Python -console.
Type e.g.
bpy.data.objects.values()

if you save this as a list e.g.:
allobjs = bpy.data.objects.values()
you can access each, look for its name and change the name to your wishes
or if you know allready the name (blabla eg.) you work e.g. like this:
myobj = bpy.data.objects[“blabla”]
myobj.name = “not anymore”
and your object has now the name notanymore

In a script you use commands like the shown ones …

Thanks PKHG, that’s really awesome :slight_smile: I don’t know very much about Python, but how about if I don’t want to care about the already existing names of objects. Just wanting to rename (in 2.5x) whatever is selected by order. But I don’t know where to go from here:

import bpy
bpy.ops.object.select_all
allobjs = bpy.data.objects.values()
allobjs.name = “something”

I know the steps, but I have trouble translating that to code:

  1. Know what objects the user has selected.
  2. Get their values
  3. Change their values to an established name plus a number

You mean something like the example Abidos gave (for 2.49)?

If I understand correct:
The user has selected several objects and want to give them a name|
myname01, myname02 … myname14 (if 14 objects were seltected with whatever name?)
And that by using a script with name = “myname” the rest automatically?


import bpy
def renameSeletedToNumbered(name):
    counter = 0
    ao = bpy.data.objects
    for el in ao:
        if el.select:
            print("name now is ",el.name)
            counter +=1
            el.name = name + str(counter)
            print("new name is", el.name)
            
renameSeletedToNumbered('aaa')            
 
''' 
name now is  Cube
new name is aaa1
name now is  Curve
new name is aaa2
name now is  Monkey
new name is aaa3
'''

That’s amazing! Thank you :smiley: