3
$\begingroup$

I wouldn't say I'm a new developer but certainly still amateurish in the Blender API ways. Basically, I'm kinda tired of passing context to every single class and function XD

Examples:

  • Can I use bpy.context.scene anywhere without issues?
  • Will bpy.context.selected_objects always return the selected objs in the 3DView regardless of where the operator was run?

Is it ever ok to use bpy.context instead of operator context?

I'm guessing there are situations where you get lucky and the two just happen to align, but I assume it's bad practice to rely on that.

...should I just get used to passing the operator's context everywhere because its safer?

$\endgroup$
3
  • 3
    $\begingroup$ My experience is they are one and the same, ie the context argument and bpy.context For Blender 2.5 the choice was made to use an operator / context paradigm. The usual suspect methods for an operator execute, invoke, modal, draw, check take 1 or 2 non self arguments, no getting around that, and using bpy.context where the context argument is passed is IMO "Poor Form". We can override any or all context members by passing a an override dictionary to an operator. As it currently stands AFAIK there is no way to distinguish between overridden context and actual context $\endgroup$
    – batFINGER
    May 9, 2021 at 10:59
  • 1
    $\begingroup$ However ( and speculatively) If a change was made such that overridden context was reflected in operators context argument but actual context was still bpy.context it would then be crucial to use the right one. Often this is more of an issue in handlers where scene is an argument (the scene being handled), or update methods where self is the scene, but bpy.context.scene is used instead and works as expected,,, until it doesn't. $\endgroup$
    – batFINGER
    May 9, 2021 at 11:04
  • $\begingroup$ Thanks for all the information. So the main issue would be losing the ability to override the operator's context. I started reading on that and how you can override 'selected_objects' (for example) with your own list of objects. Very helpful and interesting stuff. Thanks again for your time. $\endgroup$ May 10, 2021 at 18:01

1 Answer 1

1
+50
$\begingroup$

Can I use bpy.context.scene anywhere without issues?

Yes, you can always use it, as the blender scenes are practically never lower than 1. So bpy.context.scene, will always refer to the current scene

But be careful, if for example you do something like assigning this to a variable scene = bpy.context.scene, the scene may at some point, no longer exist, as the user could create new ones, and delete that scene from the bpy.data.scenes

Will bpy.context.selected_objects always return the selected objs in the 3DView regardless of where the operator was run?

Yes, this always returns the selected objects in a list, so to check for yourself, you can run bpy.context.selected_objects in the Blender console, closing any window with the 3D view, you will notice that it will always refer to the current scene, and consequently to selected objects in the scene. In addition, by returning a list of objects, you can check if the list contains any objects with if bpy.context.selected_objects

The operator in general already contains the context, in principle if known, within an operator, for example in the function execute(self, context), the context is already present together with self (self is itself, i.e. the operator class).

The context can also be assigned at the beginning of a script, as happens in the Blender console: C = bpy.context as is also the case with D = bpy.data

$\endgroup$
1
  • $\begingroup$ Thank you for the explanation. This and batFinger's coments on the original post gave me a better grasp of how context works now. $\endgroup$ May 10, 2021 at 18:03

You must log in to answer this question.

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