help with script to create a shading network for mia x

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
  04 April 2013
help with script to create a shading network for mia x

Hi, I am hoping someone could help me out with this script, i 'm pretty new to mel and this is my second script that i created, It's for a project where i have to create a shading network connecting the diffuse, weight, reflection, etc but in some cases i dont need all of them so i created a window with a checker-box option to tell maya to just give me the ones i want and connect it to the network. The problem is that no matter if i check on the check-boxes the ones i want, it still creates all of them but the unchecked ones come out disconnected from the network. Thanks in advance for the help

global proc JS_shadingNetwork()

{
string $myWindow = "JS_shadingNetwork";
if (`window -exists $myWindow`) deleteUI $myWindow;
if (`windowPref -exists $myWindow`) windowPref -e -widthHeight 200 150 $myWindow;

window $myWindow;
columnLayout;
rowColumnLayout -nc 1 -columnAttach 1 "right" 0
-columnWidth 1 200 -columnWidth 2 2;
text -label "Material Name";
separator -height 10 -style "in";
textField "inputText";
separator -height 10 -style "in";
setParent..;

rowColumnLayout -nc 2-cw 1 100 -cw 2 100 -cw 3 100;
checkBox "weight";
popupMenu -parent "weight" -alt true -ctl true;
menuItem; menuItem; menuItem;
checkBox "diffuse";
popupMenu -parent "diffuse" -alt true -ctl true;
menuItem; menuItem; menuItem;
checkBox "spec";
popupMenu -parent "spec" -alt true -ctl true;
menuItem; menuItem; menuItem;
checkBox "trans";
popupMenu -parent "trans" -alt true -ctl true;
menuItem; menuItem; menuItem;
checkBox "bump";
popupMenu -parent "bump" -alt true -ctl true;
menuItem; menuItem; menuItem;
setParent..;
columnLayout;
rowColumnLayout -nc 2 -cw 1 200 ;
separator -height 10 -style "in";
setParent..;
rowColumnLayout -nc 2 -cw 1 100 -cw 2 100;
button -h 40 -bgc 0.5 0.8 0.8 -l "Create Shader" -c "miaShader";
symbolButton -image "sphere.png" -c "createSphere";
setParent..;
rowColumnLayout -nc 2 -cw 1 200 ;
separator -height 10 -style "in";
setParent..;
rowColumnLayout -nc 2 -cw 1 60 -cw 2 120;
text -label "UV repeat";
separator -height 10 -style "none";
floatField -minValue 0 -maxValue 40 -precision 0 -step 1;
floatSlider -min -100 -max 100 -value 0 -step 1;
//separator -height 10 -style "in";
setParent..;
rowColumnLayout -nc 2 -cw 1 200 ;
separator -height 10 -style "in";
setParent..;
button -h 30 -bgc 0.9 0.45 0.45 -l " Delete Sphere ";
setParent..;

//setParent..;

showWindow;


}

