-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhoudini_python_snippets.py
299 lines (249 loc) · 8.6 KB
/
houdini_python_snippets.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# create point with hardcoded centroid values
n = hou.selectedNodes()[0]
pos = n.position()
add = n.parent().createNode("add")
add.setPosition((pos[0],pos[1]-1))
add.parm("usept0").set(1)
axis = ['x','y','z']
for a in axis:
t = 'centroid("'+n.path()+'",D_'+a.upper()+')'
add.parm("pt0"+a).setExpression(t)
add.parm("pt0"+a).deleteAllKeyframes()
# -----------------------------------------------------
# Freeze Bounds
# create box with hardcoded bounds values
n = hou.selectedNodes()[0]
pos = n.position()
box = n.parent().createNode("box")
box.setPosition((pos[0],pos[1]-1))
axis = ['x','y','z']
for a in axis:
s = 'bbox("'+n.path()+'",D_'+a.upper()+'SIZE)'
box.parm("size"+a).setExpression(s)
box.parm("size"+a).deleteAllKeyframes()
t = 'centroid("'+n.path()+'",D_'+a.upper()+')'
box.parm("t"+a).setExpression(t)
box.parm("t"+a).deleteAllKeyframes()
# -----------------------------------------------------
# create an object merge with ref from sected node (Ctrl+!)
for i in hou.selectedNodes():
m = i.parent().createNode("object_merge")
o = "IN_"+i.name()+"_"
n = 0
for j in i.parent().children():
if j.name().startswith(o):
try:
n = max(n,int(float(j.name().split("_")[-1])))
except:
n=0
m.setName(o + str(n+1))
m.setPosition((i.position()[0],i.position()[1]-1))
m.setColor(hou.Color((0.1,2,0.1)))
m.parm("objpath1").set(m.relativePathTo(i))
# -----------------------------------------------------
# align nodes and dots to grid (alt+a)
for i in hou.selectedItems():
p = i.position()
x = p[0]
y = p[1]
if isinstance(i,hou.Node):
x2 = round(x*.5,0)*2-0.5
x3 = round(x*.5+1,0)*2-0.5
y2= round(y)-0.15
y3= round(y+1)-0.15
x_ = x3
if(abs(x2-x)<abs(x3-x)):x_=x2
y_ = y3
if(abs(y2-y)<abs(y3-y)):y_=y2
if isinstance(i,hou.NetworkDot):
x1 = math.floor(x/2)*2
x2 = math.ceil(x/2)*2
x_ = x1
if(abs(x-x2)<abs(x-x1)):
x_ = x2
y_ = round(y)
i.setPosition((x_,y_))
# -----------------------------------------------------
def find_displayed_node():
# find current network editor pane
# find node with display flag ON
for pane in hou.ui.currentPaneTabs():
if isinstance(pane,hou.NetworkEditor):
for node in pane.pwd().children():
display_flag = None
try:
display_flag = node.isDisplayFlagSet()
except Exception:
pass
if display_flag:
return node
return None
# -----------------------------------------------------
# get current camera (python)
def getCurrentCamera():
for i in hou.ui.currentPaneTabs():
if i.type().name()=="SceneViewer":
return i.curViewport().camera().path()
break
# -----------------------------------------------------
# walk up the node tree (recursive)
def branch_up(node):
inputs = node.inputs()
if len(inputs)==0:
return node
else:
#default to first input unless switch node
selected_input = inputs[0]
if node.type().name()=="switch":
selected_input = inputs[node.evalParm("input")]
return branch_up(selected_input)
# -----------------------------------------------------
# walk down chain of nodes and get the last node
# useful to trigger a ROP job, from SOP
def branch_down(node):
out = node.outputs()
if len(out)==0:
return node
else:
return branch_down(out[0])
# -----------------------------------------------------
# sim time
# --------
import os
from datetime import datetime, timedelta
selected_nodes = hou.selectedNodes()
cache_node_type = "file";
cache_file_parm = "file";
file_extension = "bgeo.sc"
def window(array, window_size):
out = []
for i in range(0,int(len(array)/window_size)):
x = array[i*2]
y = array[i*2+1]
out.append([x,y])
return out
def get_time(dir,file):
return os.path.getmtime(os.path.join(dir, file))
def print_sim_time() -> None:
if selected_nodes:
node = selected_nodes[0]
if node.type().name() == cache_node_type:
file = node.evalParm(cache_file_parm)
dir = os.path.dirname(file)
try:
files = [file for file in os.listdir(dir) if file.endswith(file_extension)]
except Exception:
return
files.sort(
key=lambda x: os.path.getmtime(os.path.join(dir, x)), reverse=False
)
for fwin in window(files,2):
t0 = get_time(dir,fwin[0])
t1 = get_time(dir,fwin[1])
# time difference in seconds
time_diff_seconds = abs(t0 - t1)
# convert to timedelta object
time_diff = timedelta(seconds=time_diff_seconds)
frame = fwin[0].split(".")[1]
print(f"frame: {frame} > time: {str(time_diff)}")
# creation times for first/last files
t0 = get_time(dir,files[0])
t1 = get_time(dir,files[-1])
# time difference in seconds
time_diff_seconds = abs(t0 - t1)
# convert to timedelta object
time_diff = timedelta(seconds=time_diff_seconds)
print(f"\n> total cache time: {str(time_diff)}")
print_sim_time()
# ------------------------------------------------
# get lastest written bgeo.sc and go to that frame
#
import os
selected_nodes = hou.selectedNodes()
cache_node_type = "file";
cache_file_parm = "file";
file_extension = "bgeo.sc"
if selected_nodes:
node = selected_nodes[0]
if node.type().name() == cache_node_type:
file = node.evalParm(cache_file_parm)
dir = os.path.dirname(file)
try:
files = [file for file in os.listdir(dir) if file.endswith(file_extension)]
except Exception:
files = []
if len(files)>0:
files.sort(
key=lambda x: os.path.getmtime(os.path.join(dir, x)), reverse=True
)
newest_file = os.path.basename(files[0])
last_frame = float(newest_file.split(".")[1])
hou.setFrame(last_frame)
# ---------------------------------------------------
# bake mask values (from Paint SOP) as a wrangle node
#
import re
me = hou.pwd()
geo = me.inputs()[0].geometry()
# set floating point decimals
def prec(f:float,precision:int) -> float:
p = pow(10,precision);
return int(f*p)/p;
# store point positions and mask values: [[P.x,P.y,P.z,mask],...]
data=[]
for pt in geo.points():
p=pt.attribValue("P")
m=pt.attribValue("mask")
dec_p = me.evalParm("dec_p")
dec_m = me.evalParm("dec_m")
x = [prec(p[0],dec_p),prec(p[1],dec_p),prec(p[2],dec_p),prec(m,dec_m)]
data.append(x)
# create wrangle snippet string
s = str(data)
s = re.sub(r"\[","{",s)
s = re.sub(r"\]","}",s)
snippet = f"vector4 a[]={s};"
createpoints="""
foreach(vector4 v;a){
vector p_ = set(v[0],v[1],v[2]);
int n=addpoint(0,p_);
setpointattrib(0,"mask",n,v[3]);
}"""
snippet+=f"\n{createpoints}"
# create wrangle and set snippet string
wrangle = me.parent().createNode("attribwrangle")
pos = me.position()
wrangle.setPosition((pos[0],pos[1]-1))
wrangle.parm("class").set(0)
wrangle.parm("snippet").set(snippet)
wrangle.setDisplayFlag("on")
wrangle.setSelected("on")
# -----------------------------------------------------
# get all SOP nodes of type "subnet"
subnets = hou.sopNodeTypeCategory().nodeTypes()["subnet"].instances()
# -----------------------------------------------------
# get all SOP nodes types containing a string
# used for nodes with version "baked in" the name: studio::mynode::5.0
def get_nodetypes_and_versions(node_type_name:str):
node_types_list = []
node_types = hou.nodeTypeCategories()["Sop"].nodeTypes()
for node_type in node_types:
if(node_type_name in node_type):
node_types_list.append(node_type)
return node_types_list
"""
>>>get_nodetypes_and_versions("voronoifracture")
voronoifracture
voronoifracture::2.0
voronoifracturepoints
"""
# -----------------------------------------------------
# set enviroment variables
env_vars = {
"WEDGE":0,
"CLUSTER":0
}
for varname,value in env_vars.items():
hou.hscript(f"set -g {varname} = {value}; varchange")
print(f"var: {varname}\tvalue:{value}")
# -----------------------------------------------------