// =========================================================================== // 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. // =========================================================================== proc getShadingEnginesFromMaterial( string $SG[], string $material, string $visitedNodes[] ) { // Iterate through connections and identify ShadingGroup sets. string $dests[] = `listConnections -s 0 -d 1 $material`; for ( $dest in $dests ) { if( `objExists $dest` ) { // visitedNodes is used for loop detection and avoidance. if (!`stringArrayContains $dest $visitedNodes`) { $visitedNodes[`size $visitedNodes`] = $dest; if ( "shadingEngine" == `nodeType $dest` ) $SG[`size $SG`] = $dest; else getShadingEnginesFromMaterial($SG, $dest, $visitedNodes); } } } } global proc hypergraphAssignTextureToSelection(string $texture) { string $materials[]; string $material; string $attr; string $outputAttributeNames[] = `callbacks -executeCallbacks -hook "provideOutputAttributeNameForTextureNode" $texture`; if( size($outputAttributeNames) > 1 ) { string $msg = (uiRes("m_hypergraphAssignTextureToSelection.kMultipleTextureOwnerWarning")); warning (`format -stringArg $texture $msg`); } else if( size($outputAttributeNames) == 1 ) { $attr = ("." + $outputAttributeNames[0]); } else { $attr = ".outColor"; } $materials = `listConnections -destination true ($texture + $attr)`; if (size($materials) > 1) { string $msg = (uiRes("m_hypergraphAssignTextureToSelection.kTextureManyMat")); error(`format -stringArg $texture $msg`); return; } else if (size($materials) == 0) { // // The texture is not associated with any material, so // we'll create one now. // Create a lambert shader, & connect this texture to it. string $savedSelection[] = `ls -sl`; $material = `shadingNode -asShader lambert -n ($texture + "Material")`; defaultNavigation -connectToExisting -source $texture -destination $material -force true; select $savedSelection; } else { $material = $materials[0]; } string $shadingGroups[]; string $shadingGroup; string $visitedNodes[]; getShadingEnginesFromMaterial($shadingGroups, $material, $visitedNodes); if (size($shadingGroups) > 1) { string $msg = (uiRes("m_hypergraphAssignTextureToSelection.kManyMaterials")); error(`format -stringArg $material $msg`); return; } else if (size($shadingGroups) == 0) { // // The material is not associated with any shading group, so // we'll create one now, and & connect this material to it. $shadingGroup = `sets -renderable true -noSurfaceShader true -empty -name ($texture + "SG")`; connectAttr -f ($material + ".outColor") ($shadingGroup + ".surfaceShader"); } else { $shadingGroup = $shadingGroups[0]; } sets -edit -forceElement $shadingGroup; }