예제 #1
0
 def getCallback(self, callbackname, time, args):
     if callbackname == "motion":
         return motionCallback(time, args, 1)
     if callbackname == "queue_motion":
         return motionCallback(time, args, 0)
     if callbackname == "backgroundmotion":
         return backgroundMotionCallback(time, args)
     if callbackname == "stop":
         return stopMotionCallback(time, args)
     if callbackname == "sound":
         if self.soundEngine != None:
             return soundCallback(time, args, self.soundEngine)
         else:
             print "could not load sounds as audio playback is not supported, please install pyaudio or  pymedia"
             return None
     if callbackname == "say":
         if self.soundEngine != None:
             return sayCallback(time, args, self.soundEngine)
         else:
             print "could not load sounds as audio playback is not supported, please install pyaudio or  pymedia"
             return None
     if callbackname == "event":
         return eventCallback(time, args)
     if callbackname == "expression":
         retVal = None
         if self.faceMotion == None:
             self.faceMotion = Piavca.KeyframeMotion(1)
             retVal = motionCallback(0.0, (self.faceMotion, ))
         trackId = Piavca.Core.getCore().getExpressionId(args[0])
         if self.faceMotion.isNull(trackId):
             self.faceMotion.addFloatTrack(trackId, 0.0)
         self.faceMotion.setFloatKeyframe(trackId, time, float(args[1]))
         return retVal
