// ========================================================================= // Apply any Crysis shader to a Maya model // ========================================================================= // // Instructions: See http:/www.seithcg.com // global string $applyCrysisShaderVersion = "1.0d"; global string $crysisPath = "E:/Crysis/Game/"; // ========================================================================= // Convert 0.0-1.0 float value to 255 value // ========================================================================= global proc string[] convertFloatTo255( string $valueAsString ) { // Convert decimal values to 0-255 values to mimic Crysis's. string $attrDecimal = $valueAsString; string $attr255[] = {}; string $bufferTmp[]; int $numTokensTmp = `tokenize $attrDecimal "," $bufferTmp`; for ($i=0;$i<3;$i++) { float $bufferTmpInt = $bufferTmp[$i]; int $resultTmp = $bufferTmpInt * 255; $attr255[$i] = $resultTmp; } return( $attr255 ); } // ========================================================================= // Parse through a Crysis .mtl file to get the list of materials included // ========================================================================= global proc string[] getMaterialsInMtlFile( string $mtlFile ) { // Get the material names. $fileId = `fopen $mtlFile "r"`; string $listOfMaterialsTmp = ""; // For every line in the mtl file: while ( !`feof $fileId` ) { string $line = `fgetline $fileId`; //print $line; if ( `gmatch $line "**"` && $recordNow ) break; } if ( $materialInfo == "" ) { frewind $fileId; while ( !`feof $fileId` ) { string $line = `fgetline $fileId`; //print $line; $materialInfo = $materialInfo + $line; } } fclose $fileId; return $materialInfo; } // ========================================================================= // Choose the shader from the mtl file // ========================================================================= global proc chooseShader( string $mtlFile, string $listOfMaterials[] ) { // -------------------------- // build UI // -------------------------- string $sel[] = `ls -sl`; if (`window -exists chooseShaderWindow` ) deleteUI chooseShaderWindow; int $w = 175; int $wc = 55; window -tlb off -rtf on -sizeable true -width 50 -height 40 -title ("Choose Shader:") chooseShaderWindow; columnLayout -adjustableColumn false -p chooseShaderWindow mainColumn; frameLayout -mw 5 -bv on -lv off -collapsable false -borderStyle "etchedOut" -p mainColumn matchPivotFrame; columnLayout -adjustableColumn false -p matchPivotFrame firstColumn; separator -h 4 -st "none" -w $w; textScrollList -numberOfRows 8 -w $w -allowMultiSelection false -dcc ("doChooseShader(\"" + $mtlFile + "\")") listShadersTSL; separator -st none -h 2; button -label "Apply" -h 22 -w $w -ann "" -c ("doChooseShader(\"" + $mtlFile + "\")"); separator -st none -h 4; button -label "Cancel" -h 22 -w $w -ann "" -c "deleteUI chooseShaderWindow; error \"Aborted by user!\""; setParent ..; setParent ..; for ($i=0;$i<`size $listOfMaterials`;$i++) { textScrollList -e -appendPosition 999 $listOfMaterials[$i] listShadersTSL; } showWindow chooseShaderWindow; } global proc doChooseShader( string $mtlFile ) { //print ("$mtlFile = " + $mtlFile + "\n"); print "$listOfMaterials[] (second) = "; print $listOfMaterials; print " \n"; string $materialInfoTmp[] = `textScrollList -q -si listShadersTSL`; //print ("$materialInfoTmp[0] = " + $materialInfoTmp[0]); deleteUI chooseShaderWindow; string $materialInfo = getMaterialInfo( $mtlFile, $materialInfoTmp[0] ); //print ("$materialInfo = " + $materialInfo); string $shaderInfo[] = extractMapsInfo( $materialInfo ); //print "$shaderInfo = "; print $materialInfo; createShader( $shaderInfo, $materialInfo );// $materialInfo, $shader, $surfaceType, $diffuseMap, $specularMap, $normalMap, $detail, $opacity, $environment } // ========================================================================= // Extract maps from material // ========================================================================= global proc string[] extractMapsInfo( string $materialInfo ) { string $shader, $surfaceType, $diffuseMap, $specularMap, $subSurfaceMap, $bumpMap, $normalMap, $detail, $opacity, $environment;//print ("$materialInfo = " + $materialInfo); string $buffer[]; int $numTokens = `tokenize $materialInfo "\"" $buffer`; for ($i=0;$i<`size $buffer`;$i++) { if ( `gmatch " Shader=" $buffer[$i]` ) { $shader = $buffer[$i+1]; print ("Extract time:\n$shader = " + $shader + "\n"); } if ( `gmatch " SurfaceType=" $buffer[$i]` ) { $surfaceType = $buffer[$i+1]; if ( $surfaceType == " Diffuse=" ) $surfaceType = ""; print ("$surfaceType = " + $surfaceType + "\n"); } if ( `gmatch "Diffuse" $buffer[$i]` ) { $diffuseMap = $buffer[$i+2]; print ("$diffuseMap = " + $diffuseMap + "\n"); } if ( `gmatch "Specular" $buffer[$i]` ) { $specularMap = $buffer[$i+2]; print ("$specularMap = " + $specularMap + "\n"); } if ( `gmatch "SubSurface" $buffer[$i]` ) { $subSurfaceMap = $buffer[$i+2]; print ("$subSurfaceMap = " + $subSurfaceMap + "\n"); } if ( `gmatch "Bumpmap" $buffer[$i]` ) { $bumpMap = $buffer[$i+2]; print ("$bumpMap = " + $bumpMap + "\n"); } if ( `gmatch "Normalmap" $buffer[$i]` ) { $normalMap = $buffer[$i+2]; print ("$normalMap = " + $normalMap + "\n"); } if ( `gmatch "Detail" $buffer[$i]` ) { $detail = $buffer[$i+2]; print ("$detail = " + $detail + "\n"); } if ( `gmatch "Opacity" $buffer[$i]` ) { $opacity = $buffer[$i+2]; print ("$opacity = " + $opacity + "\n"); } if ( `gmatch "Environment" $buffer[$i]` ) { $environment = $buffer[$i+2]; print ("$environment = " + $environment + "\n\n"); } } return { $shader, $surfaceType, $diffuseMap, $specularMap, $subSurfaceMap, $bumpMap, $normalMap, $detail, $opacity, $environment };//print ("$materialInfo = " + $materialInfo); } // ========================================================================= // Check if texture file exists // ========================================================================= global proc string checkIfTextureFileExists( string $textureFile ) { global string $crysisPath; // Find out if the texture's path is relative or absolute. string $buffer[]; int $numTokens = `tokenize $textureFile ":" $buffer`; if ( `size $buffer` == 1 ) $textureFile = $crysisPath + $textureFile; // Try different suffixes. if ( !`file -q -ex $textureFile` ) { string $buffer[]; int $numTokens = `tokenize $textureFile "." $buffer`; //print ("Checking existence of " + $textureFile + "\n"); if ( `file -q -ex ($buffer[0] + ".dds")` ) { $textureFile = ($buffer[0] + ".dds"); //print ("$realTextureFile = " + ($buffer[0] + ".dds") + "\n"); } } return $textureFile; } proc createShaderConnections( string $place2dTexture, string $shadingNode ) { connectAttr -f ($place2dTexture + ".coverage") ($shadingNode + ".coverage"); connectAttr -f ($place2dTexture + ".translateFrame") ($shadingNode + ".translateFrame"); connectAttr -f ($place2dTexture + ".rotateFrame") ($shadingNode + ".rotateFrame"); connectAttr -f ($place2dTexture + ".mirrorU") ($shadingNode + ".mirrorU"); connectAttr -f ($place2dTexture + ".mirrorV") ($shadingNode + ".mirrorV"); connectAttr -f ($place2dTexture + ".stagger") ($shadingNode + ".stagger"); connectAttr -f ($place2dTexture + ".wrapU") ($shadingNode + ".wrapU"); connectAttr -f ($place2dTexture + ".wrapV") ($shadingNode + ".wrapV"); connectAttr -f ($place2dTexture + ".repeatUV") ($shadingNode + ".repeatUV"); connectAttr -f ($place2dTexture + ".offset") ($shadingNode + ".offset"); connectAttr -f ($place2dTexture + ".rotateUV") ($shadingNode + ".rotateUV"); connectAttr -f ($place2dTexture + ".noiseUV") ($shadingNode + ".noiseUV"); connectAttr -f ($place2dTexture + ".vertexUvOne") ($shadingNode + ".vertexUvOne"); connectAttr -f ($place2dTexture + ".vertexUvTwo") ($shadingNode + ".vertexUvTwo"); connectAttr -f ($place2dTexture + ".vertexUvThree") ($shadingNode + ".vertexUvThree"); connectAttr -f ($place2dTexture + ".vertexCameraOne") ($shadingNode + ".vertexCameraOne"); connectAttr ($place2dTexture + ".outUV") ($shadingNode + ".uv"); connectAttr ($place2dTexture + ".outUvFilterSize") ($shadingNode + ".uvFilterSize"); } // ========================================================================= // Create shader // ========================================================================= global proc createShader( string $shaderInfo[], string $materialInfo ) { global string $crysisPath; string $shader = $shaderInfo[0]; print ("\nBuilding time:\n$shader = " + $shader + "\n"); string $surfaceType = $shaderInfo[1]; print ("$surfaceType = " + $surfaceType + "\n"); string $diffuseMap = $shaderInfo[2]; print ("$diffuseMap = " + $diffuseMap + "\n"); string $specularMap = $shaderInfo[3]; print ("$specularMap = " + $specularMap + "\n"); string $subSurfaceMap = $shaderInfo[4]; print ("$subSurfaceMap = " + $subSurfaceMap + "\n"); string $bumpMap = $shaderInfo[5]; print ("$bumpMap = " + $bumpMap + "\n"); string $normalMap = $shaderInfo[6]; print ("$normalMap = " + $normalMap + "\n"); string $detailMap = $shaderInfo[7]; print ("$detailMap = " + $detailMap + "\n"); string $opacityMap = $shaderInfo[8]; print ("$opacityMap = " + $opacityMap + "\n"); string $environmentMap = $shaderInfo[9]; print ("$environmentMap = " + $environmentMap + "\n"); string $sel[] = `ls -sl`; string $selTypeTmp[] = `ls -st $sel[0]`; string $mayaShaderTmp[]; print ("\nProcessing shader " + $shader + " ...\n"); // Get the existing shader name. if ( $selTypeTmp[1] == "blinn" || $selTypeTmp[1] == "phong" ) $mayaShaderTmp[0] = $selTypeTmp[0]; else if ( $selTypeTmp[1] == "transform" ) { // Get the shape name. string $shapeTmp[] = `listRelatives -type "shape" $sel[0]`; print "Object = "; print $sel; //print "\n"; rename $shapeTmp[0] ($sel[0] + "Shape"); string $listShaders[] = `listConnections ($sel[0] + "Shape")`;//print "\n$sel[] = "; print $sel; print "\n"; // If there's no shader at all or if it's not a blinn or a phong then create a new shader. if ( $listShaders[0] == "" || $selTypeTmp[1] != "blinn" || $selTypeTmp[1] != "phong" ) { string $tmp = `shadingNode -asShader blinn -name $shader`; $mayaShaderTmp[0] = $tmp; string $shdrSG = `sets -renderable true -noSurfaceShader true -empty -name ($tmp + "SG")`; //print ("\n$shdrSG = " + $shdrSG) ; connectAttr -f ($tmp + ".outColor") ($shdrSG + ".surfaceShader"); sets -e -forceElement $shdrSG $sel[0]; } } // Create a new unconnected shader (this is in case the user didn't use a blin or phong shader). else { string $tmp = `shadingNode -asShader blinn -name $shader`; $mayaShaderTmp[0] = $tmp; string $shdrSG = `sets -renderable true -noSurfaceShader true -empty -name ($tmp + "SG")`; //print ("\n$shdrSG = " + $shdrSG) ; connectAttr -f ($tmp + ".outColor") ($shdrSG + ".surfaceShader"); warning (" -> " + $tmp + " is a new shader; it is not connected!"); } string $mayaShader = $mayaShaderTmp[0]; print ("Maya shader name (at creation): " + $mayaShader + "\n"); // Add the Crysis attributes to the shader. string $cryTemplate = "cryTemplate"; string $cryPhysicsMaterial = "cryPhysicsMaterial"; if (!attributeExists ($cryTemplate, $mayaShader)) addAttr -ln $cryTemplate -sn "cryT" -dt "string" -storable true $mayaShader; if (!attributeExists ($cryPhysicsMaterial, $mayaShader)) addAttr -ln $cryPhysicsMaterial -sn "cryP" -dt "string" -storable true $mayaShader; if (!attributeExists ("crysisMaterialInfo", $mayaShader)) addAttr -ln "crysisMaterialInfo" -sn "crysisMat" -dt "string" -storable true $mayaShader; setAttr -type "string" ($mayaShader + "." + $cryTemplate) $shader; setAttr -type "string" ($mayaShader + "." + $cryPhysicsMaterial) $surfaceType; // Add extra attributes mirroring the ones in Sandbox2. if (!attributeExists ("Opacity", $mayaShader)) addAttr -ln "Opacity" -sn "cryOpac" -dt "string" -storable true $mayaShader; if (!attributeExists ("DiffuseColor", $mayaShader)) addAttr -ln "DiffuseColor" -sn "cryDifCol" -dt "string" -storable true $mayaShader; if (!attributeExists ("SpecularColor", $mayaShader)) addAttr -ln "SpecularColor" -sn "crySpecCol" -dt "string" -storable true $mayaShader; if (!attributeExists ("Glossiness", $mayaShader)) //addAttr -ln "Glossiness" -sn "cryGlos" -at long -storable true $mayaShader; addAttr -ln "Glossiness" -sn "cryGlos" -at long -min 0 -max 255 $mayaShader; if (!attributeExists ("EmissiveColor", $mayaShader)) addAttr -ln "EmissiveColor" -sn "cryEmisCol" -dt "string" -storable true $mayaShader; // Replace the name of the crysis material by the Maya shader's name. string $buffer[]; int $numTokens = `tokenize $materialInfo "\"" $buffer`; string $crysisMaterialName = ""; for ($i=0;$i<`size $buffer`;$i++) { if ( `match " 1 ) chooseShader( $mtlFile, $listOfMaterials ); else { $materialInfo = getMaterialInfo( $mtlFile, $listOfMaterials[0] ); //print $materialInfo; string $shaderInfo[] = extractMapsInfo( $materialInfo ); //print "$shaderInfo = "; print $materialInfo; createShader( $shaderInfo, $materialInfo ); } }