2
$\begingroup$

i want to know how to create adaptive suffix, that are "content aware" of the situation, to avoid erasing object that have the same names. i think that this is a classic use of python, could someone explain or give me a link to a explanation/ tutorial that can help me understand how this adaptive renaming system work ?

for example in this code below im automatically creating a save as copy in the directory file.

import bpy
import os

try:
    os.mkdir(os.path.dirname(bpy.data.filepath)+"\\save as copy\\")
except: 
    print('directory already created')

Dir = os.path.dirname(bpy.data.filepath) + "\\save as copy\\"

bpy.ops.wm.save_as_mainfile(copy=True, filepath= Dir +"Copy.blend")

the only problem is that it erase the previous .blend everytime i do the action, id much prefer to implement an adaptive suffix, that create "copy 001" if copy already exist; "copy 002" if 001 already exist... i could do that with condition but i dont know how to reproduce the steps indefinitely.

in a precedent code someone show me a way to do this ? i dont know if its specific to the situation or not, but i didnt understand how to reproduce thoses act for my situation:

import bpy

context = bpy.context
scene = context.scene
A = context.object

i = 1
pref = "%s Mirror " % A.name

name = "%s%d" % (pref, i)
while A.modifiers.get(name):    
    i += 1
    name = "%s%d" % (pref, i)


bpy.ops.object.modifier_add(type='MIRROR')
A.modifiers[len(A.modifiers)-1].name = name
bpy.ops.object.empty_add(type='CUBE')
bpy.ops.transform.resize(value=(0.01, 1, 1), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='ENABLED', proportional_edit_falloff='SMOOTH', proportional_size=0.00439353)
E = bpy.context.selected_objects[0]
E.name = name
A.modifiers[name].mirror_object = E
A.select_set(state=True)

i tried

import bpy

i = 1
pref = "neww"
name = "%s%d" % (pref, i)
while bpy.context.object.name.get(name): 
    i += 1
    name = "%s%d" % (pref, i)

for ob in bpy.context.selected_objects:
    ob.name = name

i also tried

import bpy

i = 1
pref = "new"
name = "%s%d" % (pref, i)

while ob.name = name
    i += 1
    name = "%s%d" % (pref, i)

for ob in bpy.context.selected_objects:
    ob.name = name

dont work

$\endgroup$
2
  • $\begingroup$ Related blender.stackexchange.com/a/125098/15543 $\endgroup$
    – batFINGER
    Apr 6 '19 at 14:12
  • $\begingroup$ didnt get it, already tried to understand, for what i did undestand this was a specific renaming action that occure during the modifier process in a while loop, i dont see how i can apply this in this case, and how the i+=1 could be re-used at for any batch renaming $\endgroup$
    – DB3D
    Apr 6 '19 at 14:36
1
$\begingroup$

found it

import bpy

i = 0 #default number
pref = "new" #name
name = "%s%d" % (pref, i)

for ob in bpy.context.selected_objects: #for every object in selection
    i += 1 #add i+1
    name = "%s%d" % (pref, i) #redefinie name
    ob.name = name

if i wanted to just keep the same name, i need to replace pref by

pref = "%s" % bpy.context.object.name

if i want to make the prefix into a suffix or add _ - ect i just need to change

name = "%s%d" % (pref, i)
to
name = "%s_%d" % (pref, i)

basically its simple, for each object in my selection, i add +1 to i = 1 that's it

there is also a special python function that transform the number into numbers like 001 002 003 010.. ect... ii only work with strings so you need to convert the number into a string first:

newnumberfilledwithzeros =str(10).zfill(3) #will be = to 010

with all thoses examples, here what i came with this example below, and witouth the " "%s" method, its actually not necessary for this small exercise.

name="hello"
#if you want to keep the same name and just add the suffix ? use this line below instead
#name= bpy.context.selected_object.name
start=0 #could start at -1 if you want the first obj to be named 000
zeros=3 #3x= numbers like 001,019... 4x = 0012,0003... ...
for ob in bpy.context.selected_objects:
    start+=1
    f=str(start).zfill(zeros)
    ob.name = name + f
    #result will be "hello001" "hello002" "hello003" ect..
    #want to be "hello_001" ? just to this below 
    #ob.name = name + "_" + f
    #want to do a prefix ? 
    #ob.name = f + "_" + name
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.