Beispiel #1
0
def String2MAvatarPosture(lines, avatarID = "Avatar"):
    parent_map = {None : None}
    joints = []
    name = ""
    pos = None
    rot = None
    channels = []
    parentJointName = None
    for i in range(len(lines)):
        line = lines[i].strip().split(" ")
        if line[0] == "ROOT" or line[0] == "JOINT":
            name = lines[i].strip().split(" ")[1]
            if name == "PelvisCenter":
                name = "PelvisCentre"
        elif line[0] == "OFFSET":
            pos = MVector3(float(line[1]), float(line[2]), float(line[3]))
        elif line[0] == "ROTATION":
            rot = MQuaternion(float(line[4]), float(line[1]), float(line[2]), float(line[3]))
        elif line[0] == "CHANNELS":
            channels = [MChannel._NAMES_TO_VALUES[line[j]] for j in range(1, len(line))]
        joint = MJoint(name, MJointType._NAMES_TO_VALUES[name], pos, rot, channels, parentJointName)
        parent_map[name] = parentJointName
        parentJointName = name
        joints.append(joint)
        if line[0] == "}":
            name = parentJointName
            parentJointName = parent_map[parentJointName]
    
    return MAvatarPosture(avatarID, joints)
Beispiel #2
0
def QMultiply(left : MQuaternion, right : MQuaternion):
    x = left.W * right.X + left.X * right.W + left.Y * right.Z - left.Z * right.Y
    y = left.W * right.Y + left.Y * right.W + left.Z * right.X - left.X * right.Z
    z = left.W * right.Z + left.Z * right.W + left.X * right.Y - left.Y * right.X
    w = left.W * right.W - left.X * right.X - left.Y * right.Y - left.Z * right.Z

    result = MQuaternion(x, y, z, w)
    return result
Beispiel #3
0
def Inverse(quaternion : MQuaternion):
    inverted = MQuaternion(-quaternion.X, -quaternion.Y, -quaternion.Z, quaternion.W)
    norm = quaternion.X * quaternion.X + quaternion.Y * quaternion.Y + quaternion.Z * quaternion.Z + quaternion.W * quaternion.W

    inverted.X /= norm
    inverted.Y /= norm
    inverted.Z /= norm
    inverted.W /= norm
    return inverted
Beispiel #4
0
def NewJson2MAvatarPosture(data, avatarID = "Avatar"):
    joints = []
    for j in data["Joints"]:
        name = j["ID"]
        type = j["Type"]
        pos = MVector3(j["Position"]["X"], j["Position"]["Y"], j["Position"]["Z"])
        #rot = MQuaternion(j["Rotation"]["X"], j["Rotation"]["Y"], j["Rotation"]["Z"], j["Rotation"]["W"])
        rot = MQuaternion(0,0,0,1)
        joints.append(MJoint(name, type, pos, rot))
    return MAvatarPosture(avatarID, joints)
Beispiel #5
0
def rotation_from_json(v):
    x = 0
    if "X" in v:
        x = v["X"]
    y = 0
    if "Y" in v:
        y = v["Y"]
    z = 0
    if "Z" in v:
        z = v["Z"]
    w = 1
    if "W" in v:
        w = v["W"]
    return MQuaternion(x, y, z, w)
Beispiel #6
0
def JSON2MAvatarPosture(data):
    data = {}
    with open(filepath, "r") as f:
        data = json.load(f)
    name = data["1"]["str"]
    jointlist = []
    for i in range(2, len(data["2"]["lst"])):
        bonename = data["2"]["lst"][i]["1"]["str"]
        bonetype = data["2"]["lst"][i]["2"]["i32"]
        bonetype = MJointType._VALUES_TO_NAMES[bonetype]
        
        vd = data["2"]["lst"][i]["3"]["rec"]
        vector = MVector3(vd["1"]["dbl"], vd["2"]["dbl"], vd["3"]["dbl"])


        qd = data["2"]["lst"][i]["4"]["rec"]
        quat = MQuaternion(qd["1"]["dbl"], qd["2"]["dbl"], qd["3"]["dbl"], qd["4"]["dbl"])

        jointlist.append(MJoint(bonename, bonetype, vector, quat))
    return MAvatarPosture(name, jointlist)
