Scripting mel to grab the last selected node


#1

So, I have a script I’m working on. It is a mel script so although python may be easier, I can’t use it in this case.
The script I’m working on changes attributes on a redshift shader.

Redshift shaders do bump maps differently than vray or mental ray. Basically you have to create a bump node (or normal node), connect that to the bump slot, and then do all the adjustments to the bump within that node.

So here is my question. How do I get the script to find the node I created last? For instance, if I’ve created 5 bump nodes already the sixth bump node will be called “rsBumpMap6” or whatever number map it is. But every scene is different.

So how do I write a script that says “create a new bump node, and then connect it over here” when I won’t know what that newly created node will be called? I feel like there must be a simple solution to this but I can’t think of one.

Thanks!


#2

With strict naming conventions there is very high predictibility of the new node names.
Just keep the resulting name in a variable. This would be like

string $bump = `createNode bump2d -name "greatestBumpNodeEver_b2d"` ;
connectAttr ("coolTexture.outAlpha") ($bump + ".bumpValue") ;
connectAttr ($bump + ".outNormal") "whateverMaterial.bump" ;

First you name them at creation, never ever leave to the malefic forces to name it, second, have it in a variable so you can work with it. Even if there is a nameclash, or it gets created on a different namespace, using the variable will always work, however if you rename it you will have to assign it again:

$bump = `rename $bump "newBumpiness"` ;

#3

This is very useful. I did not know you could do this.

Thanks a lot!!


#4

just a quick correction:
if you make the bump node with createNode command it won’t show up in the hypershade in the utility nodes tab. if the node gets disconnected it may linger on in the scene and nobody would know.
So instead of createNode use
string $bump= shadingNode -asUtility bump2d -name "yadayadayada" ;

and make sure you are using the quote near the 1 key.