-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSMDMat.py
178 lines (170 loc) · 4.16 KB
/
SMDMat.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
import json
import shutil
class PointerFix:
def __init__(self):
pass
# Copy-pasted functions from SMDBone.py
def options(self):
jsf = open('save/options.json', 'r')
js = jsf.read()
options = json.loads(js)
return options
# nl_clean removes the \n (new line) character from every object in an array
def nl_clean(self, smd):
count = -1
for l in smd:
count += 1
smd[count] = l[:-l.count("\n")]
return smd
def nl_insert(self, smd):
count = -1
for l in smd:
count += 1
nl = l
nl += '\n'
smd[count] = nl
return smd
def list_to_string(self, listt):
stringy = ''
for l in listt:
stringy += l
return stringy
# Pointer-fixing function
def rename_part(self, file, ref, torename, replace):
opt = self.options()
if opt["backup_smd"]:
directory = file.replace('.smd', 'B.smd')
shutil.copy(file, directory)
# Reading Model SMD
mdl = open(file, 'r')
mdl_smd = self.nl_clean(mdl.readlines())
check_end = False
find_spec = False
# Checking reference value
if ref.startswith('*'):
# If * is at the start of the line, check each pointer that ends with the specified value
check_end = True
find_spec = False
ref = ref[-1:]
elif ref.endswith('*'):
# Ditto with the start of each pointer
check_end = False
find_spec = False
else:
# Otherwise the program will try to find an exact name
find_spec = True
# print("Format Error: '*' is not at the start nor end of reference variable")
count = -1
chMade = False
if check_end:
for l in mdl_smd:
count += 1
if l.endswith(ref):
mdl_smd[count] = l.replace(torename, replace)
chMade = True
if chMade:
mdl = open(file, 'w')
mdl_smd = self.nl_insert(mdl_smd)
mdl.write(self.list_to_string(mdl_smd))
mdl.close()
print("SMD Written")
else:
print("No changes were made, did you specify an invalid finder value?")
elif find_spec:
for l in mdl_smd:
count += 1
if l == ref:
mdl_smd[count] = l
chMade = True
if chMade:
mdl = open(file, 'w')
mdl_smd = self.nl_insert(mdl_smd)
mdl.write(self.list_to_string(mdl_smd))
mdl.close()
print("SMD Written")
else:
print("No changes were made, did you specify an invalid finder value?")
elif not check_end:
for l in mdl_smd:
count += 1
if l.startswith(ref):
mdl_smd[count] = l.replace(torename, replace)
chMade = True
if chMade:
mdl = open(file, 'w')
mdl_smd = self.nl_insert(mdl_smd)
mdl.write(self.list_to_string(mdl_smd))
mdl.close()
print("SMD Written")
else:
print("No changes were made, did you specify an invalid finder value?")
def add_bmp(self, file, ref):
opt = self.options()
if opt["backup_smd"]:
directory = file[:-4] + 'B.smd'
shutil.copy(file, directory)
mdl = open(file, 'r')
mdl_smd = self.nl_clean(mdl.readlines())
skip_line = True
triangles = False
chMade = False
if ref == '*':
count = -1
mini_c = 0
for l in mdl_smd:
count += 1
if skip_line:
if l == 'triangles':
skip_line = False
triangles = True
continue
elif l == 'version 1':
skip_line = True
continue
elif count >= len(mdl_smd) - 2:
continue
elif skip_line and triangles:
mini_c += 1
if mini_c == 3:
mini_c = 0
skip_line = False
continue
elif skip_line:
continue
mdl_smd[count] = l + '.bmp'
skip_line = True
chMade = True
else:
count = -1
mini_c = 0
for l in mdl_smd:
count += 1
if skip_line:
if l == 'triangles':
skip_line = False
triangles = True
continue
elif l == 'version 1':
skip_line = True
continue
elif count >= len(mdl_smd) - 2:
continue
elif skip_line and triangles:
mini_c += 1
if mini_c == 3:
mini_c = 0
skip_line = False
continue
elif skip_line:
continue
if l == ref:
mdl_smd[count] = l + '.bmp'
skip_line = True
chMade = True
if chMade:
mdl = open(file, 'w')
mdl.write(self.list_to_string(self.nl_insert(mdl_smd)))
mdl.close()
print('SMD Written')
else:
print("No changes were made, did you specify an invalid finder value?")