Maya: create hair follicules with MEL script

When rigging we often use hairfollicules because they are fixed to uv coordinates of a surface, and that's handy in many situations. However, this is probably a legacy way of doing this, since we now have UV pin constraints introduced in Maya 2020. They can probably do the job as well.

Anyhow here is some code to create a single hairfollicule without having to create a whole hair system.

global proc string get_transform(string $shape)
{
    string $transfo;
    if ( "transform" != `nodeType $shape` )
    {
        string $parents[] = `listRelatives -fullPath -parent $shape`;
        if( size($parents) > 0)
            $transfo = $parents[0];
    }else{
        // If given node is already a transform, just pass on through
        $transfo = $shape;
    }
    return $transfo;
}

global proc string get_shape( string $xform )
{
    string $shape;
    if ( "transform" == `nodeType $xform` ) {
        string $parents[] = `listRelatives -fullPath -shapes $xform`;
        if( size($parents) > 0) {
            $shape = $parents[0];
        }
    } else {
        // Assume it's already a shape;
        $shape = $xform;
    }

    return $shape;
}

// Create a single follicule on some surface at (U,V) coordinates
// @param $surface : mesh name or nurb name
// @return follicule transform name
global proc string createHairFollicule(string $surface, float $U, float $V)
{

    $surface = get_shape($surface);
    string $shapeType = `nodeType $surface`;
    
    string $folliculeShape = `createNode follicle`;
    string $follicule = get_transform($folliculeShape);
      
    // Connect Objects    
    if($shapeType == "mesh")    
        connectAttr -f ($surface+".outMesh") ($folliculeShape+".inputMesh");
    else /* assume: $shapeType == "nurbsSurface" */
        connectAttr -f ($surface+".local") ($folliculeShape+".inputSurface");    
    
    connectAttr -f ($surface+".worldMatrix[0]") ($folliculeShape+".inputWorldMatrix");
    
    connectAttr -f ($folliculeShape+".outRotate") ($follicule+".rotate");
    connectAttr -f ($folliculeShape+".outTranslate") ($follicule+".translate");
    
    // center Object On Plane
    setAttr ($folliculeShape+".parameterU") $U;
    setAttr ($folliculeShape+".parameterV") $V;  
    
    return $follicule;
}


string $follicules[];
$follicules[size($follicules)] = createHairFollicule($nurbs, 0.0, 0.5);
$follicules[size($follicules)] = createHairFollicule($nurbs, 0.5, 0.5);
$follicules[size($follicules)] = createHairFollicule($nurbs, 1.0, 0.5);
group -name "follicules" $follicules;

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: