Exemplo n.º 1
0
    def _scale_skeleton(cls, binder_namespace):
        for jnt in mc.ls(type='joint'):
            if not jnt.startswith(binder_namespace) and jnt.endswith(
                    'C_pelvis'):
                source_root_joint = jnt
                break

        scale = (1.0, 1.0, 1.0)
        meta_nodes = getMetaNodes(mTypes='MsvAnimationBinderRig')
        if meta_nodes:
            for obj in meta_nodes:
                joint = obj.rootJoints[0].mNode

                scale = mc.getAttr(mc.listRelatives(joint, p=True)[0] +
                                   '.s')[0]
                break

        parent_grp = mc.listRelatives(source_root_joint, p=True)
        if parent_grp:
            parent_grp = parent_grp[0]
            # Be careful there is a link between this group and it's children joints
            # group.scale -> joint.inverseScale, we need to break it to let scale work
            dests = mc.connectionInfo('%s.scale' % parent_grp,
                                      destinationFromSource=True)
            for dest in dests:
                mc.disconnectAttr('%s.scale' % parent_grp, dest)
        else:
            parent_grp = mc.group(source_root_joint,
                                  n='output_Grp',
                                  w=True,
                                  r=True)
            mc.xform(parent_grp, os=True, pivots=(0, 0, 0))

        if scale != (1.0, 1.0, 1.0):
            mc.xform(parent_grp, s=scale, os=True)
Exemplo n.º 2
0
    def _bake(cls, namespace):
        source_rig = None
        dest_rig = None
        source_root = None
        meta_nodes = getMetaNodes(mTypes='MsvMetaRig')
        if meta_nodes:
            for obj in meta_nodes:
                # print 'obj rig: %s, rig: %s' % (obj.rigName, rig_name)
                # if obj.rigName == rig_name:
                if obj.nameSpaceFull().startswith(namespace):
                    dest_rig = obj
                else:
                    _source_rig = obj
                    _source_root = _source_rig.get_export_root()
                    if _source_root:
                        source_root = _source_root
                        source_rig = _source_rig

        if not source_root:
            root_name = dest_rig.get_export_root().split('|')[-1].split(
                ':')[-1]
            for jnt in mc.ls(type='joint'):
                if not mc.reference(
                        jnt, q=True,
                        isNodeReferenced=True) and not jnt.startswith(
                            namespace) and jnt.endswith(root_name):
                    source_root = jnt

        binder_rigs = r9Meta.getMetaNodes("MsvAnimationBinderRig")

        # source root (new rig) name is C_pelvis, Marker
        # but the dest root (old rig) name is root, marker, etc, so don't use name matching
        dest_root = [
            joint for joint in binder_rigs[0].get_root_joints()
            if mc.listRelatives(joint, c=True, type='joint')
        ][0]

        anim_binder.bind_skeletons(source_root, dest_root)

        dest_root_node = dest_rig.get_export_root()
        if mc.nodeType(dest_root_node) == 'reference':
            # We reference the skeleton into rig file
            # and the Meta rig node will store the reference node instead of the root joint
            # ps: the reference node is parent node of root joint
            joint_nodes = [
                node for node in mc.referenceQuery(
                    dest_root_node, nodes=True, dp=True)
                if mc.nodeType(node) == 'joint'
            ]
            joint_node = joint_nodes[0].split('|')[-1]
            dest_root_node = cls._get_root_parent(joint_node)
        else:
            # dest_root_node is u'|Thanator_Rig:main_Grp|Thanator_Rig:output_Grp|Thanator_Rig:C_pelvis'
            # always to select it's root parent node
            dest_root_node = dest_root_node.split('|')[1]

        ctrls = cls._get_bound_ctrls(dest_root_node)
        # anim_binder.bake_binder_data(dest_root_node)
        self._bake_binder_data()
        return source_rig, dest_rig, ctrls
    def _get_root_bone(cls):
        meta_nodes = getMetaNodes(mTypes='MsvMetaRig')
        if meta_nodes:
            for obj in meta_nodes:
                return obj.get_export_root()

        return None
