forked from prman-pixar/RenderManForBlender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
699 lines (557 loc) · 21.1 KB
/
util.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# ##### BEGIN MIT LICENSE BLOCK #####
#
# Copyright (c) 2015 - 2017 Pixar
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
# ##### END MIT LICENSE BLOCK #####
import bpy
import mathutils
import re
import os
import platform
import sys
import fnmatch
import subprocess
from subprocess import Popen, PIPE
from extensions_framework import util as efutil
from mathutils import Matrix, Vector
EnableDebugging = False
class BlenderVersionError(Exception):
pass
def bpy_newer_257():
if (bpy.app.version[1] < 57 or (bpy.app.version[1] == 57 and
bpy.app.version[2] == 0)):
raise BlenderVersionError
def clamp(i, low, high):
if i < low:
i = low
if i > high:
i = high
return i
def throw_error(msg):
raise ImportError(msg)
# print(msg)
def getattr_recursive(ptr, attrstring):
for attr in attrstring.split("."):
ptr = getattr(ptr, attr)
return ptr
# return a list of meta tuples
def get_osl_line_meta(line):
if "%%meta" not in line:
return {}
meta = {}
for m in re.finditer('meta{', line):
sub_str = line[m.start(), line.find('}', beg=m.start())]
item_type, item_name, item_value = sub_str.split(',', 2)
val = item_value
if item_type == 'string':
val = val[1:-1]
elif item_type == 'int':
val = int(val)
elif item_type == 'float':
val = float(val)
meta[item_name] = val
return meta
def locate_openVDB_cache(frameNum):
if not bpy.data.is_saved:
return None
filename = os.path.splitext(os.path.split(bpy.data.filepath)[1])[0]
cacheDir = os.path.join(bpy.path.abspath("//"), 'blendcache_%s' % filename)
if not os.path.exists(cacheDir):
return None
for f in os.listdir(os.path.join(bpy.path.abspath("//"), cacheDir)):
if '.vdb' in f and "%06d" % frameNum in f:
return os.path.join(bpy.path.abspath("//"), cacheDir, f)
return None
def readOSO(filePath):
line_number = 0
shader_meta = {}
prop_names = []
shader_meta["shader"] = os.path.splitext(os.path.basename(filePath))[0]
with open(filePath, encoding='utf-8') as osofile:
for line in osofile:
# if line.startswith("surface") or line.startswith("shader"):
# line_number += 1
# listLine = line.split()
# shader_meta["shader"] = listLine[1]
if line.startswith("param"):
line_number += 1
listLine = line.split()
name = listLine[2]
type = listLine[1]
if type == "point" or type == "vector" or type == "normal" or \
type == "color":
defaultString = []
defaultString.append(listLine[3])
defaultString.append(listLine[4])
defaultString.append(listLine[5])
default = []
for element in defaultString:
default.append(float(element))
elif type == "matrix":
default = []
x = 3
while x <= 18:
default.append(float(listLine[x]))
x += 1
elif type == "closure":
debug('error', "Closure types are not supported")
#type = "void"
#name = listLine[3]
else:
default = listLine[3]
prop_names.append(name)
prop_meta = {"type": type, "default": default, "IO": "in"}
for tup in listLine:
if tup == '%meta{int,lockgeom,0}':
prop_meta['lockgeom'] = 0
break
prop_meta.update(get_osl_line_meta(line))
shader_meta[name] = prop_meta
elif line.startswith("oparam"):
line_number += 1
listLine = line.split()
name = listLine[2]
type = listLine[1]
if type == "point" or type == "vector" or type == "normal" or \
type == "color":
default = []
default.append(listLine[3])
default.append(listLine[4])
default.append(listLine[5])
elif type == "matrix":
default = []
x = 3
while x <= 18:
default.append(listLine[x])
x += 1
elif type == "closure":
debug('error', "Closure types are not supported")
type = "void"
name = listLine[3]
else:
default = listLine[3]
prop_names.append(name)
prop_meta = {"type": type, "default": default, "IO": "out"}
prop_meta.update(get_osl_line_meta(line))
shader_meta[name] = prop_meta
else:
line_number += 1
return prop_names, shader_meta
def debug(warningLevel, *output):
if warningLevel == 'warning' or warningLevel == 'error' or \
warningLevel == 'osl':
if(warningLevel == "warning"):
print("WARNING: ", output)
elif(warningLevel == "error"):
print("ERROR: ", output)
elif(warningLevel == "osl"):
for item in output:
print("OSL INFO: ", item)
else:
if EnableDebugging:
if(warningLevel == "info"):
print("INFO: ", output)
else:
print("DEBUG: ", output)
else:
pass
def get_Selected_Objects(scene):
objectNames = []
for obj in scene.objects:
if(obj.select == True):
objectNames.append(obj.name)
return objectNames
def get_Files_in_Directory(path):
files = []
#files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
files = [f for f in os.listdir(path)]
return files
# -------------------- Path Handling -----------------------------
# convert multiple path delimiters from : to ;
# converts both windows style paths (x:C:\blah -> x;C:\blah)
# and unix style (x:/home/blah -> x;/home/blah)
def path_delimit_to_semicolons(winpath):
return re.sub(r'(:)(?=[A-Za-z]|\/)', r';', winpath)
def args_files_in_path(prefs, idblock, shader_type='', threaded=True):
init_env(prefs)
args = {}
path_list = get_path_list_converted(prefs, 'args')
for path in path_list:
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, '*.args'):
args[filename.split('.')[0]] = os.path.join(root, filename)
return args
def get_path_list(rm, type):
paths = []
if rm.use_default_paths:
# here for getting args
if type == 'args':
rmantree = guess_rmantree()
paths.append(os.path.join(rmantree, 'lib', 'plugins'))
paths.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),
'Args'))
if type == 'shader':
paths.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),
'shaders'))
paths.append(os.path.join(bpy.utils.resource_path('LOCAL'), 'scripts',
'addons', 'cycles', 'shader'))
paths.append(os.path.join('${RMANTREE}', 'lib', 'shaders'))
paths.append('@')
if rm.use_builtin_paths:
paths.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),
"%ss" % type))
if hasattr(rm, "%s_paths" % type):
for p in getattr(rm, "%s_paths" % type):
paths.append(bpy.path.abspath(p.name))
return paths
def get_real_path(path):
return os.path.realpath(efutil.filesystem_path(path))
# Convert env variables to full paths.
def path_list_convert(path_list, to_unix=False):
paths = []
for p in path_list:
p = os.path.expanduser(p)
if p.find('$') != -1:
# path contains environment variables
# p = p.replace('@', os.path.expandvars('$DL_SHADERS_PATH'))
# convert path separators from : to ;
p = path_delimit_to_semicolons(p)
if to_unix:
p = path_win_to_unixy(p)
envpath = ''.join(p).split(';')
paths.extend(envpath)
else:
if to_unix:
p = path_win_to_unixy(p)
paths.append(p)
return paths
def get_path_list_converted(rm, type, to_unix=False):
return path_list_convert(get_path_list(rm, type), to_unix)
def path_win_to_unixy(winpath, escape_slashes=False):
# if escape_slashes:
# p = winpath.replace('\\', '\\\\')
# else:
# # convert pattern C:\\blah to //C/blah so 3delight can understand
# p = re.sub(r'([A-Za-z]):\\', r'//\1/', winpath)
# p = p.replace('\\', '/')
return winpath
# convert ### to frame number
def make_frame_path(path, frame):
def repl(matchobj):
hashes = len(matchobj.group(1))
return str(frame).zfill(hashes)
path = re.sub('(#+)', repl, path)
return path
def get_sequence_path(path, blender_frame, anim):
if not anim.animated_sequence:
return path
frame = blender_frame - anim.blender_start + anim.sequence_in
# hold
frame = clamp(frame, anim.sequence_in, anim.sequence_out)
return make_frame_path(path, frame)
def user_path(path, scene=None, ob=None, display_driver=None, layer_name=None, pass_name=None):
'''
# bit more complicated system to allow accessing scene or object attributes.
# let's stay simple for now...
def repl(matchobj):
data, attr = matchobj.group(1).split('.')
if data == 'scene' and scene != None:
if hasattr(scene, attr):
return str(getattr(scene, attr))
elif data == 'ob' and ob != None:
if hasattr(ob, attr):
return str(getattr(ob, attr))
else:
return matchobj.group(0)
path = re.sub(r'\{([0-9a-zA-Z_]+\.[0-9a-zA-Z_]+)\}', repl, path)
'''
# first env vars, in case they contain special blender variables
# recursively expand these (max 10), in case there are vars in vars
for i in range(10):
path = os.path.expandvars(path)
if '$' not in path:
break
unsaved = True if not bpy.data.filepath else False
# first builtin special blender variables
if unsaved:
path = path.replace('{blend}', 'untitled')
else:
blendpath = os.path.splitext(os.path.split(bpy.data.filepath)[1])[0]
path = path.replace('{blend}', blendpath)
if scene is not None:
path = path.replace('{scene}', scene.name)
if display_driver is not None:
if display_driver == "tiff":
path = path.replace('{file_type}', display_driver[-4:])
else:
path = path.replace('{file_type}', display_driver[-3:])
if ob is not None:
path = path.replace('{object}', ob.name)
if layer_name is not None:
path = path.replace('{layer}', layer_name)
if pass_name is not None:
path = path.replace('{pass}', pass_name)
# convert ### to frame number
if scene is not None:
path = make_frame_path(path, scene.frame_current)
# convert blender style // to absolute path
if unsaved:
path = bpy.path.abspath(path, start=bpy.app.tempdir)
else:
path = bpy.path.abspath(path)
return path
# ------------- RIB formatting Helpers -------------
def rib(v, type_hint=None):
# float, int
if type_hint == 'color':
return list(v)[:3]
if type(v) in (mathutils.Vector, mathutils.Color) or\
v.__class__.__name__ == 'bpy_prop_array'\
or v.__class__.__name__ == 'Euler':
# BBM modified from if to elif
return list(v)
# matrix
elif type(v) == mathutils.Matrix:
return [v[0][0], v[1][0], v[2][0], v[3][0],
v[0][1], v[1][1], v[2][1], v[3][1],
v[0][2], v[1][2], v[2][2], v[3][2],
v[0][3], v[1][3], v[2][3], v[3][3]]
elif type_hint == 'int':
return int(v)
elif type_hint == 'float':
return float(v)
else:
return v
def rib_ob_bounds(ob_bb):
return (ob_bb[0][0], ob_bb[7][0], ob_bb[0][1],
ob_bb[7][1], ob_bb[0][2], ob_bb[1][2])
def rib_path(path, escape_slashes=False):
return path_win_to_unixy(bpy.path.abspath(path),
escape_slashes=escape_slashes)
# return a list of properties set on this group
def get_properties(prop_group):
props = []
for (key, prop) in prop_group.bl_rna.properties.items():
# This is somewhat ugly, but works best!!
if key not in ['rna_type', 'name']:
props.append(prop)
return props
def get_global_worldspace(vec, ob):
wmatx = ob.matrix_world.to_4x4().inverted()
vec = vec * wmatx
return vec
def get_local_worldspace(vec, ob):
lmatx = ob.matrix_local.to_4x4().inverted()
vec = vec * lmatx
return vec
# ------------- Environment Variables -------------
def rmantree_from_env():
RMANTREE = ''
if 'RMANTREE' in os.environ.keys():
RMANTREE = os.environ['RMANTREE']
return RMANTREE
def set_pythonpath(path):
sys.path.append(path)
def set_rmantree(rmantree):
os.environ['RMANTREE'] = rmantree
def set_path(paths):
for path in paths:
if path is not None:
os.environ['PATH'] = path + os.pathsep + os.environ['PATH']
def check_valid_rmantree(rmantree):
prman = 'prman.exe' if platform.system() == 'Windows' else 'prman'
if os.path.exists(rmantree) and \
os.path.exists(os.path.join(rmantree, 'bin')) and \
os.path.exists(os.path.join(rmantree, 'bin', prman)):
return True
return False
# return the major, minor rman version
def get_rman_version(rmantree):
try:
prman = 'prman.exe' if platform.system() == 'Windows' else 'prman'
exe = os.path.join(rmantree, 'bin', prman)
desc = subprocess.check_output(
[exe, "-version"], stderr=subprocess.STDOUT)
vstr = str(desc, 'ascii').split('\n')[0].split()[-1]
major_vers, minor_vers = vstr.split('.')
vers_modifier = ''
for v in ['b', 'rc']:
if v in minor_vers:
i = minor_vers.find(v)
vers_modifier = minor_vers[i:]
minor_vers = minor_vers[:i]
break
return int(major_vers), int(minor_vers), vers_modifier
except:
return 0, 0, ''
def get_addon_prefs():
addon = bpy.context.user_preferences.addons[__name__.split('.')[0]]
return addon.preferences
def guess_rmantree():
prefs = get_addon_prefs()
rmantree_method = prefs.rmantree_method
choice = prefs.rmantree_choice
if rmantree_method == 'MANUAL':
rmantree = prefs.path_rmantree
elif rmantree_method == 'ENV' or choice == 'NEWEST':
rmantree = rmantree_from_env()
else:
rmantree = choice
version = get_rman_version(rmantree) # major, minor, mod
if choice == 'NEWEST':
# get from detected installs (at default installation path)
try:
base = {'Windows': r'C:\Program Files\Pixar',
'Darwin': '/Applications/Pixar',
'Linux': '/opt/pixar'}[platform.system()]
for d in os.listdir(base):
if "RenderManProServer" in d:
d_rmantree = os.path.join(base, d)
d_version = get_rman_version(d_rmantree)
if d_version > version:
rmantree = d_rmantree
version = d_version
except:
pass
# check rmantree valid
if version[0] == 0:
throw_error(
"Error loading addon. RMANTREE %s is not valid. Correct RMANTREE setting in addon preferences." % rmantree)
return None
# check that it's >= 21
if version[0] < 21:
throw_error("Error loading addon using RMANTREE=%s. RMANTREE must be version 21.0 or greater. Correct RMANTREE setting in addon preferences." % rmantree)
return None
return rmantree
def get_installed_rendermans():
base = ""
if platform.system() == 'Windows':
# default installation path
# or base = 'C:/Program Files/Pixar'
base = r'C:\Program Files\Pixar'
elif platform.system() == 'Darwin':
base = '/Applications/Pixar'
elif platform.system() == 'Linux':
base = '/opt/pixar'
rendermans = []
try:
for d in os.listdir(base):
if "RenderManProServer" in d:
try:
vstr = d.split('-')[1]
rendermans.append((vstr, os.path.join(base, d)))
except:
pass
except:
pass
return rendermans
# return true if an archive is older than the timestamp
def check_if_archive_dirty(update_time, archive_filename):
if update_time > 0 and os.path.exists(archive_filename) \
and os.path.getmtime(archive_filename) >= update_time:
return False
else:
return True
def find_it_path():
rmantree = guess_rmantree()
if not rmantree:
return None
else:
rmantree = os.path.join(rmantree, 'bin')
if platform.system() == 'Windows':
it_path = os.path.join(rmantree, 'it.exe')
elif platform.system() == 'Darwin':
it_path = os.path.join(
rmantree, 'it.app', 'Contents', 'MacOS', 'it')
elif platform.system() == 'Linux':
it_path = os.path.join(rmantree, 'it')
if os.path.exists(it_path):
return it_path
else:
return None
def find_local_queue():
rmantree = guess_rmantree()
if not rmantree:
return None
else:
rmantree = os.path.join(rmantree, 'bin')
if platform.system() == 'Windows':
lq = os.path.join(rmantree, 'LocalQueue.exe')
elif platform.system() == 'Darwin':
lq = os.path.join(
rmantree, 'LocalQueue.app', 'Contents', 'MacOS', 'LocalQueue')
elif platform.system() == 'Linux':
lq = os.path.join(rmantree, 'LocalQueue')
if os.path.exists(lq):
return lq
else:
return None
def find_tractor_spool():
base = ""
if platform.system() == 'Windows':
# default installation path
base = r'C:\Program Files\Pixar'
elif platform.system() == 'Darwin':
base = '/Applications/Pixar'
elif platform.system() == 'Linux':
base = '/opt/pixar'
latestver = 0.0
guess = ''
for d in os.listdir(base):
if "Tractor" in d:
vstr = d.split('-')[1]
vf = float(vstr)
if vf >= latestver:
latestver = vf
guess = os.path.join(base, d)
tractor_dir = guess
if not tractor_dir:
return None
else:
spool_name = os.path.join(tractor_dir, 'bin', 'tractor-spool')
if os.path.exists(spool_name):
return spool_name
else:
return None
# Default exporter specific env vars
def init_exporter_env(prefs):
if 'OUT' not in os.environ.keys():
os.environ['OUT'] = prefs.env_vars.out
# if 'SHD' not in os.environ.keys():
# os.environ['SHD'] = rm.env_vars.shd
# if 'PTC' not in os.environ.keys():
# os.environ['PTC'] = rm.env_vars.ptc
if 'ARC' not in os.environ.keys():
os.environ['ARC'] = prefs.env_vars.arc
def init_env(rm):
# init_exporter_env(scene.renderman)
# try user set (or guessed) path
RMANTREE = guess_rmantree()
os.environ['RMANTREE'] = RMANTREE
RMANTREE_BIN = os.path.join(RMANTREE, 'bin')
if RMANTREE_BIN not in sys.path:
sys.path.append(RMANTREE_BIN)
pathsep = os.pathsep
if 'PATH' in os.environ.keys():
os.environ['PATH'] += pathsep + os.path.join(RMANTREE, "bin")
else:
os.environ['PATH'] = os.path.join(RMANTREE, "bin")