def _apply_rotation(self, rot): b_q = mathutils.Quaternion((rot[3], rot[0], rot[1], rot[2])) r = 1.0 b_q = mathutils.Quaternion((b_q.w, b_q.x, b_q.y, b_q.z)) euler = b_q.to_euler() return (euler[0]*r, euler[1]*r, (euler[2])*r)
def _apply_rotation(self, rot): b_q = mathutils.Quaternion(rot[3], rot[0], rot[1], rot[2]) #b_q1 = b_q.cross(Blender.Mathutils.Quaternion([0,-1,0])) #b_q2 = b_q1.cross(Blender.Mathutils.Quaternion([-1,0,0])) #b_q3 = b_q2.cross(Blender.Mathutils.Quaternion([0,0,-1])) r = math.pi/180.0; if b_q: b_q = mathutils.Quaternion(b_q.w, b_q.x, b_q.y, b_q.z) euler = b_q.toEuler() return (euler[0]*r, euler[1]*r, (euler[2])*r)
def bone_transform(self, bone_name="Root"): """ Return 4x4 transform matrix giving bone's chage. """ frames = self.action.getFrameNumbers() (first, last) = (frames[0], frames[-1]) ipo = self.action.getChannelIpo(bone_name) position_first = Mathutils.Vector( \ list(ipo[key][first] for key in act_position_keys)) position_last = Mathutils.Vector( \ list(ipo[key][last] for key in act_position_keys)) rotation_first = Mathutils.Quaternion( \ list(ipo[key][first] for key in act_rotation_keys)) rotation_last = Mathutils.Quaternion( \ list(ipo[key][last] for key in act_rotation_keys)) delta_position = position_last - position_first delta_rotation = Mathutils.DifferenceQuats(rotation_last, rotation_first) return offset_and_rotation_to_matrix(delta_position, delta_rotation)
def _get_local_rot(self, rot, parent): return rot parent_imatrix = parent.matrix_world.copy().invert() euler = mathutils.Quaternion((rot[3], rot[0], rot[1], rot[2])) r_mat = euler.to_matrix().to_4x4() l_rmat = r_mat*parent_imatrix try: rot = l_rmat.to_quaternion() except: # blender 2.56 rot = l_rmat.to_quat() return (rot.w, rot.x, rot.y, rot.z)
def run_tests(self): """ The unit tests. """ self.tests_run = 0 self.tests_ok = 0 print "=== Starting tests. ===" # --- start of tests -------------------------------------------------- # ... ok infrastructure self.ok(1==1, "ok itself") # ... get() self.ok(get("mannequin").name=="mannequin", "get('mannequin')") self.ok(get("default action").name=="default action", " get('default action')") act1 = bpy.data.actions['step forward L to R'] self.ok(get(act1).name=='step forward L to R', ' get(action)') s1 = Step('step forward L to R') self.ok(get(s1).name=='step forward L to R', ' get(Step())') get_blank = True try: get("") except: get_blank = False self.ok(not get_blank, " get('') raises exception.") # ... blender_frame() ... Blender.Set('curframe', 1) self.ok(1 == blender_frame(), 'frame()') blender_frame(2) self.ok(2 == blender_frame(), ' frame(2)') # ... max_matrix() ... self.ok(max_matrix([[1,2,3],[-4,3,2]])==4, 'max_matrix()') # ... Step().bone_transform ... action_name = 'step back L to R' position = Mathutils.Vector(0.0, 4.91, 0.0) rotation = Mathutils.Quaternion(1,0,0,0) # no rotation transform = offset_and_rotation_to_matrix(position, rotation) motion = Step(action_name).bone_transform() # print " transform = '%s' " % str(transform) # print " motion = '%s' " % str(motion) max_diff = max_matrix(motion - transform) allowed_diff = 1.0e-3 self.ok( max_diff < allowed_diff, "Step('%s').bone_transform()" % action_name) step1 = Step('forward') self.ok(step1.action.name=='step forward L to R', " Step('forward')") step2 = Step('forward', 'L', mirror=True) self.ok(step2.action.name=='step back R to L', " step mirrored") self.ok(Step.flip('forward')=='back', " Step.flip()") # ... model ... man = Model("man") self.ok(man != None, "Model('man')") man.reset() self.ok(len(man.model.actionStrips)==1, " reset() actionstrips") self.ok(len(man.model.ipo.curves[0].bezierPoints)==1, " reset() ipo") frames = 12 action_name = 'step forward L to R' man.add_motion(action_name, frames) ## print " step : '%s'" % str(step_L_to_R) ## print " name : '%s'" % step_L_to_R.action.name ## print " strip1 : '%s'" % str(man.model.actionStrips[1]) ## print " name : '%s'" % str(man.model.actionStrips[1].action.name) # actionstrip objects are different ... that surprized me. # but name is OK; I guess it makes a copy somewhere. # self.ok(man.model.actionStrips[1] == step_L_to_R, " add_motion()") step_L_to_R = man.model.actionStrips[-1] self.ok(step_L_to_R.action.name == action_name, " add_motion()") actual_frames = step_L_to_R.stripEnd - step_L_to_R.stripStart self.ok(actual_frames == frames, " frame length") man.housekeeping() end0 = man.model.actionStrips[0].stripEnd self.ok(end0 == start_frame + frames, " housekeeping() strip0") action_name_2 = 'step side R to L' man.add_motion(action_name_2, frames) man.housekeeping() blender_frame(25) self.ok(abs(man.model.LocY - (-1.007)) < 0.01, ' add_location moves obj') ## ... # woman walking backwards ... used for manual testing if (False): woman = Model('woman') woman.reset() # woman makes 3 steps; let's see if object follows backward motion # ... looking at blender window, appears to work, # but RotZ jumps between +180 and -180, # and ipo points are left selected. steps = ('step back L to R', 'step shift R to L', 'step back L to R') for s in steps: woman.add_motion(s, frames) woman.housekeeping() # ... # couple walk sequence woman = Model('woman') seq = ['forward', 'side', 'shift', 'back', 'side', 'shift'] man.walk_sequence('L', seq, woman)
def toBlenderQuat(q): #print "\nq = ", q return bMath.Quaternion(q[3], q[0], q[1], q[2])
def toBlenderQuat(self, q): q = q.inverse().normalize() return bMath.Quaternion(q[3], q[0], q[1], q[2])