Exemplo n.º 4
0
def export_animation(file_path):
    meta_nodes = getMetaNodes(mTypes='MsvMetaRig')
    meta_rig = None
    joints = None
    if meta_nodes:
        for meta_node in meta_nodes:
            _joints = meta_node.get_export_joints()
            if _joints and meta_node.nameSpaceFull():
                meta_rig = meta_node
                joints = _joints

    if meta_rig:
        start_time = mc.playbackOptions(q=True, minTime=True)
        end_time = mc.playbackOptions(q=True, maxTime=True)
        export_name = os.path.basename(file_path).split(os.path.extsep)[0]

        try:
            import_reference(file_path)
            bake_animation(joints, start_time, end_time)
            clean_skeleton(joints)
            export_to_fbx(joints,
                          start_time,
                          end_time,
                          export_name,
                          export_folder=EXPORT_FOLDER)
            print '%s: %s: %s' % (MAYABATCHOUTPUT, EXPORTANIMATION, file_path)
        except Exception as e:
            print '%s: %s: %s, Error Msg: %s' % (MAYABATCHOUTPUT, EXPORFAIL,
                                                 file_path, e.message)
    else:
        print '%s: %s: %s, Error Msg: Can not find meta rig!' % (
            MAYABATCHOUTPUT, EXPORFAIL, file_path)
Exemplo n.º 5
0
    def lateInit(self, **kwargs):
        super(MirrorAnimModel, self).lateInit(**kwargs)

        meta_nodes = getMetaNodes(mTypes='MsvRigComponentMarkerControl')
        if meta_nodes:
            self.marker_ctl_full_name = '%s:%s' % (meta_nodes[0].nameSpaceFull(
            ), meta_nodes[0].get_virtual_name())
            self.marker_ctl_display_name = meta_nodes[0].get_virtual_name()
            self._sendMessage('mirror_plan', self.marker_ctl_display_name)
        else:
            self.marker_ctl_full_name = None
            self.marker_ctl_display_name = None
    def generate(self):
        meta_nodes = getMetaNodes(mTypes='MsvMetaRig')
        root_bone = meta_nodes[0].get_export_root()
        if meta_nodes:
            joints = [
                joint for joint in meta_nodes[0].get_export_joints()
                if not mc.getAttr(joint + '.isEnd')
            ]
            for i in range(len(joints)):
                if not joints[i].startswith(root_bone):
                    continue

                joint = jointData.JointData(joints[i], i)
                if joint.is_hit_box_model:
                    self._jointList.append(joint)
Exemplo n.º 7
0
def reference_animation(file_path, reference_path):
    meta_nodes = getMetaNodes(mTypes='MsvMetaRig')
    meta_rig = None
    if meta_nodes:
        meta_rig = meta_nodes[0]

    if meta_rig:
        try:
            retarget_reference(meta_rig, reference_path)
            print '%s: %s: %s' % (MAYABATCHOUTPUT, EXPORTANIMATION, file_path)
        except Exception as e:
            print '%s: %s: %s, Error Msg: %s' % (MAYABATCHOUTPUT, EXPORFAIL,
                                                 file_path, e.message)
    else:
        print '%s: %s: %s, Error Msg: Can not find meta rig!' % (
            MAYABATCHOUTPUT, EXPORFAIL, file_path)
Exemplo n.º 8
0
    def _remove_namespace(cls, file_path, binder_namespace):
        # mc.file(new=True, force=True)
        #
        # mc.refresh(force=True)
        mc.file(file_path, open=True, force=True, prompt=False)
        try:
            mc.namespace(moveNamespace=[binder_namespace + ':', ':'],
                         force=True)
        except Exception as e:
            print 'remove namespace fails: %s' % e.message
            # Currently if the rig file reference the bind file, it will raise an error
            # https://knowledge.autodesk.com/support/maya/troubleshooting/caas/sfdcarticles/sfdcarticles/Unable-to-remove-a-namespace-from-a-references-object.html
            # So, we need to do these steps
            # 1. Unload file reference
            rig = None

            meta_nodes = getMetaNodes(mTypes='MsvMetaRig')
            if meta_nodes:
                for obj in meta_nodes:
                    if obj.nameSpaceFull().startswith(binder_namespace):
                        rig = obj

            reference_node = rig.referenceNode()

            om.MFileIO.unloadReferenceByNode(reference_node)

            # 2. Remove namespace
            mc.namespace(moveNamespace=[binder_namespace + ':', ':'],
                         force=True)

            # 3. Load reference again
            new_reference_node_name = reference_node.replace(
                binder_namespace + ':', '')
            om.MFileIO.loadReferenceByNode(new_reference_node_name)

        mc.file(save=True, type='mayaAscii', options='v=0;', force=True)
