sun_position: fix warning from deleted prop in User Preferences
[blender-addons.git] / io_anim_camera.py
blob1c77c361c996e789e2f3cbc1f3aa1c70c651e7d0
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, 80, 0),
26 "location": "File > Export > Cameras & Markers (.py)",
27 "description": "Export Cameras & Markers (.py)",
28 "warning": "",
29 "wiki_url": "https://docs.blender.org/manual/en/dev/addons/"
30 "import_export/anim_camera.html",
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 'display_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_get():
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("bpy.context.collection.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_class(CameraExporter)
169 bpy.types.TOPBAR_MT_file_export.append(menu_export)
172 def unregister():
173 bpy.utils.unregister_class(CameraExporter)
174 bpy.types.TOPBAR_MT_file_export.remove(menu_export)
177 if __name__ == "__main__":
178 register()