glTF: Replace Separate RGB shader node by new Separate Color node
[blender-addons.git] / add_camera_rigs / operators.py
blob646566ae52928ea84528efcbedec02fe1da2c1f5
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 cam = None
10 for child in obj.children:
11 if child.type == 'CAMERA':
12 cam = child
13 break
14 if cam is not None:
15 return obj, cam
16 elif (obj.type == 'CAMERA'
17 and obj.parent is not None
18 and "rig_id" in obj.parent
19 and obj.parent["rig_id"].lower() in {"dolly_rig",
20 "crane_rig", "2d_rig"}):
21 return obj.parent, obj
22 return None, None
25 class CameraRigMixin():
26 @classmethod
27 def poll(cls, context):
28 if context.active_object is not None:
29 return get_rig_and_cam(context.active_object) != (None, None)
31 return False
34 class ADD_CAMERA_RIGS_OT_set_scene_camera(Operator):
35 bl_idname = "add_camera_rigs.set_scene_camera"
36 bl_label = "Make Camera Active"
37 bl_description = "Makes the camera parented to this rig the active scene camera"
39 @classmethod
40 def poll(cls, context):
41 if context.active_object is not None:
42 rig, cam = get_rig_and_cam(context.active_object)
43 if cam is not None:
44 return cam is not context.scene.camera
46 return False
48 def execute(self, context):
49 rig, cam = get_rig_and_cam(context.active_object)
50 scene_cam = context.scene.camera
52 context.scene.camera = cam
53 return {'FINISHED'}
56 class ADD_CAMERA_RIGS_OT_add_marker_bind(Operator, CameraRigMixin):
57 bl_idname = "add_camera_rigs.add_marker_bind"
58 bl_label = "Add Marker and Bind Camera"
59 bl_description = "Add marker to current frame then bind rig camera to it (for camera switching)"
61 def execute(self, context):
62 rig, cam = get_rig_and_cam(context.active_object)
64 marker = context.scene.timeline_markers.new(
65 "cam_" + str(context.scene.frame_current),
66 frame=context.scene.frame_current
68 marker.camera = cam
70 return {'FINISHED'}
73 class ADD_CAMERA_RIGS_OT_add_dof_object(Operator, CameraRigMixin):
74 bl_idname = "add_camera_rigs.add_dof_object"
75 bl_label = "Add DOF Object"
76 bl_description = "Create Empty and add as DOF Object"
78 def execute(self, context):
79 rig, cam = get_rig_and_cam(context.active_object)
80 bone = rig.data.bones['Aim_shape_rotation-MCH']
82 # Add Empty
83 empty_obj = bpy.data.objects.new("EmptyDOF", None)
84 context.scene.collection.objects.link(empty_obj)
86 # Parent to Aim Child bone
87 empty_obj.parent = rig
88 empty_obj.parent_type = "BONE"
89 empty_obj.parent_bone = "Aim_shape_rotation-MCH"
91 # Move to bone head
92 empty_obj.location = bone.head
94 # Make this new empty the dof_object
95 cam.data.dof.use_dof = True
96 cam.data.dof.focus_object = empty_obj
98 return {'FINISHED'}
101 classes = (
102 ADD_CAMERA_RIGS_OT_set_scene_camera,
103 ADD_CAMERA_RIGS_OT_add_marker_bind,
104 ADD_CAMERA_RIGS_OT_add_dof_object,
108 def register():
109 from bpy.utils import register_class
110 for cls in classes:
111 register_class(cls)
114 def unregister():
115 from bpy.utils import unregister_class
116 for cls in classes:
117 unregister_class(cls)