Merge branch 'blender-v2.92-release'
[blender-addons.git] / io_anim_nuke_chan / export_nuke_chan.py
blobd1c9a9b4565360b93d3e69752a71c32a6f939ca4
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-80 compliant>
21 """ This script is an exporter to the nuke's .chan files.
22 It takes the currently active object and writes it's transformation data
23 into a text file with .chan extension."""
25 from mathutils import Matrix
26 from math import radians, degrees
29 def save_chan(context, filepath, y_up, rot_ord):
31 # get the active scene and object
32 scene = context.scene
33 obj = context.active_object
34 camera = obj.data if obj.type == 'CAMERA' else None
36 # get the range of an animation
37 f_start = scene.frame_start
38 f_end = scene.frame_end
40 # prepare the correcting matrix
41 rot_mat = Matrix.Rotation(radians(-90.0), 4, 'X').to_4x4()
43 filehandle = open(filepath, 'w')
44 fw = filehandle.write
46 # iterate the frames
47 for frame in range(f_start, f_end + 1, 1):
49 # set the current frame
50 scene.frame_set(frame)
52 # get the objects world matrix
53 mat = obj.matrix_world.copy()
55 # if the setting is proper use the rotation matrix
56 # to flip the Z and Y axis
57 if y_up:
58 mat = rot_mat @ mat
60 # create the first component of a new line, the frame number
61 fw("%i\t" % frame)
63 # create transform component
64 t = mat.to_translation()
65 fw("%f\t%f\t%f\t" % t[:])
67 # create rotation component
68 r = mat.to_euler(rot_ord)
70 fw("%f\t%f\t%f\t" % (degrees(r[0]), degrees(r[1]), degrees(r[2])))
72 # if the selected object is a camera export vertical fov also
73 if camera:
74 vfov = degrees(camera.angle_y)
75 fw("%f" % vfov)
77 fw("\n")
79 # after the whole loop close the file
80 filehandle.close()
82 return {'FINISHED'}