예제 #1
0
        Piavca.setUpEventsGUI()

    print "loaded avatar"

    if len(sys.argv) > 2:
        motion_name = sys.argv[2]
    else:
        motnames = Piavca.Core.getCore().getMotionNames()
        print motnames
        dialog_return = singleChoiceDialog(lst=["None"] + motnames)
        motion_name = dialog_return.selection

    mot = Piavca.Core.getCore().getMotion(str(motion_name))
    if mot != None:
        mot.Reference()
        avatar.play_motion(Piavca.LoopMotion(mot))
    avatar.play_motion(mot)

    avatar.showMotionAtTime(Piavca.getTime())

    print "loaded motion"

    app.getCanvas().setAvatar(avatar)
    app.getCanvas().initCameraPosition()

app.getCanvas().setClearColour(1.0, 1.0, 1.0)

#app = MyApp()

#import Piavca.redirect
#Piavca.redirect.redirect()
예제 #2
0
def InterruptableSequence(seq,
                          interruptions,
                          fps=20,
                          threshold=6.5,
                          window=0.5):
    expmaps = {}

    # create a set of exponential maps
    # what this does is find the average joint
    # rotations of all of start and end poses of the
    # interruptions, and get them in a form where
    # its easy to calculate the distance of a new quaternion to
    # them
    for j in Piavca.joints(seq):
        # don't take the root into account as we reposition the motion
        if j == Piavca.root_orientation_id or j == Piavca.root_position_id:
            continue
        if seq.isNull(j):
            continue
        joint_type = seq.getTrackType(j)
        # the joint orientations of
        # this joint at the start and end
        # frames of the interruptions
        for t in (Piavca.FLOAT_TYPE, Piavca.VEC_TYPE, Piavca.QUAT_TYPE):
            if not (t & joint_type):
                continue
            extremeFrames = []
            for m in interruptions:
                if m.isNull(j):
                    continue
                if not (m.getTrackType(j) & t):
                    continue
                start_time = m.getStartTime()
                extremeFrames.append(Piavca.getValAtTime(m, j, start_time, t))
                end_time = m.getEndTime()
                extremeFrames.append(Piavca.getValAtTime(m, j, end_time, t))

            if len(extremeFrames) == 0:
                expmaps[(j, t)] = None
            elif t == Piavca.QUAT_TYPE:
                expmaps[(j, t)] = Piavca.ExpMap.TangentSpace(extremeFrames)
            elif t == Piavca.VEC_TYPE:
                expmaps[(j, t)] = sum(extremeFrames, Piavca.Vec()) / float(
                    len(extremeFrames))
            else:
                print len(extremeFrames), extremeFrames
                expmaps[(j,
                         t)] = sum(extremeFrames) / float(len(extremeFrames))

    d_minus = None
    d = None
    d_plus = None

    # find local minima of the distance function
    # if its lower than a threshold then we add it to the set of
    # of possible transition points
    minima = [seq.getStartTime()]
    values = []
    #print "start time", seq.getStartTime(), "end time", seq.getEndTime()
    for i in range(int(seq.getStartTime() * fps),
                   int(seq.getEndTime() * fps) - 1):
        d_plus = calculateDistance(seq, float(i + 1) / fps, expmaps)
        print d, d_plus, threshold
        if d:
            if d < d_plus:
                if d_minus != None and d < d_minus:
                    print float(i) / fps, d, d_plus, threshold, 4.0 * window
                    if d < threshold:
                        if len(minima) == 0 or (float(i) / fps -
                                                minima[-1]) > 4.0 * window:
                            values.append(d)
                            minima.append(float(i) / fps)
        d_minus = d
        d = d_plus

    minima.append(seq.getEndTime())

    values.sort(reverse=False)
    print "values", values
    print "minima", minima
    numMinima = len(minima)

    # create the motions
    # a submotion for each transition point
    # a sequential choice motion that plays the original motion
    # a choice motion with default that allows us to interrupt
    # and finally loop in all
    # we add "window" to the end time so that it works with
    # the smooth transitioning function
    submots = [
        Piavca.SubMotion(seq, start, end)
        for start, end in zip(minima[:-1], minima[1:])
    ]
    print submots
    choice1 = Piavca.SequentialChoiceMotion()
    choice1.setSmooth(False)
    choice1.setAccumulateRoot(False)
    choice1.setWindowLength(window)
    for mot in submots:
        print mot
        choice1.addMotion(mot)
    loop1 = Piavca.LoopMotion(choice1)
    choice2 = Piavca.ChoiceMotionWithDefault()
    choice2.setWindowLength(window)
    choice2.addMotion(loop1)
    for mot in interruptions:
        print mot
        choice2.addMotion(mot)
    loop2 = Piavca.LoopMotion(choice2)
    return loop2, numMinima
