forked from mattebb/3delightblender
/
filepath_utils.py
93 lines (80 loc) · 2.73 KB
/
filepath_utils.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
import bpy
import os
import platform
import sys
import webbrowser
import re
from ..rfb_logger import rfb_log
from .prefs_utils import get_pref
def view_file(file_path):
rman_editor = get_pref('rman_editor', '')
if rman_editor:
rman_editor = get_real_path(rman_editor)
command = rman_editor + " " + file_path
try:
os.system(command)
return
except Exception:
rfb_log().error("File or text editor not available. (Check and make sure text editor is in system path.)")
if sys.platform == ("win32"):
try:
os.startfile(file_path)
return
except:
pass
else:
if sys.platform == ("darwin"):
opener = 'open -t'
else:
opener = os.getenv('EDITOR', 'xdg-open')
opener = os.getenv('VIEW', opener)
try:
command = opener + " " + file_path
os.system(command)
return
except Exception as e:
rfb_log().error("Open file command failed: %s" % command)
pass
# last resort, try webbrowser
try:
webbrowser.open(file_path)
except Exception as e:
rfb_log().error("Open file with web browser failed: %s" % str(e))
def get_cycles_shader_path():
# figure out the path to Cycles' shader path
# hopefully, this won't change between versions
path = ''
version = '%d.%d' % (bpy.app.version[0], bpy.app.version[1])
binary_path = os.path.dirname(bpy.app.binary_path)
rel_config_path = os.path.join(version, 'scripts', 'addons', 'cycles', 'shader')
if sys.platform == ("win32"):
path = os.path.join(binary_path, rel_config_path)
elif sys.platform == ("darwin"):
path = os.path.join(binary_path, '..', 'Resources', rel_config_path )
else:
path = os.path.join(binary_path, rel_config_path)
return path
def get_token_blender_file_path(p):
# Same as filesystem_path below, but substitutes the relative Blender path
# with the <blend_dir> token
if not get_pref('rman_use_blend_dir_token', True):
return filesystem_path(p)
if p.startswith('//'):
pout = bpy.path.abspath(p)
if p != pout:
regex = r"^//"
pout = re.sub(regex, '<blend_dir>/', p, 0, re.MULTILINE)
else:
pout = p
return pout.replace('\\', '/')
def filesystem_path(p):
#Resolve a relative Blender path to a real filesystem path
if p.startswith('//'):
pout = bpy.path.abspath(p)
else:
pout = os.path.realpath(p)
return pout.replace('\\', '/')
def get_real_path(path):
if os.path.isabs(path):
return os.path.realpath(filesystem_path(path))
return path