View Full Version : automating material setup
dutch_delight 10-14-2008, 12:10 PM Hi everyone,
I am trying to get into mel programming but I havent got a clue where to start. I have a task that i am constantly repeating so was wandering if anyone would be kind enough to give me some pointers.
This is what I've got:
layered texture with 2 materials already assigned.
cubemap.
file.
I want to select all 3 of these and then press a magic button that will do this:
Create a slot 0 in the layered material and move the existing textures up a slot
Assign the cubemap to slot 0 and make it ADDitive
Assign the file as the input 0 alpha.
Any help would be greatly appreciated.
|
|
dutch_delight
10-14-2008, 03:32 PM
I now realise that re-arranging the layermaterials is not going to be easy. I've got this sofar:
string $selection[] = `ls -selection`;
global string $envi_material = "";
global string $laye_material = "";
global string $file_material = "";
for ($c in $selection)
{
string $id = `nodeType $c`;
if($id == "envCube" )
$envi_material = $c;
if($id == "layeredTexture" )
$laye_material = $c;
if($id == "file" )
$file_material = $c;
}
//assign file to newly created slots' input alpha
if (size($laye_material))
{
if (size($file_material))
{
connectAttr -f $file_material.outAlpha $laye_material.inputs[0].alpha;
}
}
Not fantastic code but this is the first i've written in mel.
I'm getting this error: Error: Invalid object or value: layeredTexture282 //
This is because the $laye_material is not recognised as a node.
Any suggestions anyone?
tbaypaul
10-14-2008, 04:57 PM
at first glance, looks like string problems...
connectAttr -f ($file_material + ".outAlpha") ($laye_material + ".inputs[0].alpha");
Would it not be easier to have a button create the shading network first? Then all you'd have to do is go fill in the locations to the textures you want to use...
Also, is this a layered shader, or a layered texture? You say you have two materials already hooked up, and when I hear material, I assume blinn's, phong's, etc..
greatPumpkin
10-14-2008, 11:58 PM
rather then re-arranging the connections, you could just record the initial order, overwrite the first connection with your new cube map, and then reconnect your previous existing connections by stacking them up +1 plug. since you have more than when you started you can just overwrite the existing ones.
string $currentLayers[] = `listConnections -p 1 $laye_matrial.inputs`;
connectAttr -f ($file_material + ".outAlpha") ($laye_material + ".inputs[0].alpha");
for($i = 0 ;$i < `size $currentLayers`;$i++)
{
connectAttr -f $currentLayers[$i] ($laye_material + ".inputs[" + ($i+1) + "]")
}
for instance something like this...
global proc makeFileNodes(string $sFileNode, string $sFile2DTexNode)
{
// this just hooks up all the defaults for the file node and associated place2DTexture node
shadingNode -asTexture file -n $sFileNode;
shadingNode -asUtility place2dTexture -n $sFile2DTexNode;
connectAttr -f ($sFile2DTexNode + ".coverage") ($sFileNode + ".coverage");
connectAttr -f ($sFile2DTexNode + ".translateFrame") ($sFileNode + ".translateFrame");
connectAttr -f ($sFile2DTexNode + ".rotateFrame") ($sFileNode + ".rotateFrame");
connectAttr -f ($sFile2DTexNode + ".mirrorU") ($sFileNode + ".mirrorU");
connectAttr -f ($sFile2DTexNode + ".mirrorV") ($sFileNode + ".mirrorV");
connectAttr -f ($sFile2DTexNode + ".stagger") ($sFileNode + ".stagger");
connectAttr -f ($sFile2DTexNode + ".wrapU") ($sFileNode + ".wrapU");
connectAttr -f ($sFile2DTexNode + ".wrapV") ($sFileNode + ".wrapV");
connectAttr -f ($sFile2DTexNode + ".repeatUV") ($sFileNode + ".repeatUV");
connectAttr -f ($sFile2DTexNode + ".offset") ($sFileNode + ".offset");
connectAttr -f ($sFile2DTexNode + ".rotateUV") ($sFileNode + ".rotateUV");
connectAttr -f ($sFile2DTexNode + ".noiseUV") ($sFileNode + ".noiseUV");
connectAttr -f ($sFile2DTexNode + ".vertexUvOne") ($sFileNode + ".vertexUvOne");
connectAttr -f ($sFile2DTexNode + ".vertexUvTwo") ($sFileNode + ".vertexUvTwo");
connectAttr -f ($sFile2DTexNode + ".vertexUvThree") ($sFileNode + ".vertexUvThree");
connectAttr -f ($sFile2DTexNode + ".vertexCameraOne") ($sFileNode + ".vertexCameraOne");
connectAttr ($sFile2DTexNode + ".outUV") ($sFileNode + ".uv");
connectAttr ($sFile2DTexNode + ".outUvFilterSize") ($sFileNode + ".uvFilterSize");
}
global proc layeredTexSetup()
{
string $sLTex = "LTex_Name";
string $sCubeMap = $sLTex + "_CubeMap_Name";
string $sCubeMap3DNode = $sLTex + "_CubeNode_Name";
string $sAlphaTex = $sLTex + "_AlphaFile";
string $sFile1 = $sLTex + "_File1";
string $sFile2 = $sLTex + "_File2";
string $sPlace2D = "_Place2DNode";
//----------------------------------------------
// create the nodes
//----------------------------------------------
//layered texture
shadingNode -asTexture layeredTexture -n $sLTex;
//cubeMap (assuming envCube)
shadingNode -asTexture envCube -n $sCubeMap;
shadingNode -asUtility place3dTexture -n $sCubeMap3DNode;
connectAttr ($sCubeMap3DNode+".wim[0]") ($sCubeMap+".pm");
//Maps (assuming fileNodes)
makeFileNodes($sAlphaTex,($sAlphaTex+$sPlace2D));
makeFileNodes($sFile1,($sFile1+$sPlace2D));
makeFileNodes($sFile2,($sFile2+$sPlace2D));
//----------------------------------------------
// connect the nodes
//----------------------------------------------
//cube map
connectAttr -f ($sCubeMap+".outColor") ($sLTex+".inputs[0].color");
// get rid of the temp input created automatically
removeMultiInstance -break true ($sLTex+".inputs[1]");
//file nodes
connectAttr -f ($sAlphaTex+".outAlpha") ($sLTex+".inputs[0].alpha");
connectAttr -f ($sFile1+".outColor") ($sLTex+".inputs[1].color");
connectAttr -f ($sFile2+".outColor") ($sLTex+".inputs[2].color");
}
layeredTexSetup();
dutch_delight
10-15-2008, 09:47 AM
Thanks for the replies, much appreciated.
This is an example file:
http://www.richterdesigns.co.uk/stuff/material_setup.mb
lambert2 is what I've got.
lambert3 is what I need lambert2 to look like but with the layered texture used by lambert2
Norb, I'm getting a lot of errors. (Probably my lack of understanding mel) I'll have another look to see what the problem is later. It's a layered texture,not a shader. and I've got 2 (file)textures assigned to that. This work is conversion work so I dont want to start creating new layered textures. The less I mess around with the scene, the better.
GreatPumpkin, I've added your code but i'm getting an error, not sure why that is.
// Error: Connection not made: 'materialInfo1.texture[0]' -> 'layeredTexture1.inputs[1]'. Data types of source and destination are not compatible. //
// Error: The attribute 'layeredTexture1.inputs[1]' cannot be connected to 'materialInfo1.texture[0]'. //
string $selection[] = `ls -selection`;
global string $env_material = "";
global string $layer_material = "";
global string $file_material = "";
for ($c in $selection)
{
string $id = `nodeType $c`;
if($id == "envCube" )
$env_material = $c;
if($id == "layeredTexture" )
$layer_material = $c;
if($id == "file" )
$file_material = $c;
}//selection
string $currentLayers[] = `listConnections -p 1 $layer_material.inputs`;
for($i = 0 ;$i < `size $currentLayers`;$i++)
{
connectAttr -f $currentLayers[$i] ($layer_material + ".inputs[" + ($i+1) + "]");
}
connectAttr -f ($file_material + ".outAlpha") ($layer_material + ".inputs[0].alpha");
I think this might be what you want. I've included what is going on at the top of the script.
global proc string fixLTex()
{
// selected item types are 'envCube' 'fileNode' 'layeredTexture'
// layered texture contains 2 files in slots 0 and 1
// envCube gets put into input[0].color
// input[0] blending mode is set to additive
// fileNode gets put into input[0].alpha
// the existing fileNodes in the layered texture get bumped to inputs 1 and 2
string $saSel[] = `ls -sl`;
string $sLTex;
string $sEnvCube;
string $sFileNode;
string $saLTexConnections[];
if(size($saSel) != 3)
error "Select 3 items only.\n";
// parse the selection
for($sSel in $saSel)
{
if(`nodeType $sSel` == "envCube")
$sEnvCube = $sSel;
else if(`nodeType $sSel` == "file")
$sFileNode = $sSel;
else if(`nodeType $sSel` == "layeredTexture")
{
$sLTex = $sSel;
$saLTexConnections = `listConnections -p 1 -s 1 -d 0 $sSel`;
}
else
return ("Invalid Node: " + $sSel);
}
// connect the envCube to input[0]
connectAttr -f ($sEnvCube+".outColor") ($sLTex+".inputs[0].color");
// set it to additive blending
setAttr ($sLTex+".inputs[0].blendMode") 4;
//connect file to input[0].alpha
connectAttr -f ($sFileNode+".outAlpha") ($sLTex+".inputs[0].alpha");
// move the pre-existing file nodes to inputs 1 and 2
int $i;
for($i=0;$i<size($saLTexConnections);$i++)
{
connectAttr -f ($saLTexConnections[$i]) ($sLTex + ".inputs[" + ($i+1) +"].color");
}
return "Done.";
}
fixLTex();
dutch_delight
10-16-2008, 01:29 PM
You're a star!
Thanks very much for that. works like a dream.
CGTalk Moderation
10-16-2008, 01:29 PM
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.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.