A getattr lookup is probably the most straightforward option:

theClass = 'Card2'
nodeConstructor = getattr(nuke.nodes, theClass, None)
if nodeConstructor is not None:
    card = nodeConstructor()

You could condense the last 3 lines into "getattr(nuke.nodes, theClass)()" if 
you know your class name will never be invalid, but that’s slightly risky.

Alternatively, you could simply clear the node selection before calling 
nuke.createNode()

theClass = 'Card2'
nukescripts.clear_selection_recursive()
card = nuke.createNode(theClass)

There are some other creative solutions too, but those are probably the most 
straightforward.

-Nathan



From: j00ey 
Sent: Wednesday, July 06, 2011 2:38 PM
To: [email protected] 
Subject: [Nuke-python] how to use nuke.nodes to create nodes with 
stringvariables

Hi all
I'm starting out in python so this is very simple one. I'm trying to write a 
simple proc that will create a Card or a Card3D depending on user input. I can 
get it to work with nuke.createNode but if the current selection is eg a read 
node the card gets connected to it which I don't want.

I know I can create an unconnected node with eg nuke.nodes.Card2() but I want 
to pass user input as the class and I can't figure out the syntax.

Can anyone help? Thanks in advance.


##query user for Card or Card3D

type=nuke.getInput('Card or Card3D','Card')

if (type=='card') | (type=='Card'):
cardClass='Card2'
elif (type=='card3D') | (type=='Card3D'):
cardClass='Card3D'

#card=nuke.createNode(cardClass)
card=nuke.nodes...


--------------------------------------------------------------------------------
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python

Reply via email to