1 # SPDX-License-Identifier: GPL-2.0-or-later
4 "name": "Export Camera Animation",
5 "author": "Campbell Barton",
8 "location": "File > Export > Cameras & Markers (.py)",
9 "description": "Export Cameras & Markers (.py)",
11 "doc_url": "{BLENDER_MANUAL_URL}/addons/import_export/anim_camera.html",
12 "support": 'OFFICIAL',
13 "category": "Import-Export",
20 def write_cameras(context
, fh
, frame_start
, frame_end
, only_selected
=False):
38 scene
= bpy
.context
.scene
42 for obj
in scene
.objects
:
43 if only_selected
and not obj
.select_get():
45 if obj
.type != 'CAMERA':
48 cameras
.append((obj
, obj
.data
))
50 frame_range
= range(frame_start
, frame_end
+ 1)
54 "scene = bpy.context.scene\n"
55 "frame = scene.frame_current - 1\n"
58 for obj
, obj_data
in cameras
:
59 fw("data = bpy.data.cameras.new(%r)\n" % obj
.name
)
60 for attr
in data_attrs
:
61 fw("data.%s = %s\n" % (attr
, repr(obj_data
.path_resolve(attr
))))
63 fw("obj = bpy.data.objects.new(%r, data)\n" % obj
.name
)
65 for attr
in obj_attrs
:
66 fw("obj.%s = %s\n" % (attr
, repr(getattr(obj
, attr
))))
68 fw("bpy.context.collection.objects.link(obj)\n")
69 fw("cameras[%r] = obj\n" % obj
.name
)
75 fw("scene.frame_set(%d + frame)\n" % f
)
77 for obj
, obj_data
in cameras
:
78 fw("obj = cameras[%r]\n" % obj
.name
)
80 matrix
= obj
.matrix_world
.copy()
81 fw("obj.location = %r, %r, %r\n" % matrix
.to_translation()[:])
82 fw("obj.scale = %r, %r, %r\n" % matrix
.to_scale()[:])
83 fw("obj.rotation_euler = %r, %r, %r\n" % matrix
.to_euler()[:])
85 fw("obj.keyframe_insert(\"location\")\n")
86 fw("obj.keyframe_insert(\"scale\")\n")
87 fw("obj.keyframe_insert(\"rotation_euler\")\n")
90 fw("data = obj.data\n")
91 fw("data.lens = %s\n" % obj_data
.lens
)
92 fw("data.keyframe_insert(\"lens\")\n")
98 for marker
in scene
.timeline_markers
:
99 fw("marker = scene.timeline_markers.new(%r)\n" % marker
.name
)
100 fw("marker.frame = %d + frame\n" % marker
.frame
)
102 # will fail if the cameras not selected
104 fw("marker.camera = cameras.get(%r)\n" % marker
.camera
.name
)
108 from bpy
.props
import StringProperty
, IntProperty
, BoolProperty
109 from bpy_extras
.io_utils
import ExportHelper
112 class CameraExporter(bpy
.types
.Operator
, ExportHelper
):
113 """Save a python script which re-creates cameras and markers elsewhere"""
114 bl_idname
= "export_animation.cameras"
115 bl_label
= "Export Camera & Markers"
118 filter_glob
: StringProperty(default
="*.py", options
={'HIDDEN'})
120 frame_start
: IntProperty(name
="Start Frame",
121 description
="Start frame for export",
122 default
=1, min=1, max=300000)
123 frame_end
: IntProperty(name
="End Frame",
124 description
="End frame for export",
125 default
=250, min=1, max=300000)
126 only_selected
: BoolProperty(name
="Only Selected",
129 def execute(self
, context
):
130 with
open(self
.filepath
, 'w', encoding
='utf-8') as fh
:
131 write_cameras(context
, fh
, self
.frame_start
, self
.frame_end
, self
.only_selected
)
134 def invoke(self
, context
, event
):
135 self
.frame_start
= context
.scene
.frame_start
136 self
.frame_end
= context
.scene
.frame_end
138 wm
= context
.window_manager
139 wm
.fileselect_add(self
)
140 return {'RUNNING_MODAL'}
143 def menu_export(self
, context
):
145 default_path
= os
.path
.splitext(bpy
.data
.filepath
)[0] + ".py"
146 self
.layout
.operator(CameraExporter
.bl_idname
, text
="Cameras & Markers (.py)").filepath
= default_path
150 bpy
.utils
.register_class(CameraExporter
)
151 bpy
.types
.TOPBAR_MT_file_export
.append(menu_export
)
155 bpy
.utils
.unregister_class(CameraExporter
)
156 bpy
.types
.TOPBAR_MT_file_export
.remove(menu_export
)
159 if __name__
== "__main__":