示例#1
0
    def process_import(self, context, name, namespace, group, options):
        from maya import cmds, mel
        from reveries import plugins

        representation = context["representation"]
        asset_id = representation["data"]["animatedAssetId"]
        selected = cmds.ls(selection=True, long=True)

        # Collect namespace from selected nodes
        namespaces = defaultdict(set)
        for node in selected:
            ns = lib.get_ns(node)
            if ns == ":":
                continue
            namespaces[ns].add(node)

        for ns, nodes in namespaces.items():
            try:
                container = pipeline.get_container_from_namespace(ns)
            except RuntimeError:
                continue

            if asset_id != cmds.getAttr(container + ".assetId"):
                confirm = plugins.message_box_warning(
                    "Warning",
                    "Applying animation to different asset, are you sure ?",
                    optional=True,
                )
                if not confirm:
                    raise Exception("Operation canceled.")

            target_ns = ns
            members = nodes
            break

        else:
            raise Exception("No matched asset found.")

        cmds.loadPlugin("animImportExport", quiet=True)

        entry_path = self.file_path(representation).replace("\\", "/")
        sele_path = entry_path.rsplit("anim", 1)[0] + "mel"
        sele_path = os.path.expandvars(sele_path)

        with capsule.maintained_selection():
            # Select nodes with order
            with contextlib.nested(capsule.namespaced(target_ns, new=False),
                                   capsule.relative_namespaced()):
                self._selection_patch(sele_path)
                mel.eval("source \"%s\"" % sele_path)

            targets = cmds.ls(selection=True, long=True)
            nodes = cmds.file(entry_path,
                              force=True,
                              type="animImport",
                              i=True,
                              importTimeRange="keep",
                              ignoreVersion=True,
                              returnNewNodes=True,
                              options=("targetTime=4;"
                                       "option=replace;"
                                       "connect=0"))
            # Apply namespace by ourselves, since animImport does not
            # take -namespace flag
            namespaced_nodes = list()
            for node in nodes:
                node = cmds.rename(node, namespace + ":" + node)
                namespaced_nodes.append(node)

            # Delete not connected
            targets = set(targets)
            connected = list()
            for node in namespaced_nodes:
                future = cmds.listHistory(node, future=True)
                future = set(cmds.ls(future, long=True))
                if targets.intersection(future):
                    connected.append(node)
                else:
                    cmds.delete(node)

            if not connected:
                raise Exception("No animation been applied.")

            self[:] = connected

        # Remove assigned from selection
        unprocessed = list(set(selected) - members)
        cmds.select(unprocessed, replace=True, noExpand=True)
示例#2
0
    def extract_anim(self, packager):
        cmds.loadPlugin("animImportExport", quiet=True)

        package_path = packager.create_package()

        entry_file = packager.file_name("anim")
        entry_path = os.path.join(package_path, entry_file)

        sele_file = packager.file_name("mel")
        sele_path = os.path.join(package_path, sele_file)

        # Save animated nodes with order
        with capsule.maintained_selection():
            cmds.select(self.data["outAnim"], replace=True)

            with contextlib.nested(
                    capsule.namespaced(self.data["animatedNamespace"],
                                       new=False),
                    capsule.relative_namespaced()):
                # Save with basename
                with open(sele_path, "w") as fp:
                    fp.write("select -r\n" + "\n".join(cmds.ls(sl=True)) + ";")

        context_data = self.context.data
        start = context_data.get("startFrame")
        end = context_data.get("endFrame")

        with contextlib.nested(
                capsule.no_refresh(),
                capsule.maintained_selection(),
                capsule.undo_chunk(),
        ):
            lib.bake(self.data["outAnim"],
                     frame_range=(start, end),
                     shape=False)

            cmds.select(self.data["outAnim"], replace=True, noExpand=True)
            cmds.file(entry_path,
                      force=True,
                      typ="animExport",
                      exportSelectedAnim=True,
                      options=("options=keys;"
                               "hierarchy=none;"
                               "precision=17;"
                               "intValue=17;"
                               "nodeNames=1;"
                               "verboseUnits=0;"
                               "whichRange=1;"
                               "helpPictures=0;"
                               "useChannelBox=0;"
                               "controlPoints=0;"
                               "shapes=0;"
                               "copyKeyCmd="
                               "-animation objects "
                               "-option keys "
                               "-hierarchy none "
                               "-controlPoints 0 "
                               "-shape 0"))

        packager.add_data({
            "entryFileName": entry_file,
            "animatedAssetId": self.data["animatedAssetId"]
        })
示例#3
0
    def process(self, instance):
        from maya import cmds
        from reveries import utils
        from reveries.maya import lib, capsule

        cmds.loadPlugin("animImportExport", quiet=True)

        staging_dir = utils.stage_dir()
        script = "%s.mel" % instance.data["subset"]
        filename = "%s.anim" % instance.data["subset"]
        scriptpath = "%s/%s" % (staging_dir, script)
        outpath = "%s/%s" % (staging_dir, filename)

        animated_asset = instance.data["animatedAssetId"]

        instance.data["repr.anim._stage"] = staging_dir
        instance.data["repr.anim._files"] = [filename, script]
        instance.data["repr.anim.entryFileName"] = filename
        instance.data["repr.anim.animatedAssetId"] = animated_asset

        # Save animated nodes with order
        with capsule.maintained_selection():
            cmds.select(instance.data["outAnim"], replace=True)

            with contextlib.nested(
                    capsule.namespaced(instance.data["animatedNamespace"],
                                       new=False),
                    capsule.relative_namespaced()):
                # Save with basename
                with open(scriptpath, "w") as fp:
                    # Allow not existing nodes between assets
                    fp.write("select -r `ls\n" + "\n".join(cmds.ls(sl=True)) +
                             "`;")

        context_data = instance.context.data
        start = context_data["startFrame"]
        end = context_data["endFrame"]

        instance.data["startFrame"] = start
        instance.data["endFrame"] = end

        with contextlib.nested(
                capsule.no_refresh(),
                capsule.maintained_selection(),
                capsule.undo_chunk(),
        ):
            lib.bake(
                instance.data["outAnim"],
                frame_range=(start, end),
                shape=False,
                # Remove baked from layer so to bake out all keys like
                # animLayers being merged.
                remove_baked_attr_from_layer=True)

            cmds.select(instance.data["outAnim"], replace=True, noExpand=True)
            cmds.file(outpath,
                      force=True,
                      typ="animExport",
                      exportSelectedAnim=True,
                      options=("options=keys;"
                               "hierarchy=none;"
                               "precision=17;"
                               "intValue=17;"
                               "nodeNames=1;"
                               "verboseUnits=0;"
                               "whichRange=1;"
                               "helpPictures=0;"
                               "useChannelBox=0;"
                               "controlPoints=0;"
                               "shapes=0;"
                               "copyKeyCmd="
                               "-animation objects "
                               "-option keys "
                               "-hierarchy none "
                               "-controlPoints 0 "
                               "-shape 0"))