-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
319 lines (278 loc) · 12.7 KB
/
Copy pathgui.py
File metadata and controls
319 lines (278 loc) · 12.7 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import json
import os
from tran import model_to_minecraft
from threading import Thread
from io import StringIO
import sys
# Language settings
LANGUAGES = {
'en': {
'title': '3DModel to Minecraft Converter',
'obj_file': 'Model File',
'game_path': 'Minecraft Game Path',
'browse': 'Browse',
'materials': 'Materials',
'wool': 'Wool',
'concrete': 'Concrete',
'terracotta': 'Terracotta',
'glass': 'Glass',
'advanced': 'Advanced Options',
'rotate_angle': 'Rotate Angle (rx, ry, rz)',
'pitch': 'Pitch',
'convert': 'Convert',
'language': 'Language',
'save': 'Save Settings',
'fold': 'Unfold/Fold',
'error_no_file': 'Please select model file and game path',
'convert_success': 'The conversion was successful!\n File saved at:',
'convert_failed': 'Conversion failed',
'output_msg': 'Output Message'
},
'zh': {
'title': '三维模型转Minecraft工具',
'obj_file': '模型文件',
'game_path': 'Minecraft游戏主目录',
'browse': '浏览',
'materials': '材料',
'wool': '羊毛',
'concrete': '混凝土',
'terracotta': '陶瓦',
'glass': '玻璃',
'advanced': '高级选项',
'rotate_angle': '旋转角度 (rx, ry, rz)',
'pitch': '体素大小',
'convert': '转换',
'language': '语言',
'save': '保存设置',
'fold': '展开/收起',
'error_no_file': '未选择模型文件或游戏主目录',
'convert_success': '转换成功!\n 文件保存在:',
'convert_failed': '转换失败',
'output_msg': '输出信息'
}
}
class StdoutRedirector(StringIO):
def __init__(self, text_widget):
super().__init__()
self.text_widget = text_widget
def write(self, string):
self.text_widget.insert(tk.END, string)
self.text_widget.see(tk.END)
self.text_widget.update_idletasks()
class App(tk.Tk):
def __init__(self):
super().__init__()
self.language = self.load_language()
self.model_dir = self.load_model_dir()
self.game_dir = self.load_game_dir()
self.title(LANGUAGES[self.language]['title'])
self.geometry('800x700')
self.minsize(800, 700)
self.create_widgets()
def load_language(self):
if os.path.exists('settings.json'):
with open('settings.json', 'r') as f:
try:
return json.load(f).get('language', 'zh')
except json.JSONDecodeError:
return 'zh'
else:
return 'zh'
def load_model_dir(self):
if os.path.exists('settings.json'):
with open('settings.json', 'r') as f:
try:
return json.load(f).get('model_dir', '')
except json.JSONDecodeError:
return ''
else:
return ''
def load_game_dir(self):
if os.path.exists('settings.json'):
with open('settings.json', 'r') as f:
try:
return json.load(f).get('game_dir', '')
except json.JSONDecodeError:
return ''
else:
return ''
def save_language(self):
if not os.path.exists('settings.json'):
with open('settings.json', 'w') as f:
json.dump({'language': self.language}, f)
else:
with open('settings.json', 'r') as f:
try:
settings = json.load(f)
except json.JSONDecodeError:
settings = {}
settings['language'] = self.language
with open('settings.json', 'w') as f:
json.dump(settings, f)
def save_model_dir(self):
if not os.path.exists('settings.json'):
with open('settings.json', 'w') as f:
json.dump({'model_dir': self.model_dir}, f)
else:
with open('settings.json', 'r') as f:
try:
settings = json.load(f)
except json.JSONDecodeError:
settings = {}
settings['model_dir'] = self.model_dir
with open('settings.json', 'w') as f:
json.dump(settings, f)
def save_game_dir(self):
if not os.path.exists('settings.json'):
with open('settings.json', 'w') as f:
json.dump({'game_dir': self.game_dir}, f)
else:
with open('settings.json', 'r') as f:
try:
settings = json.load(f)
except json.JSONDecodeError:
settings = {}
settings['game_dir'] = self.game_dir
with open('settings.json', 'w') as f:
json.dump(settings, f)
def create_widgets(self):
# Main frame
main_frame = ttk.Frame(self, padding="20")
main_frame.pack(fill=tk.BOTH, expand=True)
# Title
title_label = ttk.Label(main_frame, text="Minecraftify 2.0", font=('Microsoft YaHei UI', 24, 'bold'))
title_label.pack(pady=(0, 20))
# File path
file_frame = ttk.LabelFrame(main_frame, text=LANGUAGES[self.language]['obj_file'], padding="10")
file_frame.pack(fill=tk.X, pady=5)
self.obj_file_entry = ttk.Entry(file_frame, width=50)
self.obj_file_entry.pack(side=tk.LEFT, padx=5, fill=tk.X, expand=True)
ttk.Button(file_frame, text=LANGUAGES[self.language]['browse'], command=self.browse_obj_file).pack(side=tk.RIGHT, padx=5)
# World path
world_frame = ttk.LabelFrame(main_frame, text=LANGUAGES[self.language]['game_path'], padding="10")
world_frame.pack(fill=tk.X, pady=5)
self.game_path_entry = ttk.Entry(world_frame, width=50)
self.game_path_entry.pack(side=tk.LEFT, padx=5, fill=tk.X, expand=True)
ttk.Button(world_frame, text=LANGUAGES[self.language]['browse'], command=self.browse_game_path).pack(side=tk.RIGHT, padx=5)
# Materials
materials_frame = ttk.LabelFrame(main_frame, text=LANGUAGES[self.language]['materials'], padding="10")
materials_frame.pack(fill=tk.X, pady=5)
self.wool_var = tk.IntVar(value=1)
self.concrete_var = tk.IntVar(value=1)
self.terracotta_var = tk.IntVar(value=1)
self.glass_var = tk.IntVar(value=1)
ttk.Checkbutton(materials_frame, text=LANGUAGES[self.language]['wool'], variable=self.wool_var).pack(side=tk.LEFT, padx=10)
ttk.Checkbutton(materials_frame, text=LANGUAGES[self.language]['concrete'], variable=self.concrete_var).pack(side=tk.LEFT, padx=10)
ttk.Checkbutton(materials_frame, text=LANGUAGES[self.language]['terracotta'], variable=self.terracotta_var).pack(side=tk.LEFT, padx=10)
ttk.Checkbutton(materials_frame, text=LANGUAGES[self.language]['glass'], variable=self.glass_var).pack(side=tk.LEFT, padx=10)
# Advanced options
self.advanced_expanded = tk.BooleanVar(value=False)
advanced_frame = ttk.LabelFrame(main_frame, text=LANGUAGES[self.language]['advanced'], padding="10")
advanced_frame.pack(fill=tk.X, pady=5)
advanced_content = ttk.Frame(advanced_frame)
options = [
(LANGUAGES[self.language]['rotate_angle'], '(0, 0, 0)'),
(LANGUAGES[self.language]['pitch'], '1.0'),
]
for i, (label_text, default_value) in enumerate(options):
frame = ttk.Frame(advanced_content)
frame.pack(fill=tk.X, pady=2)
ttk.Label(frame, text=label_text).pack(side=tk.LEFT, padx=5)
entry = ttk.Entry(frame, width=20)
entry.insert(0, default_value)
entry.pack(side=tk.RIGHT, padx=5)
setattr(self, f'option_entry_{i}', entry)
ttk.Button(advanced_frame, text=LANGUAGES[self.language]['fold'], command=lambda: self.toggle_advanced(advanced_content)).pack(anchor='w', pady=5)
if not self.advanced_expanded.get():
advanced_content.pack_forget()
# Language selection
language_frame = ttk.Frame(main_frame)
language_frame.pack(fill=tk.X, pady=10)
ttk.Label(language_frame, text=LANGUAGES[self.language]['language']).pack(side=tk.LEFT, padx=5)
self.language_combo = ttk.Combobox(language_frame, values=['zh', 'en'], state='readonly', width=5)
self.language_combo.set(self.language)
self.language_combo.bind('<<ComboboxSelected>>', self.change_language)
self.language_combo.pack(side=tk.LEFT, padx=5)
# Convert button
convert_button = ttk.Button(main_frame, text=LANGUAGES[self.language]['convert'], command=self.convert)
convert_button.pack(pady=20)
# Progress bar
self.progress = ttk.Progressbar(main_frame, orient='horizontal', length=400, mode='determinate')
self.progress.pack(fill=tk.X, pady=5)
# Output message
output_frame = ttk.LabelFrame(main_frame, text=LANGUAGES[self.language]['output_msg'], padding="10")
output_frame.pack(fill=tk.BOTH, expand=True, pady=5)
self.output_text = tk.Text(output_frame, height=4, width=60, wrap=tk.WORD)
self.output_text.pack(fill=tk.BOTH, expand=True)
self.output_text.bind("<Key>", lambda e: "break")
def browse_obj_file(self):
file_path = filedialog.askopenfilename(filetypes=[('Supported Files', '*.obj;*.stl;*.ply;*.off;*.glb;*.gltf'), ('OBJ Files', '*.obj'), ('STL Files', '*.stl'), ('PLY Files', '*.ply'), ('OFF Files', '*.off'), ('GLB Files', '*.glb;*.gltf')], initialdir=self.model_dir)
if file_path:
self.obj_file_entry.delete(0, tk.END)
self.obj_file_entry.insert(0, file_path)
self.model_dir = os.path.dirname(file_path)
self.save_model_dir()
def browse_game_path(self):
folder_path = filedialog.askdirectory(initialdir=self.game_dir)
if folder_path:
self.game_path_entry.delete(0, tk.END)
self.game_path_entry.insert(0, folder_path)
self.game_dir = folder_path
self.save_game_dir()
def change_language(self, event):
self.language = self.language_combo.get()
self.save_language()
self.destroy()
App().mainloop()
def convert(self):
obj_file = self.obj_file_entry.get()
game_path = self.game_path_entry.get()
if not obj_file or not game_path:
messagebox.showerror(LANGUAGES[self.language]['title'], LANGUAGES[self.language]['error_no_file'])
return
def convert_thread():
old_stdout = sys.stdout
sys.stdout = StdoutRedirector(self.output_text)
try:
rotate_angle = eval(self.option_entry_0.get())
pitch = float(self.option_entry_1.get())
wool = bool(self.wool_var.get())
concrete = bool(self.concrete_var.get())
terracotta = bool(self.terracotta_var.get())
glass = bool(self.glass_var.get())
def call_back(stage_index, stage_num, current_step, stage_steps):
progress_percent = (stage_index + current_step / stage_steps) / stage_num * 100
self.progress['value'] = progress_percent
self.update_idletasks()
save_path = os.path.join(game_path, 'config', 'mybuilds')
if not os.path.exists(save_path):
os.makedirs(save_path)
model_to_minecraft(
obj_file=obj_file,
save_path=save_path,
pitch=pitch,
rotate_angle=rotate_angle,
wool=wool,
concrete=concrete,
terracotta=terracotta,
glass=glass,
call_back=call_back
)
messagebox.showinfo(LANGUAGES[self.language]['title'], LANGUAGES[self.language]['convert_success']+save_path.replace('/', '\\'))
except Exception as e:
messagebox.showerror(LANGUAGES[self.language]['title'], LANGUAGES[self.language]['convert_failed']+f': {str(e)}')
finally:
sys.stdout = old_stdout
self.progress['value'] = 0
Thread(target=convert_thread).start()
def toggle_advanced(self, content):
if self.advanced_expanded.get():
content.pack_forget()
else:
content.pack()
self.advanced_expanded.set(not self.advanced_expanded.get())
if __name__ == '__main__':
app = App()
app.mainloop()