Checking if a Node is a Gizmo
There are several ways to find if a certain node is a gizmo or not in Nuke. Here is a list of the most common ways to achieve that and their respective pitfalls if any.
Method I
This method basically checks for the gizmo_file knob on a node which is a knob that should only be present in nodes that are gizmos. The downside of this method is that if a user happens to add a user knob named gizmo_file to a node or group for some mysterious reason these will be listed as gizmos even though they aren't.
def isGizmo(node):
'''checks if a node is a gizmo'''
gizmo = 'gizmo_file' in node.knobs()
return gizmo
print isGizmo(nuke.selectedNode()) # to test this
Method II
This method compares the node's type output and doesn't check for class inheritance.
def isGizmo(node):
'''checks if a node is a gizmo'''
gizmo = type(node) == nuke.Gizmo
return gizmo
print isGizmo(nuke.selectedNode()) # to test this
Method III
Practically the same as method II but instead of matching the node type directly it checks for it's inheritance.
def isGizmo(node):
'''checks if a node is a gizmo'''
gizmo = isinstance(node, nuke.Gizmo)
return gizmo
print isGizmo(nuke.selectedNode()) # to test this
Method IV
This method checks if the class of the node being checked can be found on the environment (user, NUKE_PATH, install dir). The pitfall here is that if the gizmo was imported trough the import script command and isn't present in the environment it will return false even though the node in question is in fact a gizmo.
def isGizmo(node):
'''checks if a node is a gizmo'''
gizmo = (node.Class() + ".gizmo") in nuke.plugins(nuke.ALL | nuke.NODIR)
return gizmo
print isGizmo(nuke.selectedNode()) # to test this
Getting a list of Gizmos in a DAG
If you need to get a list of all the gizmos in a DAG you can do a for loop on all nodes and check for their classes using any of the isGizmo() functions mentioned above.
gizmos = [n for n in nuke.allNodes() if isGizmo(n)]
print gizmos