Fix mesh_snap_utilities_line running without key-maps available
[blender-addons.git] / add_camera_rigs / operators.py
blob058ed14667e4c20d8e32191aa5081da1ed002cfc
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 import bpy
20 from bpy.types import Operator
23 def get_rig_and_cam(obj):
24 if obj.type == 'ARMATURE':
25 cam = None
26 for child in obj.children:
27 if child.type == 'CAMERA':
28 cam = child
29 break
30 if cam is not None:
31 return obj, cam
32 elif (obj.type == 'CAMERA'
33 and obj.parent is not None
34 and "rig_id" in obj.parent
35 and obj.parent["rig_id"].lower() in {"dolly_rig",
36 "crane_rig", "2d_rig"}):
37 return obj.parent, obj
38 return None, None
41 class CameraRigMixin():
42 @classmethod
43 def poll(cls, context):
44 if context.active_object is not None:
45 return get_rig_and_cam(context.active_object) != (None, None)
47 return False
50 class ADD_CAMERA_RIGS_OT_set_scene_camera(Operator):
51 bl_idname = "add_camera_rigs.set_scene_camera"
52 bl_label = "Make Camera Active"
53 bl_description = "Makes the camera parented to this rig the active scene camera"
55 @classmethod
56 def poll(cls, context):
57 if context.active_object is not None:
58 rig, cam = get_rig_and_cam(context.active_object)
59 if cam is not None:
60 return cam is not context.scene.camera
62 return False
64 def execute(self, context):
65 rig, cam = get_rig_and_cam(context.active_object)
66 scene_cam = context.scene.camera
68 context.scene.camera = cam
69 return {'FINISHED'}
72 class ADD_CAMERA_RIGS_OT_add_marker_bind(Operator, CameraRigMixin):
73 bl_idname = "add_camera_rigs.add_marker_bind"
74 bl_label = "Add Marker and Bind Camera"
75 bl_description = "Add marker to current frame then bind rig camera to it (for camera switching)"
77 def execute(self, context):
78 rig, cam = get_rig_and_cam(context.active_object)
80 marker = context.scene.timeline_markers.new(
81 "cam_" + str(context.scene.frame_current),
82 frame=context.scene.frame_current
84 marker.camera = cam
86 return {'FINISHED'}
89 class ADD_CAMERA_RIGS_OT_add_dof_object(Operator, CameraRigMixin):
90 bl_idname = "add_camera_rigs.add_dof_object"
91 bl_label = "Add DOF Object"
92 bl_description = "Create Empty and add as DOF Object"
94 def execute(self, context):
95 rig, cam = get_rig_and_cam(context.active_object)
96 bone = rig.data.bones['Aim_shape_rotation-MCH']
98 # Add Empty
99 empty_obj = bpy.data.objects.new("EmptyDOF", None)
100 context.scene.collection.objects.link(empty_obj)
102 # Parent to Aim Child bone
103 empty_obj.parent = rig
104 empty_obj.parent_type = "BONE"
105 empty_obj.parent_bone = "Aim_shape_rotation-MCH"
107 # Move to bone head
108 empty_obj.location = bone.head
110 # Make this new empty the dof_object
111 cam.data.dof.use_dof = True
112 cam.data.dof.focus_object = empty_obj
114 return {'FINISHED'}
117 classes = (
118 ADD_CAMERA_RIGS_OT_set_scene_camera,
119 ADD_CAMERA_RIGS_OT_add_marker_bind,
120 ADD_CAMERA_RIGS_OT_add_dof_object,
124 def register():
125 from bpy.utils import register_class
126 for cls in classes:
127 register_class(cls)
130 def unregister():
131 from bpy.utils import unregister_class
132 for cls in classes:
133 unregister_class(cls)