Blender Rename Object

Hey:)

Thats my question :

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
‘’’

Well thats exactly what im looking for, but is doesnt work.

Can someone explain to me how this works, so what does each line do? And why dosnt it work?

Im using Blender 2.64.

Thanks!:slight_smile:

I would do it more pythonic:

objs = bpy.context.selected_editable_objects
digit_count= len(str(len(objs)))
prefix = "myname"

for i, ob in enumerate(objs, 1):
    ob.name = "%s%0*i" % (prefix, digit_count, i)

Breakdown:

objs = bpy.context.selected_editable_objects

Assign the list of selected and editable objects to variable “objs”

digit_count= len(str(len(objs)))

Calculate how many digits are used at max for the number

prefix = "myname"

Assign the string used for the name to variable “prefix”

for i, ob in enumerate(objs, 1):

Iterate over all objects in “objs” and keep track of how man we have now (start at 1)

    ob.name = "%s%0*i" % (prefix, digit_count, i)

Assign new name to object. Naming scheme: “myname” plus a zero-padded number. How many fill-zeroes are used depends on how many objects you selected (no leading zero for e.g. 5 objects, 1 for e.g. 20 as needed, up to two if you run it with at least 100 objects selected).

%i is a placeholder for an integer (we give it the counter variable i).
%0#i will zero-fill up to # positions (# = an integer, e.g. %03i for 000…999)
%0*i is a special syntax, you’ll have to pass a second integer before the counter var to define the amount of padding zeroes. We give it the digit_count value, which is the maximum number of digits we will use (example: 12 selected objects, string length is 2, thus we pad all numbers to two digits using zeroes, 01, 02, 03, … 09, 10, 11, …).

Great Answere Thank you very much!
And thanks for the good explanation, a bit complex for the beginning^^
I run the script and it worked fine the only Problem was that it was countig (1,2,3) and not (01,02,03)
Im not sure if you have expained it right at the bottom of your comment, im quite new in phyton (not in blender) and my english isnt the best as well, in from Germany :smiley:

Germany too here :wink:

Es werden so viele führende Nullen genutzt, wie die Zeichenlänge der höchsten Zahl ist (bzw. die Gesamtanzahl der Objekte, was hier gleichbedeutend ist, weil wir bei 1 anfangen zu zählen). Du brauchst also 10+ Objekte. Oder du benutzt %02i für 01, 02, 03, auch wenn es weniger sind.


It does count 01, 02, 03 if you have at least 10 objects selected. If you always want a padding zero for 1st to 9th element, use this instead:


    ob.name = "%s%02i" % (prefix, i)

Ok danke jetzt hab ich es verstanden :slight_smile: Scheinst dich ja gut damit auszukennen :wink:
Wäre cool wenn du mich auf Skype adden könntest, kenn nämlich kaum Deutsche die sich richtig mit Blender auskennen :smiley:
Skype ID: USPproductionzHD