[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [532] trunk/py/scripts/addons/ io_anim_camera.py: script to save out camera animation and switching

Campbell Barton ideasman42 at gmail.com
Thu Apr 1 14:45:48 CEST 2010


Revision: 532
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=532
Author:   campbellbarton
Date:     2010-04-01 14:45:45 +0200 (Thu, 01 Apr 2010)

Log Message:
-----------
script to save out camera animation and switching

Added Paths:
-----------
    trunk/py/scripts/addons/io_anim_camera.py

Added: trunk/py/scripts/addons/io_anim_camera.py
===================================================================
--- trunk/py/scripts/addons/io_anim_camera.py	                        (rev 0)
+++ trunk/py/scripts/addons/io_anim_camera.py	2010-04-01 12:45:45 UTC (rev 532)
@@ -0,0 +1,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()




More information about the Bf-extensions-cvs mailing list