Ejemplo n.º 1
0
def rotations_global(anim):
    """
    Global Animation Rotations
    
    This relies on joint ordering
    being incremental. That means a joint
    J1 must not be a ancestor of J0 if
    J0 appears before J1 in the joint
    ordering.
    
    Parameters
    ----------
    
    anim : Animation
        Input animation
        
    Returns
    -------
    
    points : (F, J) Quaternions
        global rotations for every frame F 
        and joint J
    """

    joints = np.arange(anim.shape[1])
    parents = np.arange(anim.shape[1])
    locals = anim.rotations
    globals = Quaternions.id(anim.shape)

    globals[:, 0] = locals[:, 0]

    for i in range(1, anim.shape[1]):
        globals[:, i] = globals[:, anim.parents[i]] * locals[:, i]

    return globals
Ejemplo n.º 2
0
def orients_global(anim):
    joints = np.arange(anim.shape[1])
    parents = np.arange(anim.shape[1])
    locals = anim.orients
    globals = Quaternions.id(anim.shape[1])

    globals[:, 0] = locals[:, 0]

    for i in range(1, anim.shape[1]):
        globals[:, i] = globals[:, anim.parents[i]] * locals[:, i]

    return globals
Ejemplo n.º 3
0
    def get_pair(self, idxA):
        maskA = np.zeros(shape=self.out_frame, dtype=self.dtype)
        maskB = np.zeros(shape=self.out_frame, dtype=self.dtype)

        scale = self.bvh_scale
        aug_q = Quaternions.id(1)

        if self.scale_augment:
            t = random.gauss(1, self.scale_std)
            t = max(t, 0.1)
            scale *= t

        if self.rot_augment:
            rot = random.random()
            rot = (rot - 0.5) * 2 * np.pi

            aug_q = Quaternions.from_angle_axis(rot, np.array([0, 1, 0])) * aug_q

        quatA, localA, rtjA, skelA = self.get_normalized_data(idxA, scale, aug_q)
        random_slice = get_random_slice(localA, self.out_frame)
        maskA[:np.min([self.out_frame, localA.shape[0]])] = 1.0
        localA = repeat_last_frame(localA[random_slice], self.out_frame)
        rtjA = repeat_last_frame(rtjA[random_slice], self.out_frame)
        skelA = repeat_last_frame(skelA[random_slice], self.out_frame)
        quatA = repeat_last_frame(quatA[random_slice], self.out_frame)

        # get real skeleton and motion
        skelA_name, _, _ = self.train[idxA]
        idxB = np.random.randint(self.__len__())
        while skelA_name == self.train[idxB][0]:
            idxB = np.random.randint(self.__len__())

        quatB, localB, rtjB, skelB = self.get_normalized_data(idxB, scale, aug_q)
        random_slice = get_random_slice(localB, self.out_frame)
        maskB[:np.min([self.out_frame, localB.shape[0]])] = 1.0
        localB = repeat_last_frame(localB[random_slice], self.out_frame)
        rtjB = repeat_last_frame(rtjB[random_slice], self.out_frame)
        skelB = repeat_last_frame(skelB[random_slice], self.out_frame)
        quatB = repeat_last_frame(quatB[random_slice], self.out_frame)

        localA = localA.astype(self.dtype)
        rtjA = rtjA.astype(self.dtype)
        skelA = skelA.astype(self.dtype)
        quatA = quatA.astype(self.dtype)

        localB = localB.astype(self.dtype)
        rtjB = rtjB.astype(self.dtype)
        skelB = skelB.astype(self.dtype)
        quatB = quatB.astype(self.dtype)

        return (localA, rtjA, skelA, quatA, maskA,
                localB, rtjB, skelB, quatB, maskB)
