0
$\begingroup$

I want to add a custom attribute to each model, so I went through the selected models and added a custom attribute with the name "Cube" and the content as the model name to each model.

However, after executing the code, the program only added a custom attribute to the last model because I searched online for a long time but couldn't find any relevant content, And Blender's API related to custom attributes is a bit difficult for me as a Python beginner to understand, so I am asking a question here, hoping to get a recovery.

enter image description here

enter image description here

$\endgroup$
4
  • $\begingroup$ bpy.context.object["Cube"] change to i["Cube"] $\endgroup$
    – X Y
    Commented Nov 7, 2023 at 4:46
  • $\begingroup$ bpy.context.object represents the active object, so it do with one object. $\endgroup$
    – X Y
    Commented Nov 7, 2023 at 4:46
  • $\begingroup$ @XY Oh, thank you very much for the answer. I understand. I thought bpy.context.object was written according to API regulations $\endgroup$
    – MA__AM
    Commented Nov 7, 2023 at 5:14
  • $\begingroup$ Would you kindly elaborate on what you mean by "API regulations" ? $\endgroup$
    – Gorgious
    Commented Nov 7, 2023 at 8:14

1 Answer 1

0
$\begingroup$

More idiomatic IMO

import bpy

for i, obj in enumerate(bpy.context.selectable_objects):
    obj.name = f"Cube{i}"

Use enumerate for an incremental integer variable.

$\endgroup$

You must log in to answer this question.

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