def render(self): diameter = 4.0 sz = 2.125 / diameter base_object = helpers.infer_primitive(random.choice(self.PRIMITIVES), location=(100, 100, 100), radius=sz) latitude = 16 longitude = latitude * 2 invlatitude = 1.0 / (latitude - 1) invlongitude = 1.0 / (longitude - 1) iprc = 0.0 jprc = 0.0 phi = 0.0 theta = 0.0 invfcount = 1.0 / (self.NUMBER_OF_FRAMES - 1) # Animate center of the sphere. center = Vector((0.0, 0.0, 0.0)) startcenter = Vector((0.0, -4.0, 0.0)) stopcenter = Vector((0.0, 4.0, 0.0)) # Rotate cubes around the surface of the sphere. pt = Vector((0.0, 0.0, 0.0)) rotpt = Vector((0.0, 0.0, 0.0)) # Change the axis of rotation for the point. baseaxis = Vector((0.0, 1.0, 0.0)) axis = Vector((0.0, 0.0, 0.0)) # Slerp between two rotations for each cube. startrot = Quaternion((0.0, 1.0, 0.0), pi) stoprot = Quaternion((1.0, 0.0, 0.0), pi * 1.5) currot = Quaternion() for i in range(0, latitude, 1): iprc = i * invlatitude phi = pi * (i + 1) * invlatitude rad = 0.01 + sz * abs(sin(phi)) * 0.99 pt.z = cos(phi) * diameter for j in range(0, longitude, 1): jprc = j * invlongitude theta = TWOPI * j / longitude pt.y = center.y + sin(phi) * sin(theta) * diameter pt.x = center.x + sin(phi) * cos(theta) * diameter current = helpers.duplicate_object(base_object) current.location = pt current.name = 'Object ({0:0>2d}, {1:0>2d})'.format(i, j) current.data.name = 'Mesh ({0:0>2d}, {1:0>2d})'.format(i, j) current.rotation_euler = (0.0, phi, theta) helpers.assign_material( current, helpers.random_material(self.MATERIALS_NAMES)) axis = self.vecrotatex(theta, baseaxis) currot = startrot center = startcenter for f in range(0, self.NUMBER_OF_FRAMES, 1): fprc = f / (self.NUMBER_OF_FRAMES - 1) osc = abs(sin(TWOPI * fprc)) bpy.context.scene.frame_set(f) center = startcenter.lerp(stopcenter, osc) current.location = helpers.rotate_vector( TWOPI * fprc, axis, pt) current.keyframe_insert(data_path='location') currot = startrot.slerp(stoprot, jprc * fprc) current.rotation_euler = currot.to_euler() current.keyframe_insert(data_path='rotation_euler')
def quaternionMix(t, v1, v2): q1 = Quaternion(v1) q2 = Quaternion(v2) q = q1.slerp(q2, max(0, min(t, 1))) return q
currframe = bpy.context.scene.frame_start currot = startrot center = startcenter for f in range(0, fcount, 1): fprc = f * invfcount osc = abs(sin(TWOPI * fprc)) bpy.context.scene.frame_set(currframe) # Animate location. vecrotate(TWOPI * fprc, axis, pt, rotpt) center = startcenter.lerp(stopcenter, osc) rotpt = rotpt + center current.location = rotpt current.keyframe_insert(data_path='location') # Animate rotation. currot = startrot.slerp(stoprot, jprc * fprc) current.rotation_euler = currot.to_euler() current.keyframe_insert(data_path='rotation_euler') # Animate color. #mat.diffuse_color = colorsys.hsv_to_rgb(jprc, osc, 1.0) #mat.keyframe_insert(data_path='diffuse_color') convertedColor2 = colorsys.hsv_to_rgb(jprc, osc, 1.0) current.color = [convertedColor2[0], convertedColor2[1], convertedColor2[2], 1.0] current.keyframe_insert(data_path='color') currframe += fincr
def frame_pre(scene): #if not bpy.context.screen.is_animation_playing: #bpy.app.handlers.frame_change_pre.remove(frame_pre) obj = bpy.context.object if obj and obj.type == 'ARMATURE': awc = obj.data.aut_walk_cycle try: torso_obj = obj.pose.bones[awc.torso] l_foot_ik = obj.pose.bones[awc.l_foot_ik] r_foot_ik = obj.pose.bones[awc.r_foot_ik] except Exception as e: print("ERROR", e) bpy.app.handlers.frame_change_pre.remove(frame_pre) return amp = awc.amp openness = -awc.openness anticipation = 1 - awc.anticipation fo_rot = awc.foot_rot frame = scene.frame_current up_axis = awc.up_axis.copy() up_axis.rotate(torso_obj.matrix) side_axis = awc.side_axis.copy() side_axis.rotate(torso_obj.matrix) mat_l = obj.data.bones[awc.l_foot_ik].matrix_local.copy() mat_r = obj.data.bones[awc.r_foot_ik].matrix_local.copy() if not awc.anim: front_axis = awc.front_axis.copy() front_axis.rotate(torso_obj.matrix) #print(up_axis) step = awc.step / 2 #(anticipation - .5) frequency = awc.frequency fr = 2 * pi * frame / frequency cf = cos(fr / 2) sf = sin(fr / 2) cff = cf**2 mod = (frame / frequency - .5) % 2 f = mod >= 1 up = up_axis * cff * amp front = ((sf / 2) * step) * front_axis ant = ((anticipation - .5) * step) * front_axis side = side_axis * openness fo_rot = fo_rot * side_axis foot = l_foot_ik, r_foot_ik mat = mat_l, mat_r sign = 2 * f - 1 mod -= f mloc = Matrix.Translation( ((ant + up + sign * (front + (cff - 1) * side)))) #*mat[f])) mrot = Quaternion((mod - 1 + anticipation) * fo_rot).to_matrix() foot[f].matrix = mloc * mrot.to_4x4() * mat[f] front = front_axis * (.5 - mod) * step mloc = Matrix.Translation((ant + front + sign * side)) #*mat[f-1]) if 2 * mod <= anticipation: mrot = Quaternion( (anticipation - 2 * mod) * fo_rot).to_matrix() matr = mloc * mrot.to_4x4() elif 3 * mod - 2 >= anticipation: mrot = Quaternion( (anticipation - (3 * mod - 2)) * fo_rot).to_matrix() matr = mloc * mrot.to_4x4() else: matr = mloc foot[f - 1].matrix = matr * mat[f - 1] m_cache = {} for col_bone in awc.new_bones: if col_bone.name and col_bone.show: name = col_bone.name bone = obj.pose.bones[name] bone_dat = obj.data.bones[col_bone.name] m_local = bone_dat.matrix_local #m_ch = m_cache.get(name, Matrix.Identity(4)) m_cache[name] = m_cache.get(name, m_local) if col_bone.seq_type == 'LR': fac = sf / 2 + .5 elif col_bone.seq_type == 'M': fac = cf / 2 + .5 else: # if col_bone.seq_type == 'ES' fac = cff #mloc = Matrix.Translation(col_bone.loc1.lerp(col_bone.loc2, fac)) mrot = col_bone.qua1.slerp(col_bone.qua2, fac).to_matrix() m_cache[name] *= mrot.to_4x4() loc = col_bone.loc1.lerp(col_bone.loc2, fac) if bone_dat.use_local_location: loc.rotate(m_local) m_cache[name].translation += loc parent = bone.parent.matrix * bone_dat.parent.matrix_local.inverted( ) if col_bone.add_torso: m_cache[name].translation += parent.translation bone.matrix = m_cache[name] else: bone.matrix = parent * m_cache[name] else: # awc.anim: fr_prev = frame fs = obj.data['frame_steps'].to_dict() sfs = sorted(fs, key=lambda x: int(x)) for i, frst in enumerate(sfs): fr = int(frst) if fr > frame: left = i % 2 vec2 = Vector(fs[sfs[i - 1]][0]) vec3 = Vector(fs[sfs[i]][0]) if i >= 2: vec1 = Vector(fs[sfs[i - 2]][0]) else: vec1 = vec2 sign = (2 * left - 1) rang = (frame - fr_prev) / (fr - fr_prev) # = mod%1 # | / # | / # |/___ cf = 4 * rang * (1 - rang) * sign # or sin(rang*pi) # | __ # | / \ # |/____\_ rq = Quaternion(fs[sfs[i - 1]][1]) rrot = rq.slerp(Quaternion(fs[sfs[i]][1]), rang) _mrot = rrot.to_matrix() rvec = Matrix.Translation((vec2.lerp(vec3, rang))) mat_global = rvec * _mrot.to_4x4() torso_dat = obj.data.bones[awc.torso] torso_obj.matrix = mat_global * torso_dat.matrix_local ant = rang + anticipation / 2 # antecip real vec2 = vec2.lerp(vec3, anticipation) if ant <= 1: loc = vec1.lerp(vec3, ant) else: try: vec5 = Vector(fs[sfs[i + 2]][0]) loc = vec3.lerp(vec5, ant - 1) except: loc = vec1.lerp(vec3, ant) mt_vec2 = Matrix.Translation(vec2) mat_loc = Matrix.Translation(loc) up2 = up_axis * cf * amp mrot = Matrix.Rotation( (ant - 1) * fo_rot, 3, side_axis) * _mrot mrot.resize_4x4() lat2 = side_axis * openness if left: m_vec = Matrix.Translation((1 - cf) * lat2 + up2) l_foot_ik.matrix = mat_loc * m_vec * mrot * mat_l mt_r = Matrix.Translation(-lat2) if 2 * rang <= anticipation: mr_r = Matrix.Rotation( (anticipation - 2 * rang) * fo_rot, 3, side_axis) * _mrot elif 3 * rang - 2 >= anticipation: mr_r = Matrix.Rotation( (anticipation - (3 * rang - 2)) * fo_rot, 3, side_axis) * _mrot else: mr_r = _mrot r_foot_ik.matrix = mt_vec2 * mt_r * mr_r.to_4x4( ) * mat_r else: m_vec = Matrix.Translation(-(cf + 1) * lat2 - up2) r_foot_ik.matrix = mat_loc * m_vec * mrot * mat_r mt_l = Matrix.Translation(lat2) if 2 * rang <= anticipation: mr_l = Matrix.Rotation( (anticipation - 2 * rang) * fo_rot, 3, side_axis) * _mrot elif 3 * rang - 2 >= anticipation: mr_l = Matrix.Rotation( (anticipation - (3 * rang - 2)) * fo_rot, 3, side_axis) * _mrot else: mr_l = _mrot l_foot_ik.matrix = mt_vec2 * mt_l * mr_l.to_4x4( ) * mat_l m_cache = {} for col_bone in awc.new_bones: if col_bone.name and col_bone.show: name = col_bone.name bone = obj.pose.bones[name] bone_dat = obj.data.bones[name] #m_local = m_cache.get(name, bone_dat.matrix_local) m_local = bone_dat.matrix_local m_cache[name] = m_cache.get(name, m_local) loc1 = col_bone.loc1 loc2 = col_bone.loc2 rot1 = col_bone.qua1 rot2 = col_bone.qua2 if col_bone.seq_type == 'LR': sf = cos(rang * pi) * sign # |_ _ # | \ / # |__|____|__ # | | | # | \__/ fac = sf / 2 + .5 loc = loc1.lerp(loc2, fac) rot = rot1.slerp(rot2, fac) elif col_bone.seq_type == 'M': fac = .5 - cf / 2 loc = loc1.lerp(loc2, fac) rot = rot1.slerp(rot2, fac) else: #col_bone.seq_type == 'ES': fac = cf**2 loc = loc1.lerp(loc2, fac) rot = rot1.slerp(rot2, fac) mrot = rot.to_matrix() #mloc = Matrix.Translation(vec) m_cache[name] *= mrot.to_4x4() if bone_dat.use_local_location: loc.rotate(m_local) m_cache[name].translation += loc if col_bone.add_torso: bone.matrix = mat_global * m_cache[name] else: parent = bone.parent.matrix * bone_dat.parent.matrix_local.inverted( ) bone.matrix = parent * m_cache[name] break fr_prev = fr else: bpy.app.handlers.frame_change_pre.remove(frame_pre) scene.awc_is_preview = False