fix [#36995] FBX Importer does not import fbx model
[blender-addons.git] / io_anim_camera.py
blob376a056412e370932316d9af60df183ad604075a
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, 57, 0),
26 "location": "File > Export > Cameras & Markers (.py)",
27 "description": "Export Cameras & Markers (.py)",
28 "warning": "",
29 "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"\
30 "Scripts/Import-Export/Camera_Animation",
31 "tracker_url": "https://projects.blender.org/tracker/index.php?"\
32 "func=detail&aid=22835",
33 "support": 'OFFICIAL',
34 "category": "Import-Export"}
37 import bpy
40 def write_cameras(context, filepath, frame_start, frame_end, only_selected=False):
42 data_attrs = (
43 'lens',
44 'shift_x',
45 'shift_y',
46 'dof_distance',
47 'clip_start',
48 'clip_end',
49 'draw_size',
52 obj_attrs = (
53 'hide_render',
56 fw = open(filepath, 'w').write
58 scene = bpy.context.scene
60 cameras = []
62 for obj in scene.objects:
63 if only_selected and not obj.select:
64 continue
65 if obj.type != 'CAMERA':
66 continue
68 cameras.append((obj, obj.data))
70 frame_range = range(frame_start, frame_end + 1)
72 fw("import bpy\n"
73 "cameras = {}\n"
74 "scene = bpy.context.scene\n"
75 "frame = scene.frame_current - 1\n"
76 "\n")
78 for obj, obj_data in cameras:
79 fw("data = bpy.data.cameras.new(%r)\n" % obj.name)
80 for attr in data_attrs:
81 fw("data.%s = %s\n" % (attr, repr(getattr(obj_data, attr))))
83 fw("obj = bpy.data.objects.new(%r, data)\n" % obj.name)
85 for attr in obj_attrs:
86 fw("obj.%s = %s\n" % (attr, repr(getattr(obj, attr))))
88 fw("scene.objects.link(obj)\n")
89 fw("cameras[%r] = obj\n" % obj.name)
90 fw("\n")
92 for f in frame_range:
93 scene.frame_set(f)
94 fw("# new frame\n")
95 fw("scene.frame_set(%d + frame)\n" % f)
97 for obj, obj_data in cameras:
98 fw("obj = cameras['%s']\n" % obj.name)
100 matrix = obj.matrix_world.copy()
101 fw("obj.location = %r, %r, %r\n" % matrix.to_translation()[:])
102 fw("obj.scale = %r, %r, %r\n" % matrix.to_scale()[:])
103 fw("obj.rotation_euler = %r, %r, %r\n" % matrix.to_euler()[:])
105 fw("obj.keyframe_insert('location')\n")
106 fw("obj.keyframe_insert('scale')\n")
107 fw("obj.keyframe_insert('rotation_euler')\n")
109 # only key the angle
110 fw("data = obj.data\n")
111 fw("data.lens = %s\n" % obj_data.lens)
112 fw("data.keyframe_insert('lens')\n")
114 fw("\n")
116 # now markers
117 fw("# markers\n")
118 for marker in scene.timeline_markers:
119 fw("marker = scene.timeline_markers.new(%r)\n" % marker.name)
120 fw("marker.frame = %d + frame\n" % marker.frame)
122 # will fail if the cameras not selected
123 if marker.camera:
124 fw("marker.camera = cameras.get(%r)\n" % marker.camera.name)
125 fw("\n")
128 from bpy.props import StringProperty, IntProperty, BoolProperty
129 from bpy_extras.io_utils import ExportHelper
132 class CameraExporter(bpy.types.Operator, ExportHelper):
133 """Save a python script which re-creates cameras and markers elsewhere"""
134 bl_idname = "export_animation.cameras"
135 bl_label = "Export Camera & Markers"
137 filename_ext = ".py"
138 filter_glob = StringProperty(default="*.py", options={'HIDDEN'})
140 frame_start = IntProperty(name="Start Frame",
141 description="Start frame for export",
142 default=1, min=1, max=300000)
143 frame_end = IntProperty(name="End Frame",
144 description="End frame for export",
145 default=250, min=1, max=300000)
146 only_selected = BoolProperty(name="Only Selected",
147 default=True)
149 def execute(self, context):
150 write_cameras(context, self.filepath, self.frame_start, self.frame_end, self.only_selected)
151 return {'FINISHED'}
153 def invoke(self, context, event):
154 self.frame_start = context.scene.frame_start
155 self.frame_end = context.scene.frame_end
157 wm = context.window_manager
158 wm.fileselect_add(self)
159 return {'RUNNING_MODAL'}
162 def menu_export(self, context):
163 import os
164 default_path = os.path.splitext(bpy.data.filepath)[0] + ".py"
165 self.layout.operator(CameraExporter.bl_idname, text="Cameras & Markers (.py)").filepath = default_path
168 def register():
169 bpy.utils.register_module(__name__)
171 bpy.types.INFO_MT_file_export.append(menu_export)
174 def unregister():
175 bpy.utils.unregister_module(__name__)
177 bpy.types.INFO_MT_file_export.remove(menu_export)
180 if __name__ == "__main__":
181 register()