1
$\begingroup$

I'm a little confused about the structure of bpy.

Take bpy.ops.object.some_operation(), it seams to work on the selected object. Or is it the active object?

How about bpy.context.active_object, is that the same? Or what about bpy.data.collections['Collection'].all_objects or bpy.context.scene.collection.all_objects or bpy.context.scene.objects?

How do they exactly relate to objects in Blender?

I can't really find a clear description anywhere and it leads to my code sometime missfiring, even though seemed it has worked for me previously.

I know it's a noob question but I would really appreciate any clarification on the matter.

$\endgroup$
3
$\begingroup$

A try to give a very short overview:

enter image description here

The principle is globally the same for all "object" types depending on their nature.

Above that, give a look at the documentation (probably you've already), which is very structured, module by module and allows to navigate through all data and ops hierarchies.

$\endgroup$
4
  • $\begingroup$ Thanks a lot. It just very hard to do automated scripting. Sometimes you can acces the object directly. Other times you have to select or activate the opject and then perform an operation (supposedly on the selected (or activated?) object. I get all kinds of weird and unpredictable behavior. $\endgroup$ – DrDress Nov 12 '20 at 12:21
  • $\begingroup$ Try to use class methods as possible. Sometimes bpy.ops is needed, but always painful to set the context for it (and it is slower). But that may depend on which parts you want to work on. $\endgroup$ – lemon Nov 12 '20 at 12:23
  • $\begingroup$ I want to add a modifier. But may I can create it in python and then append it to the modifier list og the object? $\endgroup$ – DrDress Nov 12 '20 at 13:06
  • $\begingroup$ An example here blender.stackexchange.com/a/201670/19156 and the doc docs.blender.org/api/current/… $\endgroup$ – lemon Nov 12 '20 at 13:19

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.