Ejemplo n.º 4
0
def load(filename, start=None, end=None, order=None, world=False):
    """
    Reads a BVH file and constructs an animation
    
    Parameters
    ----------
    filename: str
        File to be opened
        
    start : int
        Optional Starting Frame
        
    end : int
        Optional Ending Frame
    
    order : str
        Optional Specifier for joint order.
        Given as string E.G 'xyz', 'zxy'
        
    world : bool
        If set to true euler angles are applied
        together in world space rather than local
        space

    Returns
    -------
    
    (animation, joint_names, frametime)
        Tuple of loaded animation and joint names
    """

    f = open(filename, "r")

    i = 0
    active = -1
    end_site = False

    names = []
    orients = Quaternions.id(0)
    offsets = np.array([]).reshape((0, 3))
    parents = np.array([], dtype=int)

    for line in f:

        if "HIERARCHY" in line: continue
        if "MOTION" in line: continue
        """ Modified line read to handle mixamo data """
        #        rmatch = re.match(r"ROOT (\w+)", line)
        rmatch = re.match(r"ROOT (\w+:?\w+)", line)
        if rmatch:
            names.append(rmatch.group(1))
            offsets = np.append(offsets, np.array([[0, 0, 0]]), axis=0)
            orients.qs = np.append(orients.qs,
                                   np.array([[1, 0, 0, 0]]),
                                   axis=0)
            parents = np.append(parents, active)
            active = (len(parents) - 1)
            continue

        if "{" in line:
            continue

        if "}" in line:
            if end_site:
                end_site = False
            else:
                active = parents[active]
            continue

        offmatch = re.match(
            r"\s*OFFSET\s+([\-\d\.e]+)\s+([\-\d\.e]+)\s+([\-\d\.e]+)", line)
        if offmatch:
            if not end_site:
                offsets[active] = np.array(
                    [list(map(float, offmatch.groups()))])
            continue

        chanmatch = re.match(r"\s*CHANNELS\s+(\d+)", line)
        if chanmatch:
            channels = int(chanmatch.group(1))
            if order is None:
                channelis = 0 if channels == 3 else 3
                channelie = 3 if channels == 3 else 6
                parts = line.split()[2 + channelis:2 + channelie]
                if any([p not in channelmap for p in parts]):
                    continue
                order = "".join([channelmap[p] for p in parts])
            continue
        """ Modified line read to handle mixamo data """
        #        jmatch = re.match("\s*JOINT\s+(\w+)", line)
        jmatch = re.match("\s*JOINT\s+(\w+:?\w+)", line)
        if jmatch:
            names.append(jmatch.group(1))
            offsets = np.append(offsets, np.array([[0, 0, 0]]), axis=0)
            orients.qs = np.append(orients.qs,
                                   np.array([[1, 0, 0, 0]]),
                                   axis=0)
            parents = np.append(parents, active)
            active = (len(parents) - 1)
            continue

        if "End Site" in line:
            end_site = True
            continue

        fmatch = re.match("\s*Frames:\s+(\d+)", line)
        if fmatch:
            if start and end:
                fnum = (end - start) - 1
            else:
                fnum = int(fmatch.group(1))
            jnum = len(parents)
            positions = offsets[np.newaxis].repeat(fnum, axis=0)
            rotations = np.zeros((fnum, len(orients), 3))
            continue

        fmatch = re.match("\s*Frame Time:\s+([\d\.]+)", line)
        if fmatch:
            frametime = float(fmatch.group(1))
            continue

        if (start and end) and (i < start or i >= end - 1):
            i += 1
            continue

        dmatch = line.strip().split(' ')
        if dmatch:
            data_block = np.array(list(map(float, dmatch)))
            N = len(parents)
            fi = i - start if start else i
            if channels == 3:
                positions[fi, 0:1] = data_block[0:3]
                rotations[fi, :] = data_block[3:].reshape(N, 3)
            elif channels == 6:
                data_block = data_block.reshape(N, 6)
                positions[fi, :] = data_block[:, 0:3]
                rotations[fi, :] = data_block[:, 3:6]
            elif channels == 9:
                positions[fi, 0] = data_block[0:3]
                data_block = data_block[3:].reshape(N - 1, 9)
                rotations[fi, 1:] = data_block[:, 3:6]
                positions[fi, 1:] += data_block[:, 0:3] * data_block[:, 6:9]
            else:
                raise Exception("Too many channels! %i" % channels)

            i += 1

    f.close()

    eulers = np.radians(rotations)
    rotations = Quaternions.from_euler(eulers, order=order, world=world)
    # print("BVH.load: {} done. ({} order)".format(filename, order))

    return Animation(rotations, positions, orients, offsets, parents,
                     eulers), names, frametime
Ejemplo n.º 5
0
def rotations_parents_global(anim):
    rotations = rotations_global(anim)
    rotations = rotations[:, anim.parents]
    rotations[:, 0] = Quaternions.id(len(anim))
    return rotations
Ejemplo n.º 6
0
def load_from_maya(root, start, end):
    """
    Load Animation Object from Maya Joint Skeleton    
    
    Parameters
    ----------
    
    root : PyNode
        Root Joint of Maya Skeleton
        
    start, end : int, int
        Start and End frame index of Maya Animation
    
    Returns
    -------
    
    animation : Animation
        Loaded animation from maya
        
    names : [str]
        Joint names from maya   
    """

    import pymel.core as pm

    original_time = pm.currentTime(q=True)
    pm.currentTime(start)
    """ Build Structure """

    names, parents = astruct.load_from_maya(root)
    descendants = astruct.descendants_list(parents)
    orients = Quaternions.id(len(names))
    offsets = np.array([pm.xform(j, q=True, translation=True) for j in names])

    for j, name in enumerate(names):
        scale = pm.xform(pm.PyNode(name), q=True, scale=True, relative=True)
        if len(descendants[j]) == 0: continue
        offsets[descendants[j]] *= scale
    """ Load Animation """

    eulers = np.zeros((end - start, len(names), 3))
    positions = np.zeros((end - start, len(names), 3))
    rotations = Quaternions.id((end - start, len(names)))

    for i in range(end - start):

        pm.currentTime(start + i + 1, u=True)

        scales = {}

        for j, name, parent in zip(range(len(names)), names, parents):

            node = pm.PyNode(name)

            if i == 0 and pm.hasAttr(node, 'jointOrient'):
                ort = node.getOrientation()
                orients[j] = Quaternions(
                    np.array([ort[3], ort[0], ort[1], ort[2]]))

            if pm.hasAttr(node, 'rotate'):
                eulers[i, j] = np.radians(pm.xform(node, q=True,
                                                   rotation=True))
            if pm.hasAttr(node, 'translate'):
                positions[i, j] = pm.xform(node, q=True, translation=True)
            if pm.hasAttr(node, 'scale'):
                scales[j] = pm.xform(node, q=True, scale=True, relative=True)

        for j in scales:
            if len(descendants[j]) == 0: continue
            positions[i, descendants[j]] *= scales[j]

        positions[i, 0] = pm.xform(root,
                                   q=True,
                                   translation=True,
                                   worldSpace=True)

    rotations = orients[np.newaxis] * Quaternions.from_euler(
        eulers, order='xyz', world=True)
    """ Done """

    pm.currentTime(original_time)

    return Animation(rotations, positions, orients, offsets, parents), names