-
Notifications
You must be signed in to change notification settings - Fork 13
/
helpers.py
118 lines (89 loc) · 2.68 KB
/
helpers.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
import csv
import io
import re
import codecs
import json
from pathlib import Path
import os
try:
from .definitions import APP_ROOT
except ImportError as e:
print(e)
import traceback
print(traceback.format_exc())
from definitions import APP_ROOT
# remove multiple spaces
def clean_line(line):
return " ".join(line.split())
# assumes cleaned line being passed
def get_params(clean_line, lowercase=False):
parts = clean_line.split()
if lowercase:
return [x.lower() for x in parts]
return parts
def parse_csv_line(line, min_params=0):
try:
parts = list(csv.reader(io.StringIO(line), delimiter=' ', quotechar='"', skipinitialspace=True))
except csv.Error as e:
print(e)
import traceback
print(traceback.format_exc())
parts = [re.split(r"\s+", line)]
if len(parts) == 0:
return None
_params = parts[0]
if len(_params) == 0:
return None
while len(_params) < min_params:
_params.append("")
return _params
def fix_string_encoding(string):
new_string = string
if type(string) is str:
new_string = bytes(string.encode())
for codec in [codecs.BOM_UTF8, codecs.BOM_UTF16, codecs.BOM_UTF32]:
new_string = new_string.replace(codec, b'')
new_string = new_string.decode()
return new_string
def write_json(filepath, obj, indent=None, do_print=False):
try:
full_path = os.path.join(APP_ROOT, filepath)
Path(os.path.dirname(full_path)).mkdir(parents=True, exist_ok=True)
with open(full_path, 'w', encoding='utf-8', newline="\n") as file:
j = json.dumps(obj, indent=indent, ensure_ascii=False)
if do_print:
print(j)
file.write(j)
except Exception as e:
print(e)
import traceback
print(traceback.format_exc())
def read_json(filepath, default=None):
try:
full_path = os.path.join(APP_ROOT, filepath)
with open(full_path, 'r', encoding='utf-8') as file:
return json.load(file)
except Exception as e:
print(e)
import traceback
print(traceback.format_exc())
return default
def clamp(num, min_value, max_value):
return max(min(num, max_value), min_value)
def ensure_bmesh(bm):
bm.faces.ensure_lookup_table()
bm.verts.ensure_lookup_table()
bm.edges.ensure_lookup_table()
def finish_bmesh(bm, mesh):
bm.to_mesh(mesh)
bm.clear()
bm.free()
def finish_mesh(mesh):
mesh.validate()
mesh.update(calc_edges=True)
def hide_obj(obj):
obj.hide_viewport = True
obj.hide_render = True
def show_obj(obj):
obj.hide_viewport = False
obj.hide_render = False