Beispiel #7
0
def Slerp(xfrom : MQuaternion, yto : MQuaternion, t, useShortestPath = True):
    lengthFrom = Length(xfrom)
    lengthTo = Length(yto)
    Scale(xfrom,1.0 / float(lengthFrom) )
    Scale(yto,1.0 / float(lengthTo) )

    cosOmega = xfrom.X * yto.X + xfrom.Y * yto.Y + xfrom.Z * yto.Z + xfrom.W * yto.W

    if useShortestPath is True:
        if cosOmega < 0.0:
            cosOmega = -cosOmega
            yto.X = -yto.X
            yto.Y = -yto.Y
            yto.Z = -yto.Z
            yto.W = -yto.W
    else:
        if cosOmega < -1.0:
            cosOmega = -1.0
    if cosOmega > 1.0:
        cosOmega = 1.0
    
    maxCosine = 1.0 - 1e-6
    minCosine = 1e-6 - 1.0

    if cosOmega > maxCosine:
        scalexFrom = 1.0 - t
        scaleyTo = t
    elif cosOmega < minCosine:
        yto = MQuaternion(-xfrom.Y, xfrom.X, -xfrom.W, xfrom.Z)
        theta = t * math.pi
        scalexFrom = math.cos(theta)
        scaleyTo = math.sin(theta)
    else:
        omega = math.acos(cosOmega)
        sinOmega = math.sqrt(1.0 - cosOmega * cosOmega)
        scalexFrom = math.sin((1.0 - t) * omega) / sinOmega
        scaleyTo = math.sin(t * omega) / sinOmega
    
    lengthOut = lengthFrom * math.pow(lengthTo / lengthFrom, t)
    scalexFrom *= lengthOut
    scaleyTo *= lengthOut

    return MQuaternion(scalexFrom * xfrom.X + scaleyTo * yto.X,
                        scalexFrom * xfrom.Y + scaleyTo * yto.Y,
                        scalexFrom * xfrom.Z + scaleyTo * yto.Z,
                        scalexFrom * xfrom.W + scaleyTo * yto.W)
Beispiel #8
0
def FromEuler(euler : MVector3):
    xOver2 = euler.X * Deg2Rad * 0.5
    yOver2 = euler.Y * Deg2Rad * 0.5
    zOver2 = euler.Z * Deg2Rad * 0.5

    sinXOver2 = math.sin(xOver2)
    cosXOver2 = math.cos(xOver2)
    sinYOver2 = math.sin(yOver2)
    cosYOver2 = math.cos(yOver2)
    sinZOver2 = math.sin(zOver2)
    cosZOver2 = math.cos(zOver2)

    result = MQuaternion()
    result.X = cosYOver2 * sinXOver2 * cosZOver2 + sinYOver2 * cosXOver2 * sinZOver2
    result.Y = sinYOver2 * cosXOver2 * cosZOver2 - cosYOver2 * sinXOver2 * sinZOver2
    result.Z = cosYOver2 * cosXOver2 * sinZOver2 - sinYOver2 * sinXOver2 * cosZOver2
    result.W = cosYOver2 * cosXOver2 * cosZOver2 + sinYOver2 * sinXOver2 * sinZOver2

    return result
Beispiel #9
0
def Clone(quaternion : MQuaternion):
    if not quaternion is None:
        return MQuaternion(quaternion.X, quaternion.Y, quaternion.Z, quaternion.W)
    else:
        return None
Beispiel #10
0
def ToMQuaternion(values):
    return MQuaternion(values[0], values[1], values[2], values[3])
Beispiel #11
0
    "RightMiddleTip", "RightIndexProximal", "RightIndexMeta",
    "RightIndexDistal", "RightRingProximal", "RightRingMeta",
    "RightRingDistal", "RightLittleProximal", "RightLittleMeta",
    "RightLittleDistal", "RightThumbMid", "RightThumbMeta", "RightThumbCarpal"
]


def NewMJoint(id, jtype: MJointType, offset: MVector3, rotation: MQuaternion,
              parentID, channels):
    j = MJoint(id, jtype, offset, rotation)
    j.Parent = parentID
    j.Channels = channels
    return j


identityQ = MQuaternion(0, 0, 0, 1)
defaultJointChannels = [
    MChannel.WRotation, MChannel.XRotation, MChannel.YRotation,
    MChannel.ZRotation
]
defaultRootChannels = [
    MChannel.XOffset, MChannel.YOffset, MChannel.ZOffset, MChannel.WRotation,
    MChannel.XRotation, MChannel.YRotation, MChannel.ZRotation
]
zeroChannels = []

DEFAULT_JOINTS = [
    # 7 joints along the spine (6 animated, 39 channels)
    NewMJoint("PelvisCenter", MJointType.PelvisCentre, MVector3(0, 0, 0),
              identityQ, None, defaultRootChannels),
    NewMJoint("S1L5Joint", MJointType.S1L5Joint, MVector3(0, 0.18, 0),
Beispiel #12
0
def ArrayToMQuaternion(a):
    return MQuaternion(-a[1], a[2], a[3], -a[0])