Merge branch 'blender-v3.3-release'
[blender-addons.git] / add_camera_rigs / operators.py
blob29e77e3bce15aa174bd377a23fe909f27f49a48c
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
4 from bpy.types import Operator
7 def get_rig_and_cam(obj):
8 if (obj.type == 'ARMATURE'
9 and "rig_id" in obj
10 and obj["rig_id"].lower() in {"dolly_rig",
11 "crane_rig", "2d_rig"}):
12 cam = None
13 for child in obj.children:
14 if child.type == 'CAMERA':
15 cam = child
16 break
17 if cam is not None:
18 return obj, cam
19 elif (obj.type == 'CAMERA'
20 and obj.parent is not None
21 and "rig_id" in obj.parent
22 and obj.parent["rig_id"].lower() in {"dolly_rig",
23 "crane_rig", "2d_rig"}):
24 return obj.parent, obj
25 return None, None
28 class CameraRigMixin():
29 @classmethod
30 def poll(cls, context):
31 if context.active_object is not None:
32 return get_rig_and_cam(context.active_object) != (None, None)
34 return False
37 class ADD_CAMERA_RIGS_OT_set_scene_camera(Operator):
38 bl_idname = "add_camera_rigs.set_scene_camera"
39 bl_label = "Make Camera Active"
40 bl_description = "Makes the camera parented to this rig the active scene camera"
42 @classmethod
43 def poll(cls, context):
44 if context.active_object is not None:
45 rig, cam = get_rig_and_cam(context.active_object)
46 if cam is not None:
47 return cam is not context.scene.camera
49 return False
51 def execute(self, context):
52 rig, cam = get_rig_and_cam(context.active_object)
53 scene_cam = context.scene.camera
55 context.scene.camera = cam
56 return {'FINISHED'}
59 class ADD_CAMERA_RIGS_OT_add_marker_bind(Operator, CameraRigMixin):
60 bl_idname = "add_camera_rigs.add_marker_bind"
61 bl_label = "Add Marker and Bind Camera"
62 bl_description = "Add marker to current frame then bind rig camera to it (for camera switching)"
64 def execute(self, context):
65 rig, cam = get_rig_and_cam(context.active_object)
67 marker = context.scene.timeline_markers.new(
68 "cam_" + str(context.scene.frame_current),
69 frame=context.scene.frame_current
71 marker.camera = cam
73 return {'FINISHED'}
76 class ADD_CAMERA_RIGS_OT_set_dof_bone(Operator, CameraRigMixin):
77 bl_idname = "add_camera_rigs.set_dof_bone"
78 bl_label = "Set DOF Bone"
79 bl_description = "Set the Aim bone as a DOF target"
81 def execute(self, context):
82 rig, cam = get_rig_and_cam(context.active_object)
84 cam.data.dof.focus_object = rig
85 cam.data.dof.focus_subtarget = 'Aim_shape_rotation-MCH'
87 return {'FINISHED'}
90 classes = (
91 ADD_CAMERA_RIGS_OT_set_scene_camera,
92 ADD_CAMERA_RIGS_OT_add_marker_bind,
93 ADD_CAMERA_RIGS_OT_set_dof_bone,
97 def register():
98 from bpy.utils import register_class
99 for cls in classes:
100 register_class(cls)
103 def unregister():
104 from bpy.utils import unregister_class
105 for cls in classes:
106 unregister_class(cls)