Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
bl_addon_info = {
'name': 'I/O: Camera Animation',
'author': 'Campbell Barton',
'version': '0.1',
'blender': (2, 5, 3),
'location': 'File > Export > Camera Animation',
'url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/' \
'Scripts/File_I-O/Camera_Animation',
'category': 'Import/Export'}
import bpy
def writeCameras(context, path, start_frame, end_frame):
data_attrs = ['lens', 'shift_x', 'shift_y', 'dof_distance', 'clip_start', 'clip_end', 'draw_size']
obj_attrs = ['restrict_render']
fw = open(path, 'w').write
scene = bpy.context.scene
cameras = [(obj, obj.data) for obj in scene.objects if obj.type == 'CAMERA']
frame_range = range(start_frame, end_frame + 1)
fw("cameras = {}\n")
fw("scene = bpy.context.scene\n")
fw("\n")
for obj, obj_data in cameras:
fw("data = bpy.data.cameras.new('%s')\n" % obj.name)
for attr in data_attrs:
fw("data.%s = %s\n" % (attr, repr(getattr(obj_data, attr))))
fw("obj = bpy.data.objects.new('%s', data)\n" % obj.name)
for attr in obj_attrs:
fw("obj.%s = %s\n" % (attr, repr(getattr(obj, attr))))
fw("scene.objects.link(obj)\n")
fw("cameras['%s'] = obj\n" % obj.name)
fw("\n")
for f in frame_range:
scene.set_frame(f)
fw("# new frame\n")
fw("scene.set_frame(%d)\n" % f)
for obj, obj_data in cameras:
fw("obj = cameras['%s']\n" % obj.name)
matrix = obj.matrix.copy()
fw("obj.location = %s\n" % repr(tuple(matrix.translation_part())))
fw("obj.scale = %s\n" % repr(tuple(matrix.scale_part())))
fw("obj.rotation_euler = %s\n" % repr(tuple(matrix.to_euler())))
fw("obj.keyframe_insert('location')\n")
fw("obj.keyframe_insert('scale')\n")
fw("obj.keyframe_insert('rotation_euler')\n")
# only key the angle
fw("data = obj.data\n")
fw("data.lens = %s\n" % obj_data.lens)
fw("data.keyframe_insert('lens')\n")
fw("\n")
# now markers
fw("# markers\n")
for marker in scene.timeline_markers:
fw("marker = scene.timeline_markers.add('%s')\n" % marker.name)
fw("marker.frame = %d\n" % marker.frame)
if marker.camera:
fw("marker.camera = cameras['%s']\n" % marker.camera.name)
fw("\n")
from bpy.props import *
class CameraExporter(bpy.types.Operator):
'''Save a python script which re-creartes cameras and markers elsewhere'''
bl_idname = "export_animation.cameras"
bl_label = "Export Camera & Markers"
path = StringProperty(name="File Path", description="File path used for importing the RAW file", maxlen=1024, default="")
filename = StringProperty(name="File Name", description="Name of the file.")
directory = StringProperty(name="Directory", description="Directory of the file.")
start_frame = IntProperty(name="Start Frame",
description="Start frame for export",
default=1, min=1, max=300000)
end_frame = IntProperty(name="End Frame",
description="End frame for export",
default=250, min=1, max=300000)
def execute(self, context):
writeCameras(context, self.properties.path, self.start_frame, self.end_frame)
return {'FINISHED'}
def invoke(self, context, event):
self.start_frame = context.scene.start_frame
self.end_frame = context.scene.end_frame
wm = context.manager
wm.add_fileselect(self)
return {'RUNNING_MODAL'}
def menu_export(self, context):
default_path = bpy.data.filename.replace(".blend", ".py")
self.layout.operator(CameraExporter.bl_idname, text="Cameras & Markers (.py)").path = default_path
def register():
bpy.types.register(CameraExporter)
bpy.types.INFO_MT_file_export.append(menu_export)
if __name__ == "__main__":
register()