Merge branch 'master' into blender2.8
[blender-addons.git] / io_anim_camera.py
blob20cb539521c782ba03982da4855c5a299c9a2800
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 # <pep8 compliant>
21 bl_info = {
22 "name": "Export Camera Animation",
23 "author": "Campbell Barton",
24 "version": (0, 1),
25 "blender": (2, 57, 0),
26 "location": "File > Export > Cameras & Markers (.py)",
27 "description": "Export Cameras & Markers (.py)",
28 "warning": "",
29 "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
30 "Scripts/Import-Export/Camera_Animation",
31 "support": 'OFFICIAL',
32 "category": "Import-Export",
36 import bpy
39 def write_cameras(context, filepath, frame_start, frame_end, only_selected=False):
41 data_attrs = (
42 'lens',
43 'shift_x',
44 'shift_y',
45 'dof_distance',
46 'clip_start',
47 'clip_end',
48 'draw_size',
51 obj_attrs = (
52 'hide_render',
55 fw = open(filepath, 'w').write
57 scene = bpy.context.scene
59 cameras = []
61 for obj in scene.objects:
62 if only_selected and not obj.select:
63 continue
64 if obj.type != 'CAMERA':
65 continue
67 cameras.append((obj, obj.data))
69 frame_range = range(frame_start, frame_end + 1)
71 fw("import bpy\n"
72 "cameras = {}\n"
73 "scene = bpy.context.scene\n"
74 "frame = scene.frame_current - 1\n"
75 "\n")
77 for obj, obj_data in cameras:
78 fw("data = bpy.data.cameras.new(%r)\n" % obj.name)
79 for attr in data_attrs:
80 fw("data.%s = %s\n" % (attr, repr(getattr(obj_data, attr))))
82 fw("obj = bpy.data.objects.new(%r, data)\n" % obj.name)
84 for attr in obj_attrs:
85 fw("obj.%s = %s\n" % (attr, repr(getattr(obj, attr))))
87 fw("scene.objects.link(obj)\n")
88 fw("cameras[%r] = obj\n" % obj.name)
89 fw("\n")
91 for f in frame_range:
92 scene.frame_set(f)
93 fw("# new frame\n")
94 fw("scene.frame_set(%d + frame)\n" % f)
96 for obj, obj_data in cameras:
97 fw("obj = cameras['%s']\n" % obj.name)
99 matrix = obj.matrix_world.copy()
100 fw("obj.location = %r, %r, %r\n" % matrix.to_translation()[:])
101 fw("obj.scale = %r, %r, %r\n" % matrix.to_scale()[:])
102 fw("obj.rotation_euler = %r, %r, %r\n" % matrix.to_euler()[:])
104 fw("obj.keyframe_insert('location')\n")
105 fw("obj.keyframe_insert('scale')\n")
106 fw("obj.keyframe_insert('rotation_euler')\n")
108 # only key the angle
109 fw("data = obj.data\n")
110 fw("data.lens = %s\n" % obj_data.lens)
111 fw("data.keyframe_insert('lens')\n")
113 fw("\n")
115 # now markers
116 fw("# markers\n")
117 for marker in scene.timeline_markers:
118 fw("marker = scene.timeline_markers.new(%r)\n" % marker.name)
119 fw("marker.frame = %d + frame\n" % marker.frame)
121 # will fail if the cameras not selected
122 if marker.camera:
123 fw("marker.camera = cameras.get(%r)\n" % marker.camera.name)
124 fw("\n")
127 from bpy.props import StringProperty, IntProperty, BoolProperty
128 from bpy_extras.io_utils import ExportHelper
131 class CameraExporter(bpy.types.Operator, ExportHelper):
132 """Save a python script which re-creates cameras and markers elsewhere"""
133 bl_idname = "export_animation.cameras"
134 bl_label = "Export Camera & Markers"
136 filename_ext = ".py"
137 filter_glob = StringProperty(default="*.py", options={'HIDDEN'})
139 frame_start = IntProperty(name="Start Frame",
140 description="Start frame for export",
141 default=1, min=1, max=300000)
142 frame_end = IntProperty(name="End Frame",
143 description="End frame for export",
144 default=250, min=1, max=300000)
145 only_selected = BoolProperty(name="Only Selected",
146 default=True)
148 def execute(self, context):
149 write_cameras(context, self.filepath, self.frame_start, self.frame_end, self.only_selected)
150 return {'FINISHED'}
152 def invoke(self, context, event):
153 self.frame_start = context.scene.frame_start
154 self.frame_end = context.scene.frame_end
156 wm = context.window_manager
157 wm.fileselect_add(self)
158 return {'RUNNING_MODAL'}
161 def menu_export(self, context):
162 import os
163 default_path = os.path.splitext(bpy.data.filepath)[0] + ".py"
164 self.layout.operator(CameraExporter.bl_idname, text="Cameras & Markers (.py)").filepath = default_path
167 def register():
168 bpy.utils.register_module(__name__)
170 bpy.types.INFO_MT_file_export.append(menu_export)
173 def unregister():
174 bpy.utils.unregister_module(__name__)
176 bpy.types.INFO_MT_file_export.remove(menu_export)
179 if __name__ == "__main__":
180 register()