def next_quaternion(self): """Return the next quaternion as mathutils.Quaternion, raises AssetParseError on error""" x = float(self.next()) y = float(self.next()) z = float(self.next()) w = float(self.next()) quat = Quaternion((w, x, y, z)) if self.are_quaternions_inverted(): quat.invert() return quat
def invert(): scn = bpy.context.scene.objects BoneArray = {} BoneArray['init'] = Bone('init') #アーマチャーを取得 for obj in bpy.context.selected_objects: if obj.type == 'ARMATURE': amt = obj utils.activeObj(amt) for bone in amt.pose.bones: q = Quaternion(bone.rotation_quaternion) q.invert() BoneArray[bone.name] = Bone(bone.name) BoneArray[bone.name].quatanion = q m_world = Matrix(bone.matrix) BoneArray[bone.name].location = Vector( (m_world[0][3], m_world[1][3], m_world[2][3])) BoneArray[bone.name].parent_number = len(bone.parent_recursive) if bone.parent != None: BoneArray[bone.name].parent = bone.parent.name else: BoneArray[bone.name].parent = 'init' #初期マトリックス utils.mode_e() for bone in amt.data.edit_bones: BoneArray[bone.name].initmatrix = Matrix(bone.matrix).to_3x3() utils.mode_o() for b in BoneArray.values(): b.conv_quat_axis() for obj in bpy.context.selected_objects: if obj.type != 'ARMATURE': mesh = obj.data bm = bmesh.new() bm.from_mesh(mesh) boneArray = [] for group in obj.vertex_groups: boneArray.append(group.name) #ウェイト値を取得 vtxarray = [] for v in mesh.vertices: vtx = Vtx() for vge in v.groups: if vge.weight > 0.00001: #ウェイト値0は除外 vtx.getWeight(vge.group, vge.weight, boneArray) #boneArrayから骨名を割り出して格納 vtx.normalize_weight() #ウェイトをノーマライズする vtxarray.append(vtx) #obj = bpy.context.object #シェイプのインデックス #shapekeysList = len(obj.data.shape_keys.key_blocks) spIndex = obj.active_shape_key_index print(spIndex) key = bm.verts.layers.shape.keys()[spIndex] val = bm.verts.layers.shape.get(key) #sk=mesh.shape_keys.key_blocks[key] for v, vtx in zip(bm.verts, vtxarray): delta = v[val] - v.co v[val] = vtx.calc_quat(v[val], BoneArray) #ブレンドシェイプキーの操作 # for key in bm.verts.layers.shape.keys(): # val = bm.verts.layers.shape.get(key) # #print("%s = %s" % (key,val) ) # sk=mesh.shape_keys.key_blocks[key] # #print("v=%f, f=%f" % ( sk.value, sk.frame)) # #print(vtxarray) # for v,vtx in zip(bm.verts,vtxarray): # delta = v[val] - v.co # # if (delta.length > 0): # # print ( "v[%d]+%s" % ( i,delta) ) # #v[val] = vtx.calc_mat( v[val] , boneMatrixArray ,initmatrixarray ,boneLocationArray) # v[val] = vtx.calc_quat( v[val] , BoneArray) # #v[val] = m @ v[val]*0.5 + v[val] * 0.5 bm.to_mesh(obj.data)
class User(device.Sender): def __init__(self, parent, configuration): _configuration = configuration.copy() _configuration['users'] = _configuration['viewer'] self._websocket = None self._matrix = None super(User, self).__init__(parent, _configuration) self._viewer = self.BlenderVR.getUserByName(configuration['viewer']) self._host = configuration['host'] self._available = True # TODO, check if host is a valid one def run(self): try: self._updateMatrix() info = {'matrix' : self._matrix} self.process(info) except Exception as err: self.logger.log_traceback(err) def getName(self): return self._viewer.getName() def getUser(self): return self._viewer def isAvailable(self): return self._available def start(self): try: from websocket import create_connection from mathutils import Matrix self._websocket = create_connection("ws://{0}:8888/".format(self._host)) self._matrix = Matrix.Identity(4) except Exception as err: self.logger.log_traceback(err) def _updateMatrix(self): from mathutils import Quaternion, Matrix import json try: self._websocket.send('n') result = json.loads(self._websocket.recv()) self._matrix = Quaternion((result[7], result[4], result[5], result[6])).to_matrix().to_4x4() position = Matrix.Translation((result[1], result[2], result[3])) self._matrix = position * self._matrix self._matrix.invert() except Exception as err: self.logger.log_traceback(err)