-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharpSuite.py
225 lines (191 loc) · 7.38 KB
/
SharpSuite.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
import ssbh_data_py
import os
import os.path
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from tkinter import ttk
from pathlib import Path
import sys
import webbrowser
import shutil
import subprocess
from multiprocessing import Process
import threading
import imghdr
from queue import Queue
#create ui for program
root = Tk()
root.title("Sharp Smash Suite")
root.withdraw()
root.destinationDir = ""
root.searchDir = ""
root.hasModel=False
root.hasMesh=False
import configparser
config = configparser.ConfigParser()
if (not os.path.isfile(os.getcwd() + r"/img2nutexbGUI/config.ini")):
messagebox.showerror(root.title(),"Please set img2nutexb program via img2nutexbGUI")
sys.exit("No img")
config.read(os.getcwd() +'/img2nutexbGUI/config.ini')
imgnutexbLocation = config["DEFAULT"]["img2nutexbLocation"]
if not "maxThreads" in config["DEFAULT"]:
config["DEFAULT"]["maxThreads"]="4"
with open(os.getcwd() +'/img2nutexbGUI/config.ini', 'w+') as configfile:
config.write(configfile)
maxThreads = int(config["DEFAULT"]["maxThreads"])
def truncate(string,direciton=W,limit=20,ellipsis=True):
if (len(string) < 3):
return string
text = ""
addEllipsis = "..." if (ellipsis) else ""
if direciton == W:
text = addEllipsis+string[len(string)-limit:len(string)]
else:
text = string[0:limit]+addEllipsis
return text
imgnutexbLocation = os.getcwd() + "/img2nutexbGUI/img2nutexb.exe"
def GetMaterialFile():
root.materialFile = root.destinationDir+"/materials.txt"
if (not os.path.isfile(root.materialFile)):
messagebox.showinfo(root.title(),"Please select your materials.txt exported via Blender")
ftypes = [
('materials list', ["*.txt"])
]
file = filedialog.askopenfile(title = "Select materials.txt",filetypes = ftypes)
root.materialFile = file.name if file else ""
if (root.materialFile == ""):
root.destroy()
sys.exit("No model")
#make sure that it is a validated destination folder, otherwise quit
def IsValidDestination():
root.hasModel = os.path.exists(root.destinationDir+"/model.numdlb")
root.hasMesh = os.path.exists(root.destinationDir+"/model.numshb")
root.hasMaterials = os.path.exists(root.destinationDir+"/materials.txt")
return (root.hasModel and root.hasMesh and root.hasMaterials)
#open folder dialogue
def SetDestinationDir():
messagebox.showinfo(root.title(),"Select your model's folder")
root.destinationDir = filedialog.askdirectory(title = "Select your model's folder")
if (root.destinationDir == ""):
root.destroy()
sys.exit("rejected folder")
if (IsValidDestination() == False):
if not (root.hasModel and root.hasMesh):
messagebox.showerror(root.title(),"That folder doesn't contain both a model (.numdlb) and mesh (.numshb) file!")
if not (root.hasMaterials):
messagebox.showerror(root.title(),"That folder doesn't contain a materials.txt file! You'll need to run 'Export Materials List' from Blender and set the destination here.")
root.destroy()
sys.exit("No model/mesh/materials")
#make sure that it is a validated destination folder, otherwise quit
def IsValidSearch():
validPath = os.path.isdir(root.searchDir)
if (validPath):
return True
return False
#open folder dialogue
def SetSearchDir():
messagebox.showinfo(root.title(),"Select folder with images")
root.searchDir = filedialog.askdirectory(title = "Select folder with images")
if (root.searchDir == ""):
root.destroy()
sys.exit("rejected folder")
if (IsValidSearch() == False):
messagebox.showerror(root.title(),"That folder doesn't contain a model,mesh or material folder")
root.destroy()
sys.exit("No model")
root.materials={}
def ReadMaterialFile():
labels=[]
textures=[]
useLabel=False
materialListFile = open(root.materialFile,'r')
lines = materialListFile.readlines()
lines = [line.rstrip() for line in lines]
materialListFile.close()
#Populate textfile without adding an extra new line at the end
for i in range(len(lines)):
if ("-Materials-" in lines[i]):
useLabel=True
elif ("-Textures-" in lines[i]):
useLabel=False
elif (not "---" in lines[i]):
if (useLabel):
labels.append(lines[i])
else:
textures.append(lines[i])
for l in range(len(labels)):
root.materials.update({labels[l]:textures[l]})
def Init():
messagebox.showinfo(root.title(),"Make sure you imported your model already via StudioSB, and saved the materials.txt file generated from Blender to that model's folder")
SetDestinationDir()
GetMaterialFile()
ReadMaterialFile()
SetSearchDir()
root.matl = None
from typing import NewType
def Numatb_CopyEntry(matEntry,cloneEntry):
for i in matEntry:
entrytype = type(i)
param_id = i.param_id
data = i.data
c = entrytype(param_id,data)
cloneEntry.append(c)
return cloneEntry
def Numatb_Copy(matEntryData):
clone = ssbh_data_py.matl_data.MatlEntryData(matEntryData.material_label,matEntryData.shader_label)
clone.blend_states = Numatb_CopyEntry(matEntryData.blend_states,clone.blend_states)
clone.floats = Numatb_CopyEntry(matEntryData.floats,clone.floats)
clone.booleans = Numatb_CopyEntry(matEntryData.booleans,clone.booleans)
clone.vectors = Numatb_CopyEntry(matEntryData.vectors,clone.vectors)
clone.rasterizer_states = Numatb_CopyEntry(matEntryData.rasterizer_states,clone.rasterizer_states)
clone.samplers = Numatb_CopyEntry(matEntryData.samplers,clone.samplers)
clone.textures = Numatb_CopyEntry(matEntryData.textures,clone.textures)
return clone
def Numatb_CreateMatl():
template = os.getcwd() + "/template.numatb"
if not os.path.exists(template):
messagebox.showerror(root.title(),"template.numatb is missing from this directory!")
print("Numatb failed")
return
root.matl = ssbh_data_py.matl_data.read_matl(template)
i=0
currentEntry = root.matl.entries[0]
for mat in root.materials:
texture = root.materials[mat]
if ("." in texture):
texture = texture[0:texture.index(".")]
newEntry = Numatb_Copy(currentEntry)
newEntry.material_label = mat
newEntry.textures[0].data = texture
root.matl.entries.insert(i+1,newEntry)
root.matl.entries.pop(0)
root.matl.entries.sort(key=Magic_SortByLabel)
root.matl.save(root.destinationDir+"/model.numatb")
print("Numatb created")
def Magic_SortByLabel(e):
return e.material_label
def MagicModel():
from MagicModel import MagicModel
sys.path.insert(0, '/img2nutexbGUI/')
MagicModel.Init(root.destinationDir)
def BatchImg():
textures=[]
for i in root.materials:
textures.append(root.materials[i])
from img2nutexbGUI import img2nutexbGUI
sys.path.insert(0, '/img2nutexbGUI/')
img2nutexbGUI.init(root.searchDir,root.destinationDir,os.getcwd() + r"/img2nutexbGUI/")
img2nutexbGUI.ValidatePorgram()
img2nutexbGUI.BatchImg(textures)
def Main():
Init()
BatchImg()
Numatb_CreateMatl()
MagicModel()
#messagebox.showinfo(root.title(),"Finished!")
#webbrowser.open(root.destinationDir)
sys.exit("Finished")
if __name__ == '__main__':
Main()
root.mainloop()