Fix #100973: Node Wrangler: previewing node if hierarchy not active
[blender-addons.git] / add_camera_rigs / operators.py
blobc9a1d9cccdcdcc2be62e9a9fc79359fa022e77f2
1 # SPDX-FileCopyrightText: 2019-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import bpy
6 from bpy.types import Operator
9 def get_rig_and_cam(obj):
10 if (obj.type == 'ARMATURE'
11 and "rig_id" in obj
12 and obj["rig_id"].lower() in {"dolly_rig",
13 "crane_rig", "2d_rig"}):
14 cam = None
15 for child in obj.children:
16 if child.type == 'CAMERA':
17 cam = child
18 break
19 if cam is not None:
20 return obj, cam
21 elif (obj.type == 'CAMERA'
22 and obj.parent is not None
23 and "rig_id" in obj.parent
24 and obj.parent["rig_id"].lower() in {"dolly_rig",
25 "crane_rig", "2d_rig"}):
26 return obj.parent, obj
27 return None, None
30 class CameraRigMixin():
31 @classmethod
32 def poll(cls, context):
33 if context.active_object is not None:
34 return get_rig_and_cam(context.active_object) != (None, None)
36 return False
39 class ADD_CAMERA_RIGS_OT_set_scene_camera(Operator):
40 bl_idname = "add_camera_rigs.set_scene_camera"
41 bl_label = "Make Camera Active"
42 bl_description = "Makes the camera parented to this rig the active scene camera"
44 @classmethod
45 def poll(cls, context):
46 if context.active_object is not None:
47 rig, cam = get_rig_and_cam(context.active_object)
48 if cam is not None:
49 return cam is not context.scene.camera
51 return False
53 def execute(self, context):
54 rig, cam = get_rig_and_cam(context.active_object)
55 scene_cam = context.scene.camera
57 context.scene.camera = cam
58 return {'FINISHED'}
61 class ADD_CAMERA_RIGS_OT_add_marker_bind(Operator, CameraRigMixin):
62 bl_idname = "add_camera_rigs.add_marker_bind"
63 bl_label = "Add Marker and Bind Camera"
64 bl_description = "Add marker to current frame then bind rig camera to it (for camera switching)"
66 def execute(self, context):
67 rig, cam = get_rig_and_cam(context.active_object)
69 marker = context.scene.timeline_markers.new(
70 "cam_" + str(context.scene.frame_current),
71 frame=context.scene.frame_current
73 marker.camera = cam
75 return {'FINISHED'}
78 class ADD_CAMERA_RIGS_OT_set_dof_bone(Operator, CameraRigMixin):
79 bl_idname = "add_camera_rigs.set_dof_bone"
80 bl_label = "Set DOF Bone"
81 bl_description = "Set the Aim bone as a DOF target"
83 def execute(self, context):
84 rig, cam = get_rig_and_cam(context.active_object)
86 cam.data.dof.focus_object = rig
87 cam.data.dof.focus_subtarget = (
88 'Center-MCH' if rig["rig_id"].lower() == '2d_rig'
89 else 'Aim_shape_rotation-MCH')
91 return {'FINISHED'}
94 classes = (
95 ADD_CAMERA_RIGS_OT_set_scene_camera,
96 ADD_CAMERA_RIGS_OT_add_marker_bind,
97 ADD_CAMERA_RIGS_OT_set_dof_bone,
101 def register():
102 from bpy.utils import register_class
103 for cls in classes:
104 register_class(cls)
107 def unregister():
108 from bpy.utils import unregister_class
109 for cls in classes:
110 unregister_class(cls)