예제 #2
0
def readMotions(motions):
    mots = []
    element_list = []
    child_motion_list = []
    for motion in motions:
        if motion.nodeType == minidom.Node.ELEMENT_NODE:
            #print "current node", motion.nodeName
            if motion.nodeName == "Avatar":
                name = str(motion.getAttribute("name"))
                if name == "":
                    name = str(motion.getAttribute("Name"))
                if name == "":
                    print "Avatar statement without a name"

                position = str(motion.getAttribute("position"))
                if position == "":
                    position = str(motion.getAttribute("Position"))
                if position != "":
                    position = str(position).strip()
                    position = stringToValueList(position)
                    position = ValueListToVec(position)
                else:
                    position = Piavca.Vec()

                rotation = str(motion.getAttribute("rotation"))
                if rotation == "":
                    rotation = str(motion.getAttribute("Rotation"))
                if rotation != "":
                    rotation = str(rotation).strip()
                    rotation = stringToValueList(rotation)
                    rotation = ValueListToQuat(rotation)
                else:
                    rotation = Piavca.Quat()

                avatar = Piavca.Avatar(name)
                #print "loaded avatar", name, position, rotation
                avatar.setRootPosition(position)
                avatar.setRootOrientation(rotation)
                for child in motion.childNodes:
                    if child.nodeType != minidom.Node.ELEMENT_NODE:
                        continue
                    if child.nodeName != "Event":
                        #print "expected a Event statement as a child of an avatar but got", child.nodeName
                        continue
                    name = str(child.getAttribute("name"))
                    if name == "":
                        name = str(child.getAttribute("Name"))
                    if name == "":
                        print "Event statement without a name"
                    Piavca.addEvent(avatar, name)

            elif motion.nodeName == "Motion":
                #print "found a motion statement", motion
                for i in range(motion.attributes.length):
                    #print motion.attributes.item(i).name, motion.attributes.item(i).nodeValue
                    unknownAttrs = []
                    if motion.attributes.item(i).name != "name":
                        unknownAttrs.append(
                            (motion.attributes.item(i).name,
                             motion.attributes.item(i).nodeValue))
                name = str(motion.getAttribute("name"))
                if name == "":
                    name = str(motion.getAttribute("Name"))
                if name == "":
                    print "Motion statement without a name"
                else:
                    #print "*"+name+"*"
                    mot = Piavca.getMotion(name)
                    #print mot
                    if mot == None:
                        print "could not find motion", name
                    else:
                        mots.append((mot, str(motion.nodeName), unknownAttrs))
                        #print "mots", mots

            elif motion.nodeName == "File":
                name = str(motion.getAttribute("name"))
                filename = str(motion.getAttribute("filename"))
                Paivca.loadMotion(name, filename)

            elif motion.nodeName == "Keyframes":
                for i in range(motion.attributes.length):
                    #print motion.attributes.item(i).name, motion.attributes.item(i).nodeValue
                    unknownAttrs = []
                    if motion.attributes.item(i).name != "name":
                        unknownAttrs.append(
                            (motion.attributes.item(i).name,
                             motion.attributes.item(i).nodeValue))
                name = str(motion.getAttribute("name"))
                mot = Piavca.KeyframeMotion()
                mot.thisown = False
                for child in motion.childNodes:
                    if child.nodeType != minidom.Node.ELEMENT_NODE:
                        continue
                    if child.nodeName != "Key":
                        print "expected a key statement as a child of a keyframe motion but got", child.nodeName
                        continue
                    key_type = str(child.getAttribute("type"))
                    key_value = str(child.getAttribute("value"))
                    key_time = float(child.getAttribute("time"))
                    key_joint = str(child.getAttribute("joint"))
                    if key_type == "FLOAT" or key_type == "Float" or key_type == "float":
                        key_joint = Piavca.Core.getCore().getExpressionId(
                            key_joint)
                    else:
                        key_joint = Piavca.Core.getCore().getJointId(key_joint)
                    if key_joint == Piavca.Core.getCore().nullId:
                        raise ValueError("Unknown Joint Id " +
                                         str(child.getAttribute("joint")))
                        Piavca.Core.getCore().addJointNameSet([key_joint])
                    if key_type == "FLOAT" or key_type == "Float" or key_type == "float":
                        if mot.isNull(key_joint):
                            mot.addFloatTrack(key_joint, 0.0)
                        key_value = float(key_value)
                        mot.setFloatKeyframe(key_joint, float(key_time),
                                             key_value)
                    if key_type == "VEC" or key_type == "Vec" or key_type == "vec":
                        if mot.isNull(key_joint):
                            mot.addVecTrack(key_joint, Piavca.Vec())
                        valuelist = stringToValueList(key_value)
                        key_value = ValueListToVec(valuelist)
                        mot.setVecKeyframe(key_joint, float(key_time),
                                           key_value)
                    if key_type == "QUAT" or key_type == "Quat" or key_type == "quat":
                        if mot.isNull(key_joint):
                            mot.addQuatTrack(key_joint, Piavca.Quat())
                        valuelist = stringToValueList(key_value)
                        key_value = ValueListToQuat(valuelist)
                        mot.setQuatKeyframe(key_joint, float(key_time),
                                            key_value)
                if name != "":
                    Piavca.loadMotion(name, mot)

            else:
                try:
                    #print "other motion types", motion.nodeName
                    cl = getattr(Piavca, motion.nodeName)
                    if type(cl) == types.TypeType and issubclass(
                            cl, Piavca.Motion):
                        #print "found motion type", motion.nodeName
                        mot = cl()

                        storedMotName = mot.getName()
                        storedMotStr = str(mot)
                        if mot == None:
                            raise "could not create motion " + motion.nodeName
                            continue
                        mot.thisown = False
                        unknownAttrs = []
                        for i in range(motion.attributes.length):
                            if str(
                                    motion.attributes.item(i).name
                            ) == "name" or str(
                                    motion.attributes.item(i).name) == "Name":
                                name = str(motion.attributes.item(i).nodeValue)
                                #print "========================motion name", name
                                if name != "":
                                    Piavca.loadMotion(name, mot)
                                continue
                            attrName = motion.attributes.item(i).name
                            attrValue = motion.attributes.item(i).nodeValue
                            #print attrName, attrValue
                            if not setAttribute(mot, attrName, attrValue):
                                unknownAttrs.append((attrName, attrValue))
                        child_motion_list.append((mot, motion.childNodes))

                        mot.create()
                        mots.append((mot, str(motion.nodeName), unknownAttrs))
                    else:
                        raise str(
                            motion.nodeName
                        ) + " is not a motion type, its type is: " + str(
                            type(cl))
                        # add to a "add list" instead, parse attributes into a dictionary
                except AttributeError, e:
                    print "Attribute Error:", e

                    elementName = motion.nodeName
                    attrList = []
                    for i in range(motion.attributes.length):

                        attrName = motion.attributes.item(i).name
                        attrValue = motion.attributes.item(i).nodeValue
                        attrList.append((attrName, attrValue))
                    element_list.append((elementName, attrList))