forked from niacdoial/blemd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
175 lines (137 loc) · 5.41 KB
/
common.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
"""
A common function module for BleMD.
Mostly used by BModel, but can be expanded further
"""
import bpy
import os, sys, re, glob
from time import sleep
import subprocess
from contextlib import contextmanager
import logging
log = logging.getLogger('bpy.ops.import_mesh.bmd.maxH')
IDE = False # is changed by test launcher
if not (sys.platform[:3].lower()=='win' or sys.platform[:3].lower()=='lin'):
log.error('Your platform (%s) is not supported. images will not be imported', sys.platform[:3].lower())
@contextmanager
def stdout_redirected(to=os.devnull):
'''
# curtesy of https://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python#4675744
import os
with stdout_redirected(to=filename):
print("from Python")
os.system("echo non-Python applications are also supported")
'''
fd = sys.stdout.fileno()
##### assert that Python and C stdio write using the same file descriptor
####assert libc.fileno(ctypes.c_void_p.in_dll(libc, "stdout")) == fd == 1
def _redirect_stdout(to):
sys.stdout.close() # + implicit flush()
os.dup2(to.fileno(), fd) # fd writes to 'to' file
sys.stdout = os.fdopen(fd, 'w') # Python writes to fd
with os.fdopen(os.dup(fd), 'w') as old_stdout:
with open(to, 'w') as file:
_redirect_stdout(to=file)
try:
yield # allow code to be run with the redirected stdout
finally:
_redirect_stdout(to=old_stdout) # restore stdout.
# buffering and flags such as
# CLOEXEC may be different
@contextmanager
def active_object(obj):
act_bk = bpy.context.view_layer.objects.active
bpy.context.view_layer.objects.active = obj
try:
yield # run some code
finally:
bpy.context.view_layer.objects.active = act_bk
def MessageBox(string):
log.warning(string)
if IDE:
input('press any key to continue')
return
#drawer = (lambda obj, context: obj.layout.label(string))
#bpy.context.window_manager.popup_menu(drawer, 'message box', icon='ERROR')
#sleep(5)
def ReverseArray(inputArray):
i = 0
rev = []
i = len(inputArray)
while i > 0:
rev.append(inputArray[i-1])
i -= 1
return rev
def dict_get_set(dct, key, default):
if key not in dct.keys():
dct[key] = default
return dct[key]
def SubProcCall(exefile, args, startpath=os.getcwd()):
# this is the function to edit to adapt the program for non-windows platforms
# just add an 'elif' to the if/else block below, in which `exefile` is adapted
# (from 'path/to/bmdview' to 'path/to/bmdview.exe' in this example)
if sys.platform[:3].lower() == "win": # windows: use EXE
exefile += '.exe'
elif sys.platform == 'linux':
exefile += '.lin'
else:
raise RuntimeError('For now, image extraction does not support your platform')
if not os.path.isabs(exefile):
exefile = os.path.abspath(os.path.join(startpath, exefile))
# if ' ' in exefile: # whitespace: quotes needed
# exefile = '"' + exefile + '"'
# args = ['"' + com + '"' for com in args]
# do not change original data, and add quotes on args
temp = subprocess.run([exefile] + args, shell=False)
if temp.stdout:
log.info ("process output:\n %s", temp.stdout)
if temp.stderr:
log.error('process errors:\n %s', temp.stderr)
def newfile(name):
if not os.exists(name): # if it doesn't exist
open(name, 'ab').close() # create file
def getFilenameFile(path):
dir, file = os.path.split(path)
file = os.path.splitext(file)[0]
return file
# return os.path.join(dir, file)
def getFiles(*pathparts, basedir=""):
"""get the path of files matching a known globbing pattern, starting with a known base directory"""
basedir = glob.escape(basedir)
return glob.glob(os.path.join(basedir, *pathparts))
def dedup_lines(string):
lines = {} # dict: {line: count}
for line in string.split('\n'):
line = line + ' (x{:d})\n'
lines[line] = dict_get_set(lines, line, 0) + 1
dest=""
for line in lines.keys():
dest += line.format(lines[line])
return dest
class Prog_params:
def __init__(
self, filename, boneThickness, frc_cr_bn,
import_anims, import_anims_type,
tx_pck, ic_sc, imtype,
dvg=False, nat_bn=False,
use_nodes=False,
val_msh=False, paranoia=False,
no_rot_cv=False, anim_rot_smallest=False,
):
self.filename = filename
self.boneThickness = boneThickness
self.forceCreateBones = frc_cr_bn
self.loadAnimations = import_anims and not nat_bn
self.animationType = import_anims_type
self.enforce_smallest_movement = anim_rot_smallest
self.naturalBones = nat_bn
self.packTextures = tx_pck
self.includeScaling = ic_sc
self.imtype = imtype
self.DEBUGVG = dvg
self.PARANOID = paranoia
self.use_nodes = use_nodes
self.validate_mesh = val_msh
self.no_rot_conversion = no_rot_cv
# secondary parameters (computed later on)
self.createBones = True
GLOBALS = None # will hold a Prog_params instance