proc string rootNode( string $node ) { string $no_component = `match "^[^\.]*" $node`; return $no_component; } // //////////////////////////////////////////////////////////////////// // getMaterialFromSG // // Description: Returns the Material node feeing the '.surfaceShader' // attribute of the specified Shading Group node (shadingEngine). proc string getMaterialFromSG( string $SG ) { string $material = ""; if ( "shadingEngine" == `nodeType $SG` && `connectionInfo -id ( $SG + ".surfaceShader" )` ) { $material = rootNode( `connectionInfo -sfd ( $SG + ".surfaceShader" )` ); } return $material; } // //////////////////////////////////////////////////////// // getTextureFromMaterial // // Description: Returns the File Texture node feeding the '.color' // attribute of the specified Material node proc string getTextureFromMaterial( string $material ) { string $texture = ""; string $class[] = getClassification( `nodeType $material` ); if ( "shader/surface" == $class[0] && `connectionInfo -id ( $material + ".color" )` ) { $texture = rootNode( `connectionInfo -sfd ( $material + ".color" )` ); } return $texture; } // //////////////////////////////////////////////////////////////////// // getSGsFromShape // // Description: Returns an array of the Shading Groups (shadingEngine // nodes) responsible for shading the specified shape node. proc string[] getSGsFromShape( string $shape ) { string $shadingEngines[]; if ( `objExists $shape` ) { string $dest[] = `listConnections -destination true -source false -plugs false -type "shadingEngine" $shape`; // listConnections can return duplicates within its list. // The select below is a quick trick to avoid dupes in the // returned array. if ( size( $dest ) ) { string $select[] = `ls -sl`; select -r -ne $dest; $shadingEngines = `ls -sl`; select -r $select; } } return $shadingEngines; } proc string[] getShapes( string $xform ) { string $shapes[]; $shapes[0] = $xform; if ( "transform" == `nodeType $xform` ) // If given node is not a transform, assume it is a shape // and pass it through { $shapes = `listRelatives -fullPath -shapes $xform`; } return $shapes; } global proc ajhReloadTextures() { string $selectionList[] = `ls -sl`; for ( $object in $selectionList ) { string $newShapes[] = getShapes( $object ); for ( $shape in $newShapes ) { print "Shape: "; print $shape; print "\n"; string $newSGS[] = getSGsFromShape( $shape ); for ( $SG in $newSGS ) { print "SG: "; print $SG; print "\n"; string $newMaterial = getMaterialFromSG( $SG ); print "Material:"; print $newMaterial; print "\n"; string $newTexture = getTextureFromMaterial( $newMaterial ); if ( $newTexture != "" ) { print "Texture:"; print $newTexture; print "\n"; $command = "getAttr " + $newTexture + ".fileTextureName"; string $name = eval($command); print "Name:"; print $name; print "\n"; setAttr ( $newTexture + ".fileTextureName" ) -type "string" $name; } } } } }