Notifications
Clear all

[Closed] Here is a function to help you with nodes

Hi everyone.
If you program alot of scripts a common situation to handle is nodes. I always want my functions to be as flexible as possible so I can throw in undefined nodes, selections sets or simple nodes. To simplify this I wrote a function that you can put in all situation when you are working with nodes.

 If sel is the variable, it can be of 4 different types:
 - undefined. If this happens a MessageBox will say "No Selection" You can disable this by writing warning:false after the function call
 - SelectionSet. If you write $selection you always get a selectionSet. If you write $ you can get a selectionset but also a node or undefined.
 - Array. You may have created an array of nodes
 - Node. Such as $sphere
 
 Here is the function:
fn IOI_NodeTest &sel warning:true=
     fn IOI_NodeTest &sel warning:true=
  (
  	--Function to handle nodes automatically
  	--Sel can be Undefined, Node, Array, ObjectSet, Pathname (example $sphere*)
  	
  	tReturn=true
  	if IsValidNode sel do sel=#(sel)
  	if sel==undefined then 
  		tReturn=false
  	else
  		if sel.count==0 do tReturn=false
  	
  	if tReturn==false and warning do
  		MessageBox "No Selection" title:"Note"
  		
  	tReturn
  )
  
--Example of usage:
     for i=1 to 10 do Sphere() --Create 10 spheres
     
     --Setting different object types. Test them all.
     sel=$sphere01
     --sel=undefined
     --sel=$sphere*
 --sel=$noobject* --this is an empty array, so the function should return false
     --sel=#($sphere01, $sphere02)
     
     --The Core Code
     if IOI_NodeTest &sel do
     	for iObj=1 to sel.count do
     	(
     		--Code
     		print sel[iObj].name
     	)
 To use the NodeTest function you should put the function in a script in the Scripts/Startup folder. This way it will always be accessible.
 If you distribute your plugins don't forget to include it as well.
 Happy Coding!
1 Reply

I made a small update so that the function also returns False, if we have empty arrays of objects. For example if you write $sphere* and there is no spheres in the scene, you get an empty array.
/Andreas