2
$\begingroup$

I am getting the error mentioned in the title:

all_objects = bpy.data.objects[:]
non_hidden = [obj for obj in all_objects if obj.hide_viewport == False]
counter = 1

# Iterate through all objects non hidden objects in the scene
for obj in non_hidden:
    bpy.ops.object.duplicate()                     
    duplicated_obj = bpy.context.active_object
    duplicated_obj.name = f"{obj.name}_{counter}"

It specifically complains about the last line.
I have some code comming after that and when i deleted the last line it gave me a new error in the same line now saying that object doesn't have attribute 'data', where the line looked like this:

for material in duplicated_obj.data.materials:

My code is only 45 lines, so if needed i can add the whole thing at the end of this question.
I don't understand what the problem could be, i would appreciate any help, thank you in advance!

$\endgroup$

1 Answer 1

1
$\begingroup$

I found the problem:

bpy.ops.object.duplicate() duplicates the selected objects not the active objects.

So, bpy.context.active_object was "None". Once I had selected the object I wanted to duplicate, it worked.

$\endgroup$
1
  • $\begingroup$ Hello ! Thanks for adding an answer to your problem. Would you mind adding the specific line of code you added to make it work to your answer ? Cheers $\endgroup$
    – Gorgious
    Nov 9, 2023 at 15:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .