There is a reoccurring problem with using Blender operators from Python API and using Context.temp_override - I feel like everyone is doing the same guesswork and it would be great if it was somehow documented.
I tried to report it as a bug to Blender documentation (#137210) but it was expectedly closed as I guess it’s not a documentation bug, but a documentation issue. So I’m posting it now here as it’s probably the best place to discuss Python API.
Issue description from the bug report:
There is a reoccurring issue for every Blender Python API user - you try to use default Blender operators as they are very efficient but you never know what
Contextattributes totemp_overrideto make it work (object/active_object/selected_objects/selected_editable_objects) and whether it’s possible at all. Typically user would try to provide all attributes they can possibly guess and if doesn’t work, the solution is to go and check C++ source and identify what context members are used.And since Blender doesn’t have a requirement to make all operators context overridable from API, so it’s never a bug. Missing context override support may be wasn’t implemented as it wasn’t needed or supporting it may not be trivial or it could be part of design (e.g. code requires accessing selected objects from view layer explicitly). So theoretically any operator user meets may not support
temp_overrideat all and require manual context manipulation.Could there be a general solution to this?
E.g. I just neededobject.make_single_useroperator. I’ve investigated the source code and it doesn’t useContextbut instead is usingFOREACH_OBJECT_FLAG_BEGIN (scene, view_layer, v3d, flag, ob)- so checking selected objects on view layer explicitly.Is there a place in docs where such thing could be documented? So this kind of information could slowly accumulate by the community contributions and then every Python API user (and myself in the future) wouldn’t need to go and recheck it again themselves.
As I see it, there’s probably no way to fit it in in the current Blender opertors documentation. E.g. object operators page - it’s using operator and properties description from the UI and have no details on how it suppose to/can be used from Python API. UI descriptions shouldn’t be polluted with implementation details, so something new should be added to the documentation. Posting this as an idea on Right-Click Select is probably not very meaningful as it usually end up just hanging there forever.
Maybe just creating another public repository with documentation for operators would make sense? E.g. with small descriptions for default Blender operator like shown below.
Any thoughts?
object.convert:
support context override
- can be overridden using
selected_editable_objects- active object is not affected
object.make_single_user:
context override
- check view layer selected objects explicitly
- active object is not affected
object.transform_apply:
support context override
- can be overridden using
selected_editable_objects- active object is not affected