global proc miaShader()
{
string $materialName = `textField -q -tx "inputText"`;
string $sgName;
string $textureFile;
string $fileName;
string $materialTypes[] = {"diffuse", "wght", "spec", "bump", "trns"};
int $i = 0;
string $fileName = $materialName+("_file_");
string $connection[];
string $bump2d;
string $prefix = $materialName;
string $place2dName;
string $shader;

int $weight = `checkBox -q -v "weight"`;
int $diffuse = `checkBox -q -v "diffuse"`;
int $spec= `checkBox -q -v "spec"`;
int $bump= `checkBox -q -v "bump"`;
int $trans= `checkBox -q -v "trans"`;

if(!`objExists $materialName`)
{
$shader = `shadingNode -as mia_material_x -n ($materialName)`;
$sgName = `sets -renderable true -noSurfaceShader true -empty -name ($materialName + "SG")`;

// connect Material to Shading Group
connectAttr ($materialName+".message") ($sgName+".miMaterialShader");
connectAttr ($materialName+".message") ($sgName+".miPhotonShader");
connectAttr ($materialName+".message") ($sgName+".miShadowShader");

setAttr ($materialName+".diffuse") -type double3 0.8374 0.8374 0.8374 ;
setAttr ($materialName+".diffuse_roughness") 0.292683;
setAttr ($materialName+".reflectivity") 0.1;
setAttr ($materialName+".refl_gloss") 0.232927;

for($type in $materialTypes)
{
//creating file texture nodes
$fileName = `shadingNode -asTexture file -n ($prefix+"_"+$type+"_file")`;

//connecting file texture nodes to shader
$place2dName = `shadingNode -asUtility place2dTexture -n ($prefix+"_"+$type+"_place2d")`;

//connecting 2d placement to file texture
connectAttr -f ($place2dName+".coverage") ($fileName+".coverage");
connectAttr -f ($place2dName+".translateFrame") ($fileName+".translateFrame");
connectAttr -f ($place2dName+".rotateFrame") ($fileName+".rotateFrame");
connectAttr -f ($place2dName+".mirrorU") ($fileName+".mirrorU");
connectAttr -f ($place2dName+".mirrorV") ($fileName+".mirrorV");
connectAttr -f ($place2dName+".stagger") ($fileName+".stagger");
connectAttr -f ($place2dName+".wrapU") ($fileName+".wrapU");
connectAttr -f ($place2dName+".wrapV") ($fileName+".wrapV");
connectAttr -f ($place2dName+".repeatUV") ($fileName+".repeatUV");
connectAttr -f ($place2dName+".offset") ($fileName+".offset");
connectAttr -f ($place2dName+".rotateUV") ($fileName+".rotateUV");
connectAttr -f ($place2dName+".noiseUV") ($fileName+".noiseUV");
connectAttr -f ($place2dName+".vertexUvOne") ($fileName+".vertexUvOne");
connectAttr -f ($place2dName+".vertexUvTwo") ($fileName+".vertexUvTwo");
connectAttr -f ($place2dName+".vertexUvThree") ($fileName+".vertexUvThree");
connectAttr -f ($place2dName+".vertexCameraOne") ($fileName+".vertexCameraOne");
connectAttr -f ($place2dName+".outUV") ($fileName+".uv");
connectAttr -f ($place2dName+".outUvFilterSize") ($fileName+".uvFilterSize");

if(($type =="diffuse")&&($diffuse))
{
// connect file to material
connectAttr -f ($fileName+".outColor") ($shader+".diffuse");
//connectAttr -f ($fileName+".outAlpha") ($shader+".diffuseA");
}

else if(($type =="wght")&&($weight))
{
// connect file to material
$connection = `listConnections -d off -s on ($shader+".diffuse_weight")`;
if ($connection[0] == "")
{
setAttr ($fileName+".alphaIsLuminance") true;
connectAttr -f ($fileName+".outAlpha") ($shader+".diffuse_weight");
}
}
else if(($type =="spec")&&($spec))
{
//setAttr ($fileName+".alphaIsLuminance") true;
$connection = `listConnections -d off -s on ($shader+".refl_color")`;
if ($connection[0] == "")
{
connectAttr -f ($fileName+".outColor") ($shader+".refl_color");
connectAttr -f ($fileName+".outAlpha") ($shader+".refl_colorA");
}
}

else if(($type =="bump")&&($bump))
{
if (!`objExists ($prefix+"_"+$type+"_2d")`)
{
$bump2d =`shadingNode -asUtility bump2d -n ($prefix+"_"+$type+"_2d")`;
setAttr ($fileName+".alphaIsLuminance") true;
connectAttr -f ($fileName+".outAlpha") ($bump2d+".bumpValue");
connectAttr -f ($bump2d+".outNormal") ($shader+".standard_bump");
}

}

else if(($type =="trns")&&($trans))
{
//setAttr ($fileName+".alphaIsLuminance") true;
$connection = `listConnections -d off -s on ($shader+".refr_color")`;
if ($connection[0] == "")
{
connectAttr -f ($fileName+".outColor") ($shader+".refr_color");
connectAttr -f ($fileName+".outAlpha") ($shader+".refr_colorA");
}
}

$i++;
}

}

}
 
  04 April 2013
These lines are not conditional on any of your checkBox ifs.

$fileName = `shadingNode -asTexture file -n ($prefix+"_"+$type+"_file")`;
$place2dName = `shadingNode -asUtility place2dTexture -n ($prefix+"_"+$type+"_place2d")`;

I would write a proc to handle the creation of the fileTexture and place2dTexture. And another proc to hook up the fileTexture to your shader. This will help avoid repeating code blocks in the if statements.

eg:

global proc string create_fileTexture(string $prefix, string $type) {
 	string $fileName = `shadingNode -asTexture file -n ($prefix+"_"+$type+"_file")`;
 	string $place2dName = `shadingNode -asUtility place2dTexture -n ($prefix+"_"+$type+"_place2d")`;
 	// and the rest of the place2dTexture stuff ...
 	return $fileName;
 }
 

Then one to do the connections where you supply the shader name and the fileTexture name and the source and target attr names. This one might need a bit of thought, since some attrs like bump need special treatment.

David
__________________
http://www.djx.com.au
 
  04 April 2013
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 07:18 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.