Maya Scripting | Mirroring + Slicing

Welcome to some scripting fundamentals. I got asked to show how to write a script to handle geometric mirror and slice operations in Maya. I adapted a version from my personal framework for general use.

mirror_utils.py

import pymel.core as pm


class Axis:
    x = 0
    y = 1
    z = 2


def mirror(nodes=None, axis=Axis.x, positive=False, merge_threshold=0.001):
    """
    Mirrors geometry along an axis
    :param axis: Specify geometric axis
    :param positive: Specify positive or negative axis
    :param merge_threshold: threshold along axis
    """
    selection = pm.ls(sl=True)
    nodes = pm.ls(nodes) if nodes else pm.ls(sl=True, tr=True)
    direction = {
        Axis.x: 0 + positive,
        Axis.y: 2 + positive,
        Axis.z: 4 + positive
    }

    for item in nodes:
        pm.select(item)
        pivot_position = [pm.xform(item, query=True, piv=True, ws=True)[i] for i in range(3)]
        slice(item, axis, not positive)
        pm.polyMirrorFace(
            item, 
            ws=True, 
            d=direction[axis], 
            mergeMode=1, 
            p=pivot_position, 
            mt=merge_threshold, 
            mtt=1
        )
    
    pm.select(selection)



def slice(nodes=None, axis=Axis.x, positive=True):
    """
    Slices geometry along an axis
    :param axis: Specify geometric axis
    :param positive: Specify positive or negative axis
    """
    selection = pm.ls(sl=True)
    print(selection)
    nodes = pm.ls(nodes) if nodes else pm.ls(sl=True, tr=True)
    angles = {
        Axis.x: [0, positive * 180 - 90, 0],
        Axis.y: [90 - positive * 180, 0, 0],
        Axis.z: [0, 180 - positive * 180, 0]
    }
    cut_axis = angles[axis]

    for item in nodes:
        pm.select(item)
        pivot_matrix = pm.xform(item, query=True, piv=True, ws=True)
        pivot_position = [pivot_matrix[0], pivot_matrix[1], pivot_matrix[2]]
        pm.polyCut(
            cutPlaneCenter=pivot_position,
            cutPlaneRotate=cut_axis,
            extractFaces=True,
            extractOffset=[0, 0, 0],
            deleteFaces=True
        )

    pm.select(selection)

1) Put the script into a folder called MayaScripts in your user directory

2) Add that directory path to your Maya.env PYTHONPATH variable:

PYTHONPATH=$HOME/MayaScripts

3) Create buttons on your shelf with the following scripts:

Slice button:

from mirror_utils import slice
slice()

Mirror button:

from mirror_utils import mirror
mirror()

These buttons should now give you slice and mirror functionality.

Leave a comment