[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [2728] contrib/py/scripts/addons/ io_anim_nuke_chan: cleanup and fixes
Campbell Barton
ideasman42 at gmail.com
Mon Dec 5 20:09:10 CET 2011
Revision: 2728
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=2728
Author: campbellbarton
Date: 2011-12-05 19:09:05 +0000 (Mon, 05 Dec 2011)
Log Message:
-----------
cleanup and fixes
- without camera was not writing a new lines.
- added support for importing axis-angle keyframes.
- removed unneeded checks
- use context not bpy.context since its passed as an arg, also define a scene var.
Modified Paths:
--------------
contrib/py/scripts/addons/io_anim_nuke_chan/export_nuke_chan.py
contrib/py/scripts/addons/io_anim_nuke_chan/import_nuke_chan.py
Modified: contrib/py/scripts/addons/io_anim_nuke_chan/export_nuke_chan.py
===================================================================
--- contrib/py/scripts/addons/io_anim_nuke_chan/export_nuke_chan.py 2011-12-05 18:31:02 UTC (rev 2727)
+++ contrib/py/scripts/addons/io_anim_nuke_chan/export_nuke_chan.py 2011-12-05 19:09:05 UTC (rev 2728)
@@ -26,36 +26,34 @@
def save_chan(context, filepath, y_up, rot_ord):
- #check if we have anything selected, if not, end with no action
- if not bpy.context.active_object:
- return {'FINISHED'}
- #get the active object
- obj = bpy.context.active_object
+ #get the active scene and object
+ scene = context.scene
+ obj = context.active_object
#get the range of an animation
- f_start = bpy.context.scene.frame_start
- f_end = bpy.context.scene.frame_end
+ f_start = scene.frame_start
+ f_end = scene.frame_end
#get the resolution (needed by nuke)
- res_x = bpy.context.scene.render.resolution_x
- res_y = bpy.context.scene.render.resolution_y
+ res_x = scene.render.resolution_x
+ res_y = scene.render.resolution_y
res_ratio = res_y / res_x
#prepare the correcting matrix
- rot_mat = Matrix.Rotation(radians(-90), 4, "X").to_4x4()
+ rot_mat = Matrix.Rotation(radians(-90.0), 4, 'X').to_4x4()
- f = open(filepath, 'w')
+ filehandle = open(filepath, 'w')
+ fw = filehandle.write
+
#iterate the frames
- for a in range(f_start, f_end, 1):
- #reset the new line of a chan file
- export_string = []
+ for frame in range(f_start, f_end, 1):
#set the current frame
- bpy.context.scene.frame_set(a)
+ scene.frame_set(frame)
#get the objects world matrix
- mat = obj.matrix_world
+ mat = obj.matrix_world.copy()
#if the setting is proper use the rotation matrix
#to flip the Z and Y axis
@@ -63,19 +61,16 @@
mat = rot_mat * mat
#create the first component of a new line, the frame number
- export_string.append("%i\t" % a)
+ fw("%i\t" % frame)
#create transform component
t = mat.to_translation()
- export_string.append("%f\t%f\t%f\t" % t[:])
+ fw("%f\t%f\t%f\t" % t[:])
#create rotation component
r = mat.to_euler(rot_ord)
- #export_string += "%f\t%f\t%f\t" % (r[0], r[1], r[2])
- export_string.append("%f\t%f\t%f\t" % (degrees(r[0]),
- degrees(r[1]),
- degrees(r[2])))
+ fw("%f\t%f\t%f\t" % (degrees(r[0]), degrees(r[1]), degrees(r[2])))
#if we have a camera, add the focal length
if obj.type == 'CAMERA':
@@ -92,12 +87,11 @@
#atan2 function whitch returns the degree (in radians) of
#an angle formed by a triangle with two legs of a given lengths
vfov = degrees(atan2(sensor_y / 2, cam_lens))*2
- export_string.append("%f\n" % vfov)
+ fw("%f" % vfov)
- #when all is set and done write the new line
- f.write("".join(export_string))
+ fw("\n")
#after the whole loop close the file
- f.close()
+ filehandle.close()
return {'FINISHED'}
Modified: contrib/py/scripts/addons/io_anim_nuke_chan/import_nuke_chan.py
===================================================================
--- contrib/py/scripts/addons/io_anim_nuke_chan/import_nuke_chan.py 2011-12-05 18:31:02 UTC (rev 2727)
+++ contrib/py/scripts/addons/io_anim_nuke_chan/import_nuke_chan.py 2011-12-05 19:09:05 UTC (rev 2728)
@@ -24,42 +24,41 @@
def read_chan(context, filepath, z_up, rot_ord):
- #check if we have anything selected, if not, finish without doing anything.
- if not bpy.context.active_object:
- return {'FINISHED'}
#get the active object
- obj = bpy.context.active_object
+ scene = context.scene
+ obj = context.active_object
#get the resolution (needed to calculate the camera lens)
- res_x = bpy.context.scene.render.resolution_x
- res_y = bpy.context.scene.render.resolution_y
+ res_x = scene.render.resolution_x
+ res_y = scene.render.resolution_y
res_ratio = res_y / res_x
#prepare the correcting matrix
- rot_mat = Matrix.Rotation(radians(90), 4, "X").to_4x4()
+ rot_mat = Matrix.Rotation(radians(90.0), 4, 'X').to_4x4()
#read the file
- f = open(filepath, 'r')
+ filehandle = open(filepath, 'r')
#iterate throug the files lines
- for line in f.readlines():
+ for line in filehandle:
#reset the target objects matrix
#(the one from whitch one we'll extract the final transforms)
m_trans_mat = Matrix()
- m_trans_mat.to_4x4()
#strip the line
data = line.split()
#test if the line is not commented out
- if data[0] != "#":
+ if data and not data[0].startswith("#"):
#set the frame number basing on the chan file
- bpy.context.scene.frame_set(int(data[0]))
+ scene.frame_set(int(data[0]))
#read the translation values from the first three columns of line
- v_transl = Vector([float(data[1]), float(data[2]), float(data[3])])
+ v_transl = Vector((float(data[1]),
+ float(data[2]),
+ float(data[3])))
translation_mat = Matrix.Translation(v_transl)
translation_mat.to_4x4()
@@ -68,9 +67,9 @@
#you have to keep it noted somewhere
#the actual objects rotation order doesn't matter since the
#rotations are being extracted from the matrix afterwards
- e_rot = Euler([radians(float(data[4])),
- radians(float(data[5])),
- radians(float(data[6]))])
+ e_rot = Euler((radians(float(data[4])),
+ radians(float(data[5])),
+ radians(float(data[6]))))
e_rot.order = rot_ord
mrot_mat = e_rot.to_matrix()
mrot_mat.resize_4x4()
@@ -88,25 +87,31 @@
#set the location and the location's keyframe
obj.location = trns[0]
- obj.keyframe_insert('location')
+ obj.keyframe_insert("location")
#convert the rotation to euler angles (or not)
#basing on the objects rotation mode
- if obj.rotation_mode != "QUATERNION":
+ if obj.rotation_mode == 'QUATERNION':
+ obj.rotation_quaternion = trns[1]
+ obj.keyframe_insert("rotation_quaternion")
+ elif obj.rotation_mode == 'AXIS_ANGLE':
+ tmp_rot = trns[1].to_axis_angle()
+ obj.rotation_axis_angle = (tmp_rot[1], ) + tmp_rot[0][:]
+ obj.keyframe_insert("rotation_axis_angle")
+ del tmp_rot
+ else:
obj.rotation_euler = trns[1].to_euler(obj.rotation_mode)
- obj.keyframe_insert('rotation_euler')
- else:
- obj.rotation_euler = trns[1]
- obj.keyframe_insert('rotation_quaternion')
+ obj.keyframe_insert("rotation_euler")
+
#check if the object is camera and fov data is present
- if obj.type == "CAMERA" and len(data) > 7:
+ if obj.type == 'CAMERA' and len(data) > 7:
v_fov = float(data[7])
sensor_v = 32.0
sensor_h = sensor_v * res_ratio
- lenslen = ((sensor_h/2) / tan(radians(v_fov / 2)))
+ lenslen = ((sensor_h / 2.0) / tan(radians(v_fov / 2.0)))
obj.data.lens = lenslen
- obj.data.keyframe_insert('lens')
- f.close()
+ obj.data.keyframe_insert("lens")
+ filehandle.close()
return {'FINISHED'}
More information about the Bf-extensions-cvs
mailing list