Merge branch 'blender-v3.3-release'
[blender-addons.git] / io_anim_nuke_chan / export_nuke_chan.py
blob5a21018bf0e3e3776b9b9b34106feac2395ea185
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 """ This script is an exporter to the nuke's .chan files.
4 It takes the currently active object and writes it's transformation data
5 into a text file with .chan extension."""
7 from mathutils import Matrix, Euler
8 from math import radians, degrees
11 def save_chan(context, filepath, y_up, rot_ord):
13 # get the active scene and object
14 scene = context.scene
15 obj = context.active_object
16 camera = obj.data if obj.type == 'CAMERA' else None
18 # get the range of an animation
19 f_start = scene.frame_start
20 f_end = scene.frame_end
22 # prepare the correcting matrix
23 rot_mat = Matrix.Rotation(radians(-90.0), 4, 'X').to_4x4()
24 previous_rotation = Euler()
26 filehandle = open(filepath, 'w')
27 fw = filehandle.write
29 # iterate the frames
30 for frame in range(f_start, f_end + 1, 1):
32 # set the current frame
33 scene.frame_set(frame)
35 # get the objects world matrix
36 mat = obj.matrix_world.copy()
38 # if the setting is proper use the rotation matrix
39 # to flip the Z and Y axis
40 if y_up:
41 mat = rot_mat @ mat
43 # create the first component of a new line, the frame number
44 fw("%i\t" % frame)
46 # create transform component
47 t = mat.to_translation()
48 fw("%f\t%f\t%f\t" % t[:])
50 # create rotation component
51 r = mat.to_euler(rot_ord, previous_rotation)
53 fw("%f\t%f\t%f\t" % (degrees(r[0]), degrees(r[1]), degrees(r[2])))
55 # store previous rotation for compatibility
56 previous_rotation = r
58 # if the selected object is a camera export vertical fov also
59 if camera:
60 vfov = degrees(camera.angle_y)
61 fw("%f" % vfov)
63 fw("\n")
65 # after the whole loop close the file
66 filehandle.close()
68 return {'FINISHED'}