Exemplo n.º 9
0
    def attribute_changed_callback(self, *args):
        """
        This callback will be invoked after open animation file
        :param args:
        :return:
        """
        meta_nodes = getMetaNodes(mTypes='MsvMetaRig')
        is_tetrapteron = False
        namespace = ''
        if meta_nodes:
            for obj in meta_nodes:
                if obj.rigName == 'Tetrapteron':
                    is_tetrapteron = True
                    namespace = obj.nameSpaceFull()
                    break

        if not is_tetrapteron:
            return

        def string_to_list(long_string):
            li = list()
            for data in long_string.split(';'):
                if data:
                    tmp = [d for d in data.split(',') if d]
                    li.append(tmp)
            return li

        def update_transform(datas):
            """
            :param datas:
            L_beakReflex_01_parentConstraint1,L_beakReflex_01,L_Fk_beakReflex_01_Ctl,L_Fk_beakReflex_01W0,L_beakReflex_01_bakeW1;
            L_beakReflex_05_parentConstraint1,L_beakReflex_05,L_Fk_beakReflex_05_Ctl,L_Fk_beakReflex_05W0;
            :return:
            """
            matrices = list()
            for i in range(len(datas)):
                skin_joint = datas[i][1]
                matrix = mc.xform('%s:%s' % (namespace, skin_joint),
                                  q=True,
                                  ws=True,
                                  m=True)
                matrices.append(matrix)

            for i in range(len(datas)):
                fk_ctl = datas[i][2]
                mc.xform('%s:%s' % (namespace, fk_ctl), m=matrices[i], ws=True)

        def switch_constraint(datas, index):
            """
            :param datas:
            L_beakReflex_01_parentConstraint1,L_beakReflex_01,L_Fk_beakReflex_01_Ctl,L_Fk_beakReflex_01W0,L_beakReflex_01_bakeW1;
            L_beakReflex_05_parentConstraint1,L_beakReflex_05,L_Fk_beakReflex_05_Ctl,L_Fk_beakReflex_05W0;
            :param index:
            :return:
            """
            constraint = datas[0]
            if index == 0:
                mc.setAttr('%s:%s.%s' % (namespace, constraint, datas[3]), 0)
                if len(datas) == 5:
                    mc.setAttr('%s:%s.%s' % (namespace, constraint, datas[4]),
                               1)
            elif index == 1:
                mc.setAttr('%s:%s.%s' % (namespace, constraint, datas[3]), 1)
                if len(datas) == 5:
                    mc.setAttr('%s:%s.%s' % (namespace, constraint, datas[4]),
                               0)

        def switch(index, node_name):
            """
            :param index: 0: sim, 1: fk
            :param node_name:
            :return:
            """
            left_parent_constraint_list = string_to_list(
                mc.getAttr(node_name + '.leftBeakParentConstraint'))
            right_parent_constraint_list = string_to_list(
                mc.getAttr(node_name + '.rightBeakParentConstraint'))

            if index == 1:
                update_transform(left_parent_constraint_list)
                update_transform(right_parent_constraint_list)

            for data in left_parent_constraint_list:
                switch_constraint(data, index)
            for data in right_parent_constraint_list:
                switch_constraint(data, index)

        def change_simfk(msg, m_plug, other_msg, client_data):
            """
            # msg  kIncomingDirection = 2048
            # msg  kAttributeSet = 8
            # 2048 + 8 = 2056
            :param msg:
            :param m_plug:
            :param other_msg:
            :param client_data:
            :return:
            """
            if msg == 2056:
                if m_plug.partialName() == 'simfk':
                    node = om.MFnDependencyNode(m_plug.node())
                    index = node.findPlug('simfk', False).asInt()
                    switch(index, node.name())

        li = om.MSelectionList()
        li.add('%s:%s' % (namespace, self.beak_reflex_target))
        obj = li.getDependNode(0)

        # remove previous callbacks
        ids = om.MNodeMessage.nodeCallbacks(obj)
        om.MNodeMessage.removeCallbacks(ids)

        # add new one
        om.MNodeMessage.addAttributeChangedCallback(obj, change_simfk)
Exemplo n.º 10
0
from blue.dcc.maya.core.msvAnimationRetarget import HIKCharacter, HIKSource, MetaRigDestination, MetaRigSource, BinderDestination
from Red9.core.Red9_Meta import getMetaNodes
from shared.dcc.maya.core.msvTime import get_current_time_range

rig = None
if getMetaNodes(mTypes='MsvMetaRig'):
    meta_nodes = getMetaNodes(mTypes='MsvMetaRig')
    for obj in meta_nodes:
        if obj.rigName == '{rig_name}':
            rig = obj

source_mapping = MetaRigSource.create_from_meta_rig(rig)
source_mapping.enable()

destination_mapping = BinderDestination.create_from_file('{binder_file}')
destination_mapping.enable()

MetaRigDestination.connect_mappings(source_mapping, destination_mapping)

start, end = get_current_time_range()
destination_mapping.bake_data(start=start, end=end)