forked from jedypod/nuke-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbake_gizmos.py
More file actions
183 lines (148 loc) · 6.62 KB
/
bake_gizmos.py
File metadata and controls
183 lines (148 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from __future__ import with_statement
import nuke, os, re
import nukescripts
nuke.menu('Nuke').addCommand('Edit/Node/Bake Gizmos to Groups', 'bake_gizmos.bake_selected_gizmos()', 'ctrl+alt+shift+meta+g')
def get_all_nodes(topLevel):
'''
recursively return all nodes starting at topLevel. Default topLevel is nuke.root()
'''
allNodes = nuke.allNodes( group=topLevel )
for n in allNodes:
allNodes = allNodes+get_all_nodes( n )
return allNodes
def get_outputs(gizmo):
'''
Return a dictionary of the nodes and pipes that are connected to node
'''
depDict = {}
# There appears to be a bug with the dependent() function where sometimes the first time you run it after a nuke script is opened, it returns nothing.
# To work around this we will call dependent twice, just in case it's the first gizmo we're baking in the script.
tmp = gizmo.dependent( nuke.INPUTS | nuke.HIDDEN_INPUTS )
dependents = gizmo.dependent( nuke.INPUTS | nuke.HIDDEN_INPUTS )
del(tmp)
# Iterate through each node that is connected to the gizmo
for dep in dependents:
# Add each dependency as a dictionary key, with a blank list
# The list will contain what input of the dep is connected to our gizmo
depDict[dep] = []
# Loop through each input of dep, check if that input is connected to gizmo
# If so, add the input number to the list that is the value of the dep key in the dict
for i in range( dep.inputs() ):
if dep.input( i ) == gizmo:
depDict[ dep ].append( i )
return depDict
def is_gizmo(node):
'''
return True if node is gizmo
'''
return 'gizmo_file' in node.knobs()
def gizmo_is_default(gizmo):
'''Check if gizmo is in default install path'''
installPath = os.path.dirname(nuke.EXE_PATH)
gizmoPath = gizmo.filename()
if gizmoPath:
installPathSet = set(installPath.split('/'))
gizmoPathSet = set(gizmoPath.split('/'))
gizmoPathSet.issubset(installPathSet)
gizmo_is_default = os.path.commonprefix([installPath, gizmoPath]) == installPath
else:
gizmo_is_default = False
return gizmo_is_default
def get_parent( n ):
'''
return n's parent node, return nuke.root()n if on the top level
'''
return nuke.toNode( '.'.join( n.fullName().split('.')[:-1] ) ) or nuke.root()
def bake_gizmo( gizmo ):
'''
copy gizmo to group and replace it in the tree, so all inputs and outputs use the new group.
returns the new group node
'''
parent = get_parent( gizmo )
gizmo_outputs = get_outputs(gizmo)
gizmo_inputs = gizmo.inputs()
print "INPUTS:", gizmo_inputs #[inputnodes.name() for inputnodes in gizmo_inputs]
print "OUTPUTS:", gizmo_outputs #[output_nodes.name() for output_nodes in gizmo_outputs]
# This old method can't detect failures
#groupName = nuke.tcl( 'global no_gizmo; set no_gizmo 1; in %s {%s -New} ; return [value [stack 0].name]' % ( parent.fullName(), gizmo.Class() ) )
for n in get_all_nodes(nuke.root()):
n.setSelected(False)
try:
nuke.tcl('copy_gizmo_to_group {0}'.format(gizmo.fullName()))
#group = nuke.toNode( '.'.join( (parent.fullName(), groupName) ) )
## We will use the selected node to get the created group
group = nuke.selectedNode()
if gizmo_outputs:
#RECONNECT OUTPUTS IF THERE ARE ANY
for node, pipes in gizmo_outputs.iteritems():
for i in pipes:
node.setInput( i, group )
#RECONNECT INPUTS
for i in range( gizmo.inputs() ):
group.setInput( i, gizmo.input( i ) )
group.setSelected( False )
except RuntimeError:
# Occurs if the gizmo was sourced: "RuntimeError: This gizmo was created with a "load" or "source" command. Copy to group does not work for it."
print "This gizmo was created with a 'load' or 'source' command. Manually re-creating it..."
with parent:
# gizmo.Class() fails because the gizmo class is defined as "Gizmo" - super hacky but we'll try to use the node's name
gizmo_guess_class = re.split('[0-9]*$', gizmo.name())[0]
tmp_gizmo = nuke.createNode(gizmo_guess_class, inpanel=False)
nuke.tcl('copy_gizmo_to_group {0}'.format(tmp_gizmo.fullName()))
# fix bug where the copy_gizmo_to_group puts node outside of the parent: cut node outside group, paste node inside group
nuke.nodeCopy(nukescripts.cut_paste_file()); nukescripts.node_delete(popupOnError=True)
with parent:
nuke.nodePaste(nukescripts.cut_paste_file())
group = nuke.selectedNode()
nuke.delete(tmp_gizmo)
group.setSelected( False )
group.hideControlPanel()
if gizmo_outputs:
#RECONNECT OUTPUTS IF THERE ARE ANY
for node, pipes in gizmo_outputs.iteritems():
for i in pipes:
node.setInput( i, group )
#RECONNECT INPUTS
for i in range( gizmo_inputs ):
group.setInput( i, gizmo.input( i ) )
group.setXYpos( gizmo.xpos(), gizmo.ypos() )
# COPY VALUES
group.readKnobs( gizmo.writeKnobs(nuke.TO_SCRIPT) )
gizmoName = gizmo.name()
nuke.delete( gizmo )
group.setName(gizmoName)
return group
def bake_gizmos(topLevel=nuke.root(), exclude_default=True):
# Store the selected Nodes, we store names because the bake can mess up the python binding
names = [n.name() for n in nuke.selectedNodes()]
for n in get_all_nodes(topLevel):
n.setSelected(False)
for n in get_all_nodes(topLevel):
try:
if is_gizmo(n):
if not gizmo_is_default(n):
# ALWAYS BAKE CUSTOM GIZMOS
print "Baking:", n.fullName()
bake_gizmo(n)
elif not exclude_default:
# BAKE NON-DEFAULT GIZMOS IF REQUESTED
bake_gizmo(n)
except ValueError:
pass
# Reselect the selected Nodes
for node in nuke.allNodes():
node.setSelected(False)
for name in names:
node = nuke.toNode(name)
if node:
node.setSelected(True)
def bake_selected_gizmos(topLevel=nuke.root(), exclude_default=True):
nodes = [n for n in nuke.selectedNodes() if is_gizmo(n)]
for node in nodes:
if not gizmo_is_default(n):
# ALWAYS BAKE CUSTOM GIZMOS
print "Baking:", n.fullName()
bake_gizmo(node)
elif not exclude_default:
# BAKE NON-DEFAULT GIZMOS IF REQUESTED
bake_gizmo(node)