// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== // // // Creation Date: 2005 // // Description: assign selected follicles to selected hair constraint nodes // proc string getConstraintHairSystem( string $constraint ) { string $hsys[] = `listConnections -type hairSystem $constraint`; if( size( $hsys ) > 0 ){ return $hsys[0]; } else { return ""; } } proc string getFollicleHairSystem( string $follicle ) { string $hsys[] = `listConnections -type hairSystem $follicle`; if( size( $hsys ) > 0 ){ return $hsys[0]; } else { return ""; } } proc int getFollicleIndex( string $follicle ) { string $con[] = `connectionInfo -dfs ($follicle + ".outHair")`; if( size( $con ) > 0 ){ string $buffer[]; int $numTokens = `tokenize $con[0] ".[]" $buffer`; if( $numTokens == 3 ){ return( $buffer[2] ); } } return( -1 ); } global proc assignHairConstraint(int $append) { string $constraints[] = `ls -sl -dag -type hairConstraint`; if( size( $constraints ) < 1 ){ warning( (uiRes("m_assignHairConstraint.kNoHairConstraint")) ); return; } string $follicles[] = getSelectedHairCurves(); if( size( $follicles ) < 1 ){ warning( (uiRes("m_assignHairConstraint.kNoFolliclesSelected")) ); return; } string $constraint, $follicle; for( $constraint in $constraints ){ string $constraintHairSystem = getConstraintHairSystem( $constraint ); string $inds = ($constraint + ".curveIndices"); int $numIndices = `getAttr -size $inds`; int $newIndice = 0; float $currentIndices[]; if( $append ){ $newIndice = $numIndices; // note that getAttr returns a float array, // even though curveIndices is an integer multi attribute $currentIndices = getAttr( $inds ); } for( $follicle in $follicles ){ string $follicleHairSystem = getFollicleHairSystem( $follicle ); if( $follicleHairSystem != $constraintHairSystem ){ string $fmt = (uiRes("m_assignHairConstraint.kDifferentHairSystem")); warning( `format -s $follicle -s $constraint $fmt` ); continue; } int $follicleIndex = getFollicleIndex( $follicle ); if( $follicleIndex == -1 ){ string $fmt = (uiRes("m_assignHairConstraint.kBadFollicleConnection")); warning( `format -s $follicle $fmt` ); continue; } if( $append ){ // avoid adding duplicate indices int $matchFound = false; float $curInd; for( $curInd in $currentIndices ){ if( $follicleIndex ==(int)$curInd ){ $matchFound = true; break; } } if( $matchFound ){ continue; } } setAttr ($inds + "[" + $newIndice + "]") $follicleIndex; $newIndice++; } if( !$append ){ while ($newIndice < $numIndices){ removeMultiInstance -b true ($inds + "[" + $newIndice + "]"); $newIndice++; } } } refresh -f; // force update }