[Maya] Create UVPin with MEL

It seems the command UVPin relies on optionVar rather than standard command parameters, so I had to dig through the built in python code that defines the UVPin command: here is how to call it without changing the preset parameters of the "UV Pin Options" window:

/*
    Equivalent to: UV Pin Options window found in 
    Rigging -> Constraints -> UV Pin

    Here we preset parameters to create locators as UV pins
*/
{
       
    /*
    (Python code it was extracted from)
    Default parameters for UVPin command:

    //C:\Program Files\Autodesk\Maya2024\Python\Lib\site-packages\maya\internal\nodes\uvpin\cmd_create.py
    'normalAxis': pin_ifc.eAxisType.kXAxis,
    'tangentAxis': pin_ifc.eAxisType.kYAxis,
    'outputConnect':  pin_ifc.eXformIO.kCurrent,
    'setupMode' : ePinUVSetupMode.kTransforms,
    'uvSetName' : '',
    'relativeSpaceMode' : pin_ifc.eSpaceMode.kWorld,
    'normalizedIsoParms':  True 

    class ePinUVSetupMode(object):
        kComponents = 0
        kTransforms = 1

    // Python\Lib\site-packages\maya\internal\nodes\common\pin\node_interface.py
    class eAxisType(object):
        kXAxis	  = 0
        kYAxis	  = 1
        kZAxis	  = 2
        kNXAxis	  = 3
        kNYAxis	  = 4
        kNZAxis	  = 5
        kNoneAxis = 6


    class eXformIO(object):
        kCurrent = 0
        kMatrix = 1
        kNull = 2 # i.e just a transform.
        kLocator = 3

    class eSpaceMode(object):
        kWorld = 0
        kLocal = 1
        kCustom = 2

    */

    // Select mesh first
    string $selections[] = `ls -selection`;

    if( size($selections) < 0 ){
        warning("Select a mesh then joints or controllers");         
        return;
    }

    // check first element is a mesh:
    string $mesh = get_shape($selections[0]);
    if( nodeType($mesh) != "mesh" ){
        warning("Select a mesh *first* then joints or controllers");         
        return;
    }

    // *then* select joints
    // skip the check

    // setup default values
    int $normalAxisValue = 0;
    int $tangentAxisValue = 1;
    int $outputConnectValue = 0;
    int $setupModeValue = 1;
    string $uvSetNameValue = "";
    int $relativeSpaceModeValue = 0;
    int $normalizedIsoParmsValue = 1;

    // Get the values if it exists:
    if (`optionVar -exists UVPinnormalAxis`)
        $normalAxisValue = `optionVar -q UVPinnormalAxis`;        
    if (`optionVar -exists UVPintangentAxis`)
        $tangentAxisValue = `optionVar -q UVPintangentAxis`;
    if (`optionVar -exists UVPinoutputConnect`)
        $outputConnectValue = `optionVar -q UVPinoutputConnect`;
    if (`optionVar -exists UVPinsetupMode`)
        $setupModeValue = `optionVar -q UVPinsetupMode`;
    if (`optionVar -exists UVPinuvSetName`)
        $uvSetNameValue = `optionVar -q UVPinuvSetName`;
    if (`optionVar -exists UVPinrelativeSpaceMode`)
        $relativeSpaceModeValue = `optionVar -q UVPinrelativeSpaceMode`;
    if (`optionVar -exists UVPinnormalizedIsoParms`)
        $normalizedIsoParmsValue = `optionVar -q UVPinnormalizedIsoParms`;

    // Set the values of option variables for UVPin
    optionVar -intValue UVPinnormalAxis 0; // X
    optionVar -intValue UVPintangentAxis 1; // Y
    optionVar -intValue UVPinoutputConnect 3; // Locator
    optionVar -intValue UVPinsetupMode 1; // Transforms
    optionVar -stringValue UVPinuvSetName ""; // Default UV set
    optionVar -intValue UVPinrelativeSpaceMode 0; // World
    optionVar -intValue UVPinnormalizedIsoParms 1; // True

    // Launch the UVPin command
    UVPin;

    // restore option variables
    optionVar -intValue UVPinnormalAxis $normalAxisValue;
    optionVar -intValue UVPintangentAxis $tangentAxisValue;
    optionVar -intValue UVPinoutputConnect $outputConnectValue;
    optionVar -intValue UVPinsetupMode $setupModeValue;
    optionVar -stringValue UVPinuvSetName $uvSetNameValue;
    optionVar -intValue UVPinrelativeSpaceMode $relativeSpaceModeValue;
    optionVar -intValue UVPinnormalizedIsoParms $normalizedIsoParmsValue;
}

No comments

(optional field, I won't disclose or spam but it's necessary to notify you if I respond to your comment)
All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Anti-spam question: