-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathlabutils.py
More file actions
242 lines (193 loc) · 7.83 KB
/
labutils.py
File metadata and controls
242 lines (193 loc) · 7.83 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
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
import os
import hou
import uuid
import shutil
import json
try:
import requests
requests_enabled = True
except:
# requests library missing
requests_enabled = False
try:
from hutil.PySide.QtCore import QSettings
settings = QSettings("SideFX", "SideFXLabs")
except ImportError:
try:
from PySide2.QtCore import QSettings
settings = QSettings("SideFX", "SideFXLabs")
except:
settings = None
home = os.environ["HOUDINI_USER_PREF_DIR"]
config = os.path.join(home, "hcommon.pref")
GA_TRACKING_ID = "UA-2947225-9"
def can_send_anonymous_stats():
can_share = False
f = open(config, "r")
for line in f.readlines():
if line.startswith("sendAnonymousStats"):
if line.strip().strip(";").split(":=")[1].strip() == "1":
can_share = True
break
f.close()
override = os.getenv("HOUDINI_ANONYMOUS_STATISTICS", "1")
if int(override) == 0:
can_share = False
return can_share
def track_event(category, action, label=None, value=0):
# Generate a random user ID and store it as a setting per Google's guidelines
hou_uuid = uuid.uuid4()
if settings:
if settings.value("uuid"):
hou_uuid = settings.value("uuid")
else:
settings.setValue("uuid", hou_uuid)
data = {
'v': '1', # API Version.
'tid': GA_TRACKING_ID, # Tracking ID / Property ID.
# Anonymous Client Identifier. Ideally, this should be a UUID that
# is associated with particular user, device, or browser instance.
'cid': hou_uuid,
't': 'event', # Event hit type.
'ec': category, # Event category.
'ea': action, # Event action.
'el': label, # Event label.
'ev': value, # Event value, must be an integer
}
# Temporarily skips the rest because of a 'collections.abc' Python warning.
# Should remove this return when that warning is addressed.
return
if requests_enabled:
try:
response = requests.post('http://www.google-analytics.com/collect', data=data, timeout=0.1)
except:
pass
def like_node(node):
if can_send_anonymous_stats():
track_event("Like Events", "liked node", str(node.type().name()))
hou.ui.displayMessage("Thanks!\n We're glad you like using this tool.\n"
" Letting us know will help us prioritize which tools get focused on. ")
def dislike_node(node):
if can_send_anonymous_stats():
track_event("Like Events", "dislike node", str(node.type().name()))
hou.ui.displayMessage("Thanks!\n We're sorry you're not enjoying using this tool.\n"
" If you'd like to share your thoughts, please email us at support@sidefx.com. ")
# Temporary check to see if node in labs namespace
def is_labs_node(node):
name = node.type().name()
if name.startswith("labs::"):
return True
else:
return False
def send_on_create_analytics(node):
if can_send_anonymous_stats() and is_labs_node(node):
track_event("Node Created", str(node.type().name()), str(node.type().definition().version()))
def create_directory_if_not_exists(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def empty_directory_recursive(directory):
for file in os.listdir(directory):
file_path = os.path.join(directory, file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except:
pass
def extract_section_file(section, savelocation, writemode="wb"):
with open(savelocation, writemode) as SectionFile:
try:
SectionFile.write(section.contents())
except:
SectionFile.write(section.binaryContents())
def saveBackgroundImages(node, images):
theBackgroundImagesKey = 'backgroundimages'
result = []
for image in images:
image_dict = {
'path' : image.path(),
'rect' : [
image.rect().min().x(),
image.rect().min().y(),
image.rect().max().x(),
image.rect().max().y()
]
}
if image.relativeToPath():
image_dict['relativetopath'] = image.relativeToPath()
if image.brightness() != 1.0:
image_dict['brightness'] = image.brightness()
result.append(image_dict)
with hou.undos.group('Edit Background Images'):
if result:
node.setUserData(theBackgroundImagesKey, json.dumps(result))
else:
node.destroyUserData(theBackgroundImagesKey)
def add_network_image(network_editor, image_path, scale=0.4, embedded=False, relativeto_path=None, bounds=None):
image = hou.NetworkImage()
parent_path = network_editor.pwd().path()
if embedded:
data = None
with open(image_path, "rb") as file:
data = file.read()
hou.node(parent_path).setDataBlock(os.path.basename(image_path), data, '')
image.setPath("opdatablock:{}/{}".format(parent_path, os.path.basename(image_path)))
else:
image.setPath(image_path)
if relativeto_path:
image.setRelativeToPath(relativeto_path)
if not bounds:
bounds = network_editor.visibleBounds()
bounds.expand((-bounds.size()[0]*scale, -bounds.size()[1]*scale))
image.setRect(bounds)
background_images = network_editor.backgroundImages() + (image,)
network_editor.setBackgroundImages(background_images)
saveBackgroundImages(hou.node(parent_path), background_images)
def remap_material_override(material_type, material_override, mapping_file):
CleanDict = {}
if not isinstance(material_override, dict):
material_override = json.loads(material_override)
with open(mapping_file) as f:
data = json.load(f)
Lookup = data["materials"][material_type]
for x in data["supported"]:
if x in Lookup.keys():
enabled = Lookup[x][0]
if enabled == 1:
if Lookup[x][1] in material_override.keys():
CleanDict[x] = material_override[Lookup[x][1]]
return CleanDict
def extract_embedded_image(path, destination):
# Likely a COP
if path.startswith("op:"):
node = hou.node(path)
if node != None:
if node.type().category().name() == "Cop2":
node.saveImage(destination)
# Deal with HDA Stored "Textures"
elif path.startswith("opdef:"):
with open(destination, "w") as f:
f.write(hou.readFile(path))
def create_node_help(nodetypename, context, directory):
from labsopui import labsdocs
labsdocs.create_node_help(nodetypename, context, directory)
def create_node_help_auto(node):
from labsopui import labsdocs
labsdocs.create_node_help_auto(node)
def manage_ocio(destination="$HOUDINI_USER_PREF_DIR/packages/Labs_OpenColorIO.json", install=0):
destination = hou.text.expandString(destination)
if install == 1:
create_directory_if_not_exists(os.path.dirname(destination))
shutil.copyfile(hou.text.expandString("$SIDEFXLABS/optional/labs_ocio/Labs_OpenColorIO.json"), destination)
else:
if os.path.isfile(destination):
os.remove(destination)
def manage_viewport_alt_grey(destination="$HOUDINI_USER_PREF_DIR/config/3DSceneColors.bw", install=0):
destination = hou.text.expandString(destination)
if install == 1:
create_directory_if_not_exists(os.path.dirname(destination))
shutil.copyfile(hou.text.expandString("$SIDEFXLABS/optional/viewport/3DSceneColors.bw"), destination)
else:
if os.path.isfile(destination):
os.remove(destination)