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 #####
22 "name": "Export Camera Animation",
23 "author": "Campbell Barton",
25 "blender": (2, 80, 0),
26 "location": "File > Export > Cameras & Markers (.py)",
27 "description": "Export Cameras & Markers (.py)",
29 "doc_url": "{BLENDER_MANUAL_URL}/addons/import_export/anim_camera.html",
30 "support": 'OFFICIAL',
31 "category": "Import-Export",
38 def write_cameras(context
, filepath
, frame_start
, frame_end
, only_selected
=False):
54 fw
= open(filepath
, 'w').write
56 scene
= bpy
.context
.scene
60 for obj
in scene
.objects
:
61 if only_selected
and not obj
.select_get():
63 if obj
.type != 'CAMERA':
66 cameras
.append((obj
, obj
.data
))
68 frame_range
= range(frame_start
, frame_end
+ 1)
72 "scene = bpy.context.scene\n"
73 "frame = scene.frame_current - 1\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
)
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")
108 fw("data = obj.data\n")
109 fw("data.lens = %s\n" % obj_data
.lens
)
110 fw("data.keyframe_insert('lens')\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
122 fw("marker.camera = cameras.get(%r)\n" % marker
.camera
.name
)
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"
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",
147 def execute(self
, context
):
148 write_cameras(context
, self
.filepath
, self
.frame_start
, self
.frame_end
, self
.only_selected
)
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
):
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
167 bpy
.utils
.register_class(CameraExporter
)
168 bpy
.types
.TOPBAR_MT_file_export
.append(menu_export
)
172 bpy
.utils
.unregister_class(CameraExporter
)
173 bpy
.types
.TOPBAR_MT_file_export
.remove(menu_export
)
176 if __name__
== "__main__":