Pose Library: update for rename of asset_library to asset_library_ref
[blender-addons.git] / io_anim_camera.py
blob6130b552f48f4d1da304b1deddc8010d0aea69da
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 "doc_url": "{BLENDER_MANUAL_URL}/addons/import_export/anim_camera.html",
30 "support": 'OFFICIAL',
31 "category": "Import-Export",
35 import bpy
38 def write_cameras(context, filepath, frame_start, frame_end, only_selected=False):
40 data_attrs = (
41 'lens',
42 'shift_x',
43 'shift_y',
44 'dof_distance',
45 'clip_start',
46 'clip_end',
47 'display_size',
50 obj_attrs = (
51 'hide_render',
54 fw = open(filepath, 'w').write
56 scene = bpy.context.scene
58 cameras = []
60 for obj in scene.objects:
61 if only_selected and not obj.select_get():
62 continue
63 if obj.type != 'CAMERA':
64 continue
66 cameras.append((obj, obj.data))
68 frame_range = range(frame_start, frame_end + 1)
70 fw("import bpy\n"
71 "cameras = {}\n"
72 "scene = bpy.context.scene\n"
73 "frame = scene.frame_current - 1\n"
74 "\n")
76 for obj, obj_data in cameras:
77 fw("data = bpy.data.cameras.new(%r)\n" % obj.name)
78 for attr in data_attrs:
79 fw("data.%s = %s\n" % (attr, repr(getattr(obj_data, attr))))
81 fw("obj = bpy.data.objects.new(%r, data)\n" % obj.name)
83 for attr in obj_attrs:
84 fw("obj.%s = %s\n" % (attr, repr(getattr(obj, attr))))
86 fw("bpy.context.collection.objects.link(obj)\n")
87 fw("cameras[%r] = obj\n" % obj.name)
88 fw("\n")
90 for f in frame_range:
91 scene.frame_set(f)
92 fw("# new frame\n")
93 fw("scene.frame_set(%d + frame)\n" % f)
95 for obj, obj_data in cameras:
96 fw("obj = cameras['%s']\n" % obj.name)
98 matrix = obj.matrix_world.copy()
99 fw("obj.location = %r, %r, %r\n" % matrix.to_translation()[:])
100 fw("obj.scale = %r, %r, %r\n" % matrix.to_scale()[:])
101 fw("obj.rotation_euler = %r, %r, %r\n" % matrix.to_euler()[:])
103 fw("obj.keyframe_insert('location')\n")
104 fw("obj.keyframe_insert('scale')\n")
105 fw("obj.keyframe_insert('rotation_euler')\n")
107 # only key the angle
108 fw("data = obj.data\n")
109 fw("data.lens = %s\n" % obj_data.lens)
110 fw("data.keyframe_insert('lens')\n")
112 fw("\n")
114 # now markers
115 fw("# markers\n")
116 for marker in scene.timeline_markers:
117 fw("marker = scene.timeline_markers.new(%r)\n" % marker.name)
118 fw("marker.frame = %d + frame\n" % marker.frame)
120 # will fail if the cameras not selected
121 if marker.camera:
122 fw("marker.camera = cameras.get(%r)\n" % marker.camera.name)
123 fw("\n")
126 from bpy.props import StringProperty, IntProperty, BoolProperty
127 from bpy_extras.io_utils import ExportHelper
130 class CameraExporter(bpy.types.Operator, ExportHelper):
131 """Save a python script which re-creates cameras and markers elsewhere"""
132 bl_idname = "export_animation.cameras"
133 bl_label = "Export Camera & Markers"
135 filename_ext = ".py"
136 filter_glob: StringProperty(default="*.py", options={'HIDDEN'})
138 frame_start: IntProperty(name="Start Frame",
139 description="Start frame for export",
140 default=1, min=1, max=300000)
141 frame_end: IntProperty(name="End Frame",
142 description="End frame for export",
143 default=250, min=1, max=300000)
144 only_selected: BoolProperty(name="Only Selected",
145 default=True)
147 def execute(self, context):
148 write_cameras(context, self.filepath, self.frame_start, self.frame_end, self.only_selected)
149 return {'FINISHED'}
151 def invoke(self, context, event):
152 self.frame_start = context.scene.frame_start
153 self.frame_end = context.scene.frame_end
155 wm = context.window_manager
156 wm.fileselect_add(self)
157 return {'RUNNING_MODAL'}
160 def menu_export(self, context):
161 import os
162 default_path = os.path.splitext(bpy.data.filepath)[0] + ".py"
163 self.layout.operator(CameraExporter.bl_idname, text="Cameras & Markers (.py)").filepath = default_path
166 def register():
167 bpy.utils.register_class(CameraExporter)
168 bpy.types.TOPBAR_MT_file_export.append(menu_export)
171 def unregister():
172 bpy.utils.unregister_class(CameraExporter)
173 bpy.types.TOPBAR_MT_file_export.remove(menu_export)
176 if __name__ == "__main__":
177 register()