1
$\begingroup$

I'm trying to duplicate an object and add a _low to the duplicated object and _high to the one it was duplicated from. For example, if I had "cylinder" I want the duplicated object moved to the side to be "cylinder_low" and the one it was being duplicated from to be "cylinder_high"

import bpy
for obj in bpy.context.selected_objects:
    suffixL = "_low"
    suffixH = "_high"
    act_obj = bpy.context.view_layer.objects.active
    
    bpy.ops.object.duplicate(linked=False, mode='TRANSLATION')
    obj.location[1] += 5
    
    if suffixL not in act_obj.name:
        act_obj.name = act_obj.name + suffixL
        
    
        
    elif suffixH not in act_obj.name:
        act_obj.name = act_object.name + suffixH

enter image description here

enter image description here

This is what I have but when I run it it works, but the original one is named "Cylinder.001" instead of with the suffix "_high".

$\endgroup$
3
$\begingroup$

For question code above put act_obj = context.active_object after the duplicate operator call. Then it will be the duplicate of obj. if only one object is selected and active.

However also at play is the duplicate operator duplicates all selected objects. Try running with more than a single object selected.

You are only renaming the original obj if and when it is act_obj

However possibly much easier to use API calls and keep a ref to the newly created object and rename both based on original base name.

import bpy

context = bpy.context

for ob in context.selected_objects:
    name = ob.name
    copy = ob.copy()
    copy.data = copy.data.copy() # linked = False
    ob.name = f"{name}_high"
    copy.name = f"{name}_low"
    ob.location.x += 5
    context.collection.objects.link(copy)
$\endgroup$
1
  • $\begingroup$ Awesome! Thanks for this, this works exactly how I wanted it to $\endgroup$ Oct 23 '20 at 19:52

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.