Export_3ds: Improved distance cue node search
[blender-addons.git] / io_anim_camera.py
blob60b50bda4a71fec7a093c3a6d23d6c2072799afe
1 # SPDX-FileCopyrightText: 2010-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 bl_info = {
6 "name": "Export Camera Animation",
7 "author": "Campbell Barton",
8 "version": (0, 1),
9 "blender": (2, 80, 0),
10 "location": "File > Export > Cameras & Markers (.py)",
11 "description": "Export Cameras & Markers (.py)",
12 "warning": "",
13 "doc_url": "{BLENDER_MANUAL_URL}/addons/import_export/anim_camera.html",
14 "support": 'OFFICIAL',
15 "category": "Import-Export",
19 import bpy
22 def write_cameras(context, fh, frame_start, frame_end, only_selected=False):
24 data_attrs = (
25 "lens",
26 "shift_x",
27 "shift_y",
28 "dof.focus_distance",
29 "clip_start",
30 "clip_end",
31 "display_size",
34 obj_attrs = (
35 "hide_render",
38 fw = fh.write
40 scene = bpy.context.scene
42 cameras = []
44 for obj in scene.objects:
45 if only_selected and not obj.select_get():
46 continue
47 if obj.type != 'CAMERA':
48 continue
50 cameras.append((obj, obj.data))
52 frame_range = range(frame_start, frame_end + 1)
54 fw("import bpy\n"
55 "cameras = {}\n"
56 "scene = bpy.context.scene\n"
57 "frame = scene.frame_current - 1\n"
58 "\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)
72 fw("\n")
74 for f in frame_range:
75 scene.frame_set(f)
76 fw("# new frame\n")
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")
91 # only key the angle
92 fw("data = obj.data\n")
93 fw("data.lens = %s\n" % obj_data.lens)
94 fw("data.keyframe_insert(\"lens\")\n")
96 fw("\n")
98 # now markers
99 fw("# markers\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
105 if marker.camera:
106 fw("marker.camera = cameras.get(%r)\n" % marker.camera.name)
107 fw("\n")
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"
119 filename_ext = ".py"
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",
129 default=True)
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)
134 return {'FINISHED'}
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):
146 import os
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
151 def register():
152 bpy.utils.register_class(CameraExporter)
153 bpy.types.TOPBAR_MT_file_export.append(menu_export)
156 def unregister():
157 bpy.utils.unregister_class(CameraExporter)
158 bpy.types.TOPBAR_MT_file_export.remove(menu_export)
161 if __name__ == "__main__":
162 register()