예제 #3
0
def MotionGraph(motions, pcFile=None, fps=20, num_quants=6, threshold=1.0):

    if pcFile != None and pcFile != "":
        print "found pc file", pcFile
        pcs = Piavca.PCA()
        pcs.Load(pcfile)
    else:
        pcs = Piavca.PCA()
        pcs.setUseVels(True)
        pcs.do_analysis(motions, fps)

    projectedMotions = []
    expmaps = []
    # project all the motions onto the pcs
    for motion in motions:
        projectedMotions.append([
            pw[1] for pw in pcs.projectMotion(motion, frames_per_second=fps)
        ])  #, file_extension="weights_out_"+ str(motion.getStart()))])
        expmaps.append(pcs.getExpMaps(motion, frames_per_second=fps))
        #f = open("weights_out_"+ str(motion.getStart()) +".csv", "w")
        #for weightset in projectedMotions[-1]:
        #	for w in weightset:
        #		print >> f, w, ",",
        #	print >> f, ""
        #f.close()

    if pcs.quants == None:
        print "calculating cluster centres"
        weights = []
        for projmot in projectedMotions:
            weights = weights + projmot
        weights = scipy.array(weights)
        quantizedWeights = pcs.KMeans(weights, num_quants)

    num_quants = pcs.numQuants()

    quants = []
    for projmot in projectedMotions:
        quants.append(pcs.VectorQuantizeWithDistance(scipy.array(projmot)))

    minima = []
    cluster_counts = [0 for i in range(num_quants)]

    for motNum, (motQuants, motDists) in enumerate(quants):
        minima.append([])

        currentQuant = None
        currentMinimum = None
        currentMinimumVal = 1000000.0
        added = False
        for i in range(1, len(motQuants) - 1):
            cluster_counts[motQuants[i]] += 1
            if motQuants[i] != currentQuant:
                if currentQuant != None and not added:
                    if currentMimimumVal < threshold:
                        minima[motNum].append((currentQuant, currentMinimum))
                added = False
                currentQuant = motQuants[i]
                currentMinimum = float(i) / fps
                currentMimimumVal = motDists[i]
            else:
                if motDists[i] < currentMinimumVal:
                    currentMinimum = float(i) / fps
                    currentMimimumVal = motDists[i]
            if motQuants[i -
                         1] == motQuants[i] and motQuants[i] == motQuants[i +
                                                                          1]:
                if motDists[i - 1] > motDists[i] and motDists[i] < motDists[i +
                                                                            1]:
                    if motDists[i] < threshold:
                        minima[motNum].append((motQuants[i], float(i) / fps))
                        added = True

        if currentQuant != None and not added:
            minima[motNum].append((currentQuant, currentMinimum))

    # create the motions

    random_choices = [Piavca.RandomChoiceMotion() for i in range(num_quants)]
    #for i, rc in enumerate(random_choices):
    #	rc.setName("Random_choice_"+str(i))
    transitions = []

    print len(minima)
    print ""

    num_transitions = [[0 for i in range(num_quants)]
                       for j in range(num_quants)]

    for motNum, mot_minima in enumerate(minima):
        for start, end in zip(mot_minima[:-1], mot_minima[1:]):
            print start, end
            start_node = start[0]
            end_node = end[0]
            num_transitions[start_node][end_node] += 1
            submot = Piavca.SubMotion(motions[motNum], start[1], end[1])
            submot.setName(
                str(motNum) + "_" + str(start[1]) + "_" + str(end[1]))
            transitions.append((submot.getName(), end_node))
            random_choices[start_node].addMotion(submot)
            print transitions[-1], random_choices[start_node].getNumMotions()

    for i, rc in enumerate(random_choices):
        print "random choice", i, rc.getNumMotions()

    #mo_graph = Piavca.MotionGraph()
    #mo_graph.addEvent("default")

    #for rc in random_choices:
    #	mo_graph.addMotion(rc)

    #for motName, node in transitions:
    #	print motName, type(motName), node, type(node)
    #	mo_graph.addNextNode("default", motName, int(node))

    mo_graph = Piavca.EventMapChoice()
    mo_graph.setResetOnEvent(False)

    # set the mo_graph as a listener for all random choices so that it
    # receieves their events
    for rc in random_choices:
        rc.addListener(mo_graph)

    # add the random choices to the motion graph
    # include an intermediary choice motion which handles
    # events that change the probabilities at a node
    for rc in random_choices:
        eventChoice = Piavca.ChoiceMotion()
        eventChoice.setResetOnPlay(True)
        rc.setName("default")
        eventChoice.addMotion(rc)
        mo_graph.addMotion(eventChoice)

    for motName, node in transitions:
        print motName, type(motName), node, type(node)
        mo_graph.addMapItem(motName, int(node))

    for i in range(num_quants):
        for j in range(num_quants):
            print "tranisitions", i, j, num_transitions[i][j]

    #mo_graph.setSmooth(False)
    mo_graph.setAccumulateRoot(False)

    loop = Piavca.LoopMotion(mo_graph)
    return loop