Home Youtube GitHub

Pre and Post scripts sharing


#1

While mGear is an awesome tool, it does sometimes require customization when a deadline is approaching.

This thread is meant to serve as a collection for all pre and post scripts people might want to share to the community.


#2

ngSkinTools Importer

I was missing a way of importing ngSkinTools exports with layers, so I made this post script:

import os

import pymel.core as pc
import ngSkinTools.importExport
import mgear


basename = os.path.basename(pc.sceneName())
filename = os.path.splitext(basename)[0]
directory = os.path.dirname(pc.sceneName())

# Find *.skin folder starting with same name as current file
json_files = []
for folder in os.listdir(directory):
    if folder == (filename + ".skin"):
        path = os.path.join(directory, filename + ".skin")
        for f in os.listdir(path):
            if f.endswith(".json"):
                json_files.append(os.path.join(path, f))

mismatch_files = []
for json_file in json_files:
    path = os.path.join(directory, json_file)
    importer = ngSkinTools.importExport.JsonImporter()
    with open(path) as f:
        data = importer.process(f.read())

        missing_nodes = []
        for name in data.getAllInfluences():
            if not pc.objExists(name):
                missing_nodes.append(name)

        if missing_nodes:
            raise ValueError(
                "Missing joints for {0}:\n{1}".format(
                    path, "\n".join(missing_nodes)
                )
            )

        # Setup skinned mesh from deformer members
        joints = pc.PyNode("rig_deformers_grp").members()

        # Find mesh from filename
        mesh_name = os.path.splitext(os.path.basename(json_file))[0]
        if not pc.objExists(mesh_name):
            mismatch_files.append(json_file)
            continue
        mesh = pc.PyNode(mesh_name)
        skinCluster = pc.skinCluster(
            joints, mesh, skinMethod=2, removeUnusedInfluence=False
        )
        data.saveTo(mesh.name())

if mismatch_files:
    mgear.log(
        "Missing meshes for files:\n{0}".format("\n".join(mismatch_files))
    )

Usage
Export your skinning with https://www.ngskintools.com/. Name a folder next to the current maya file {maya_filename}.skin and save the skinning in there with {mesh_name}.json.


#3

Studio Library Importer

I was missing a way to import deformation animation when rebuilding.

import os

import pymel.core as pc
import studiolibrary
from studiolibrarymaya import animitem


basename = os.path.basename(pc.sceneName())
filename = os.path.splitext(basename)[0]
directory = os.path.dirname(pc.sceneName())

# Find *.anim folder starting with same name as current file
anim_folder = None
for f in os.listdir(directory):
    if f == (filename + ".anim"):
        anim_folder = f

# Loading an animation item
item = animitem.AnimItem(os.path.join(directory, anim_folder))
item.load(
    objects=[],
    namespaces=[],
    option=animitem.PasteOption.ReplaceCompletely,
    connect=False,
    currentTime=False
)

Usage
Export your animation with https://www.studiolibrary.com/, name it the same as the maya file and save it next to the maya file you are working with. You can also just copy/rename the folder from an existing library.
Now just add the post script to the settings.

The script will search the directory of the currently opened maya file, find {maya_filename}.anim folder and apply the animation to rig.


#4

Cool idea. I’ll try to find some of mine that are not too specific to our characters.

This one maybe should have been a bug report. When I move my character hundreds of units, the mouth curves slowly begin to collapse. I fix it like this:

# a hack or solution? Mouth wires don't follow rig 100% unless you crank up the dropoffDistance.
# So when the character moves large distances, the mouth slowly begins to collapse.
for each in pm.ls(type='wire'):
    each.dropoffDistance[0].set(100)

#5

@Toke_Stuart_Jepsen and @chrislesage

Thanks guys! for sharing all this with the community :smiley:

This is super good idea!

EDIT: Just pinned it globally in the forum :slight_smile:


#6

#8

Eyes Rigger Importer

import os

import pymel.core as pc
import mgear


basename = os.path.basename(pc.sceneName())
filename = os.path.splitext(basename)[0]
directory = os.path.dirname(pc.sceneName())

# Find *.eyes folder starting with same name as current file
eyes_files = []
for folder in os.listdir(directory):
    if folder == (filename + ".eyes"):
        path = os.path.join(directory, folder)
        for f in os.listdir(path):
            if f.endswith(".eyes"):
                eyes_files.append(os.path.join(path, f))

for eyes_file in eyes_files:
    path = os.path.join(directory, eyes_file)
    mgear.rigbits.facial_rigger.eye_rigger.rig_from_file(path)

Usage

Name a folder next to the current maya file {maya_filename}.eyes and export your eye config there.


#9

Delta Mush Importer

import os

import pymel.core


basename = os.path.basename(pymel.core.sceneName())
filename = os.path.splitext(basename)[0]
directory = os.path.dirname(pymel.core.sceneName())

# Find "delta_mush" folder in scene file directory
file_paths = []
for item in os.listdir(directory):
    if item == "delta_mush":
        for f in os.listdir(os.path.join(directory, "delta_mush")):
            if f.startswith(filename):
                file_paths.append(os.path.join(directory, "delta_mush", f))

for file_path in file_paths:
    node_name = file_path.split(".")[-2]

    if not pymel.core.objExists(node_name):
        raise ValueError(
            "Missing \"{0}\" for delta mush from \"{1}\"".format(
                node_name, file_path
            )
        )

    delta_mush = pymel.core.deltaMush(node_name)

    pymel.core.deformerWeights(
        os.path.basename(file_path),
        path=os.path.dirname(file_path),
        im=True,
        deformer=delta_mush,
        method="index"
    )

Usage
Export your delta mush weights with Deform > Export Weights as json. Name a folder next to the current maya file “delta_mush” and name the file {maya_filename}.{mesh_name}.json.


#10

Does Deform -> Export Weights actually work for you? It has been completely unusable for me, spitting out useless data and/or importing mangled data.


#11

Yeah, it works. I export json files and import by index. Any of the other import options like “nearest” doesn’t really work for me.

BTW I’m on Maya 2019, maybe that makes a difference.