1 # SPDX-FileCopyrightText: 2010-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
6 "name": "Export Camera Animation",
7 "author": "Campbell Barton",
10 "location": "File > Export > Cameras & Markers (.py)",
11 "description": "Export Cameras & Markers (.py)",
13 "doc_url": "{BLENDER_MANUAL_URL}/addons/import_export/anim_camera.html",
14 "support": 'OFFICIAL',
15 "category": "Import-Export",
22 def write_cameras(context
, fh
, frame_start
, frame_end
, only_selected
=False):
40 scene
= bpy
.context
.scene
44 for obj
in scene
.objects
:
45 if only_selected
and not obj
.select_get():
47 if obj
.type != 'CAMERA':
50 cameras
.append((obj
, obj
.data
))
52 frame_range
= range(frame_start
, frame_end
+ 1)
56 "scene = bpy.context.scene\n"
57 "frame = scene.frame_current - 1\n"
60 for obj
, obj_data
in cameras
:
61 fw("data = bpy.data.cameras.new(%r)\n" % obj
.name
)
62 for attr
in data_attrs
:
63 fw("data.%s = %s\n" % (attr
, repr(obj_data
.path_resolve(attr
))))
65 fw("obj = bpy.data.objects.new(%r, data)\n" % obj
.name
)
67 for attr
in obj_attrs
:
68 fw("obj.%s = %s\n" % (attr
, repr(getattr(obj
, attr
))))
70 fw("bpy.context.collection.objects.link(obj)\n")
71 fw("cameras[%r] = obj\n" % obj
.name
)
77 fw("scene.frame_set(%d + frame)\n" % f
)
79 for obj
, obj_data
in cameras
:
80 fw("obj = cameras[%r]\n" % obj
.name
)
82 matrix
= obj
.matrix_world
.copy()
83 fw("obj.location = %r, %r, %r\n" % matrix
.to_translation()[:])
84 fw("obj.scale = %r, %r, %r\n" % matrix
.to_scale()[:])
85 fw("obj.rotation_euler = %r, %r, %r\n" % matrix
.to_euler()[:])
87 fw("obj.keyframe_insert(\"location\")\n")
88 fw("obj.keyframe_insert(\"scale\")\n")
89 fw("obj.keyframe_insert(\"rotation_euler\")\n")
92 fw("data = obj.data\n")
93 fw("data.lens = %s\n" % obj_data
.lens
)
94 fw("data.keyframe_insert(\"lens\")\n")
100 for marker
in scene
.timeline_markers
:
101 fw("marker = scene.timeline_markers.new(%r)\n" % marker
.name
)
102 fw("marker.frame = %d + frame\n" % marker
.frame
)
104 # will fail if the cameras not selected
106 fw("marker.camera = cameras.get(%r)\n" % marker
.camera
.name
)
110 from bpy
.props
import StringProperty
, IntProperty
, BoolProperty
111 from bpy_extras
.io_utils
import ExportHelper
114 class CameraExporter(bpy
.types
.Operator
, ExportHelper
):
115 """Save a python script which re-creates cameras and markers elsewhere"""
116 bl_idname
= "export_animation.cameras"
117 bl_label
= "Export Camera & Markers"
120 filter_glob
: StringProperty(default
="*.py", options
={'HIDDEN'})
122 frame_start
: IntProperty(name
="Start Frame",
123 description
="Start frame for export",
124 default
=1, min=1, max=300000)
125 frame_end
: IntProperty(name
="End Frame",
126 description
="End frame for export",
127 default
=250, min=1, max=300000)
128 only_selected
: BoolProperty(name
="Only Selected",
131 def execute(self
, context
):
132 with
open(self
.filepath
, 'w', encoding
='utf-8') as fh
:
133 write_cameras(context
, fh
, self
.frame_start
, self
.frame_end
, self
.only_selected
)
136 def invoke(self
, context
, event
):
137 self
.frame_start
= context
.scene
.frame_start
138 self
.frame_end
= context
.scene
.frame_end
140 wm
= context
.window_manager
141 wm
.fileselect_add(self
)
142 return {'RUNNING_MODAL'}
145 def menu_export(self
, context
):
147 default_path
= os
.path
.splitext(bpy
.data
.filepath
)[0] + ".py"
148 self
.layout
.operator(CameraExporter
.bl_idname
, text
="Cameras & Markers (.py)").filepath
= default_path
152 bpy
.utils
.register_class(CameraExporter
)
153 bpy
.types
.TOPBAR_MT_file_export
.append(menu_export
)
157 bpy
.utils
.unregister_class(CameraExporter
)
158 bpy
.types
.TOPBAR_MT_file_export
.remove(menu_export
)
161 if __name__
== "__main__":