I want to align the camera view to the cube without selecting the Cube. For that, i wanted to use a context.temp_override() for the “selected_objects” and then call the bpy.ops.view3d.camera_to_view_selected() operator like so:
import bpy
selected_objects = [bpy.data.objects['Cube']]
with bpy.context.temp_override(selected_objects=selected_objects):
bpy.ops.view3d.camera_to_view_selected() # No reaction...
# bpy.ops.transform.translate(value=(0.0, 0.0, 1.0)) # --> This works fine
Doing the same thing with the translate operator works fine, but with this one it just doesn’t work… If i have the Suzanne selected, the camera view will be aligned to that Suzanne and not the Cube…
Does anyone have some idea, what i’m missing here?
I’m currently rewriting a script at work, which often made use of operators and therefore deselecting and selecting objects all the time. It’s awful to work with, hence why i want to avoid that.
I did managed to get the same functionality with the .camera_fit_coords() function - letting me avoid operators all together. But in terms of speed, i just couldn’t get it on par with the operator… For context: I’m doing this on CAD-geometry with a couple million vertices.
Because of that, i thought i would just swallow the pill and use the operator → but with a context.override() to avoid having to deselect and select objects. But that’s where i hit a road block
I’ve investigated view3d.camera_to_view_selected code and it seems it’s accessing selected objects explicitly, not through context.selected_objects. So you won’t be able to use temp_override for this, the only option is to deselect all objects, select others and run the operators and then apply selection again. Or you can temporarily deselect jump couple objects you don’t need.