Cleanup: autopep8 for 3DS i/o
[blender-addons.git] / io_anim_camera.py
blob8acfd5d06cb5de4da7bdfe7ac47f74e8e98a5e9d
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 bl_info = {
4 "name": "Export Camera Animation",
5 "author": "Campbell Barton",
6 "version": (0, 1),
7 "blender": (2, 80, 0),
8 "location": "File > Export > Cameras & Markers (.py)",
9 "description": "Export Cameras & Markers (.py)",
10 "warning": "",
11 "doc_url": "{BLENDER_MANUAL_URL}/addons/import_export/anim_camera.html",
12 "support": 'OFFICIAL',
13 "category": "Import-Export",
17 import bpy
20 def write_cameras(context, fh, frame_start, frame_end, only_selected=False):
22 data_attrs = (
23 "lens",
24 "shift_x",
25 "shift_y",
26 "dof.focus_distance",
27 "clip_start",
28 "clip_end",
29 "display_size",
32 obj_attrs = (
33 "hide_render",
36 fw = fh.write
38 scene = bpy.context.scene
40 cameras = []
42 for obj in scene.objects:
43 if only_selected and not obj.select_get():
44 continue
45 if obj.type != 'CAMERA':
46 continue
48 cameras.append((obj, obj.data))
50 frame_range = range(frame_start, frame_end + 1)
52 fw("import bpy\n"
53 "cameras = {}\n"
54 "scene = bpy.context.scene\n"
55 "frame = scene.frame_current - 1\n"
56 "\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)
70 fw("\n")
72 for f in frame_range:
73 scene.frame_set(f)
74 fw("# new frame\n")
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")
89 # only key the angle
90 fw("data = obj.data\n")
91 fw("data.lens = %s\n" % obj_data.lens)
92 fw("data.keyframe_insert(\"lens\")\n")
94 fw("\n")
96 # now markers
97 fw("# markers\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
103 if marker.camera:
104 fw("marker.camera = cameras.get(%r)\n" % marker.camera.name)
105 fw("\n")
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"
117 filename_ext = ".py"
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",
127 default=True)
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)
132 return {'FINISHED'}
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):
144 import os
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
149 def register():
150 bpy.utils.register_class(CameraExporter)
151 bpy.types.TOPBAR_MT_file_export.append(menu_export)
154 def unregister():
155 bpy.utils.unregister_class(CameraExporter)
156 bpy.types.TOPBAR_MT_file_export.remove(menu_export)
159 if __name__ == "__main__":
160 register()