It can be difficult to quickly find and select the scene camera from many other objects in large scenes. However, we can make an operator and assign its call to a hotkey to find the scene camera in a moment.
We can get the current scene camera with:
1 |
bpy.context.scene.camera |
To select it, we can execute the following command:
1 |
bpy.context.scene.camera.select_set(True) |
Let’s create an operator to call this:
1 2 3 4 5 6 7 8 9 10 11 |
from bpy.types import Operator class SCENE_select_camera(Operator): bl_idname = 'scene.select_camera' bl_label = 'Select scene camera' bl_description = 'Select scene camera' bl_options = {'REGISTER', 'UNDO'} def execute(self, context): context.scene.camera.select_set(True) return {'FINISHED'} |
and register it Blender Python API:
1 2 3 4 5 6 7 8 9 |
from bpy.utils import register_class, unregister_class def register(): register_class(SCENE_select_camera) def unregister(): unregister_class(SCENE_select_camera) |
All together:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import bpy from bpy.types import Operator from bpy.utils import register_class, unregister_class class SCENE_select_camera(Operator): bl_idname = 'scene.select_camera' bl_label = 'Select scene camera' bl_description = 'Select scene camera' bl_options = {'REGISTER', 'UNDO'} def execute(self, context): context.scene.camera.select_set(True) return {'FINISHED'} def register(): register_class(SCENE_select_camera) def unregister(): unregister_class(SCENE_select_camera) if __name__ == '__main__': register() |
After executing this code from the Blender text editor, we can call our operator, for example, from the Python console:
1 |
bpy.ops.scene.select_camera() |
Now the current scene camera will be selected.
To map our operator to a shortcode, open the Preferences window and switch to the KeyMap section.
Expand the “3D View – 3D View (Global)” section and add a new keymap by pressing the “New” button.
Type the “bl_idname” value of our operator in the operator field and assign a convenient key for calling, for example, “Y”.
Now when we press the “Y” key on the keyboard, we will select the current scene camera.
To reset other selections and make only the camera selected, we can improve our operator as follows – before code of selecting the camera, type the following code for deselected all the previous selections:
1 2 |
for obj in context.selected_objects: obj.select_set(False) |
We can improve our operator more by adding the centering of the viewport window on the selected camera.
We can center the viewport window on the selected object with the operator:
1 |
bpy.ops.view3d.view_selected() |
Let’s add this operator call after the selecting camera code.
The updated execute function of our operator now looks like the following:
1 2 3 4 5 6 |
def execute(self, context): for obj in context.selected_objects: obj.select_set(False) context.scene.camera.select_set(True) bpy.ops.view3d.view_selected() return {'FINISHED'} |
Now, when we press the “Y” hotkey, only the current scene camera becomes selected, and the viewport window will center on it.
*.blend and *.py file with an example code for my subscribers on Patreon