238 lines
7.4 KiB
Python
238 lines
7.4 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
简单的拖拽导入工具
|
||
直接运行此工具,然后将3D文件拖拽到窗口中
|
||
工具会生成可在demo.py中使用的导入命令
|
||
"""
|
||
|
||
import tkinter as tk
|
||
from tkinter import filedialog, messagebox, scrolledtext
|
||
import os
|
||
import sys
|
||
|
||
class SimpleDragDrop:
|
||
def __init__(self):
|
||
self.root = tk.Tk()
|
||
self.root.title("3D模型拖拽导入工具")
|
||
self.root.geometry("600x400")
|
||
|
||
# 设置窗口始终在最前面
|
||
self.root.attributes('-topmost', True)
|
||
|
||
# 支持的文件格式
|
||
self.supported_formats = ['.gltf', '.glb', '.fbx', '.bam', '.egg', '.obj']
|
||
|
||
self.setup_ui()
|
||
self.setup_drag_drop()
|
||
|
||
def setup_ui(self):
|
||
"""设置用户界面"""
|
||
# 主框架
|
||
main_frame = tk.Frame(self.root)
|
||
main_frame.pack(fill='both', expand=True, padx=10, pady=10)
|
||
|
||
# 标题
|
||
title_label = tk.Label(
|
||
main_frame,
|
||
text="将3D模型文件拖拽到此处",
|
||
font=('Arial', 16, 'bold')
|
||
)
|
||
title_label.pack(pady=(0, 10))
|
||
|
||
# 拖拽区域
|
||
self.drop_area = tk.Frame(
|
||
main_frame,
|
||
bg='lightgray',
|
||
relief='sunken',
|
||
bd=2,
|
||
height=150
|
||
)
|
||
self.drop_area.pack(fill='both', expand=True, pady=(0, 10))
|
||
|
||
# 提示文本
|
||
hint_label = tk.Label(
|
||
self.drop_area,
|
||
text="支持格式: .gltf, .glb, .fbx, .bam, .egg, .obj\n\n拖拽文件到此处或点击下方按钮选择文件",
|
||
bg='lightgray',
|
||
font=('Arial', 11)
|
||
)
|
||
hint_label.pack(expand=True)
|
||
|
||
# 按钮框架
|
||
button_frame = tk.Frame(main_frame)
|
||
button_frame.pack(fill='x', pady=(0, 10))
|
||
|
||
# 选择文件按钮
|
||
select_btn = tk.Button(
|
||
button_frame,
|
||
text="选择文件",
|
||
command=self.select_files,
|
||
width=15
|
||
)
|
||
select_btn.pack(side='left', padx=5)
|
||
|
||
# 清空按钮
|
||
clear_btn = tk.Button(
|
||
button_frame,
|
||
text="清空",
|
||
command=self.clear_files,
|
||
width=15
|
||
)
|
||
clear_btn.pack(side='left', padx=5)
|
||
|
||
# 生成命令按钮
|
||
generate_btn = tk.Button(
|
||
button_frame,
|
||
text="生成导入命令",
|
||
command=self.generate_commands,
|
||
bg='lightblue',
|
||
font=('Arial', 10, 'bold'),
|
||
width=15
|
||
)
|
||
generate_btn.pack(side='right', padx=5)
|
||
|
||
# 文件列表
|
||
list_label = tk.Label(
|
||
main_frame,
|
||
text="文件列表:",
|
||
font=('Arial', 10, 'bold')
|
||
)
|
||
list_label.pack(anchor='w', pady=(0, 5))
|
||
|
||
# 文件列表框
|
||
self.file_listbox = tk.Listbox(
|
||
main_frame,
|
||
height=8,
|
||
selectmode=tk.MULTIPLE
|
||
)
|
||
self.file_listbox.pack(fill='both', expand=True, pady=(0, 10))
|
||
|
||
# 命令输出区域
|
||
output_label = tk.Label(
|
||
main_frame,
|
||
text="导入命令:",
|
||
font=('Arial', 10, 'bold')
|
||
)
|
||
output_label.pack(anchor='w', pady=(0, 5))
|
||
|
||
self.command_text = scrolledtext.ScrolledText(
|
||
main_frame,
|
||
height=6,
|
||
width=70
|
||
)
|
||
self.command_text.pack(fill='both', expand=True)
|
||
|
||
def setup_drag_drop(self):
|
||
"""设置拖拽功能"""
|
||
try:
|
||
# 设置拖拽目标
|
||
self.drop_area.drop_target_register('DND_Files')
|
||
self.drop_area.dnd_bind('<<Drop>>', self.on_drop)
|
||
self.drop_area.dnd_bind('<<DragEnter>>', self.on_drag_enter)
|
||
self.drop_area.dnd_bind('<<DragLeave>>', self.on_drag_leave)
|
||
except:
|
||
# 如果拖拽不支持,只使用文件选择器
|
||
print("拖拽功能不可用,请使用文件选择器")
|
||
|
||
def on_drag_enter(self, event):
|
||
"""拖拽进入事件"""
|
||
self.drop_area.configure(bg='lightblue')
|
||
|
||
def on_drag_leave(self, event):
|
||
"""拖拽离开事件"""
|
||
self.drop_area.configure(bg='lightgray')
|
||
|
||
def on_drop(self, event):
|
||
"""拖拽释放事件"""
|
||
try:
|
||
self.drop_area.configure(bg='lightgray')
|
||
|
||
# 获取拖拽的文件
|
||
files = self.root.tk.splitlist(event.data)
|
||
|
||
for file_path in files:
|
||
self.add_file(file_path)
|
||
|
||
except Exception as e:
|
||
messagebox.showerror("错误", f"拖拽处理失败: {e}")
|
||
|
||
def add_file(self, file_path):
|
||
"""添加文件到列表"""
|
||
if os.path.exists(file_path):
|
||
file_ext = os.path.splitext(file_path)[1].lower()
|
||
if file_ext in self.supported_formats:
|
||
if file_path not in self.file_listbox.get(0, tk.END):
|
||
self.file_listbox.insert(tk.END, file_path)
|
||
else:
|
||
messagebox.showwarning("格式错误", f"不支持的文件格式: {file_ext}")
|
||
else:
|
||
messagebox.showerror("文件错误", f"文件不存在: {file_path}")
|
||
|
||
def select_files(self):
|
||
"""选择文件"""
|
||
files = filedialog.askopenfilenames(
|
||
title="选择3D模型文件",
|
||
filetypes=[
|
||
("所有支持的格式", "*.gltf *.glb *.fbx *.bam *.egg *.obj"),
|
||
("glTF文件", "*.gltf *.glb"),
|
||
("FBX文件", "*.fbx"),
|
||
("BAM文件", "*.bam"),
|
||
("EGG文件", "*.egg"),
|
||
("OBJ文件", "*.obj"),
|
||
("所有文件", "*.*")
|
||
]
|
||
)
|
||
|
||
for file_path in files:
|
||
self.add_file(file_path)
|
||
|
||
def clear_files(self):
|
||
"""清空文件列表"""
|
||
self.file_listbox.delete(0, tk.END)
|
||
self.command_text.delete(1.0, tk.END)
|
||
|
||
def generate_commands(self):
|
||
"""生成导入命令"""
|
||
files = list(self.file_listbox.get(0, tk.END))
|
||
|
||
if not files:
|
||
messagebox.showwarning("没有文件", "请先选择或拖拽文件")
|
||
return
|
||
|
||
# 生成Python导入代码
|
||
commands = "# 在demo.py的控制台中执行以下命令来导入文件:\n\n"
|
||
|
||
for i, file_path in enumerate(files, 1):
|
||
filename = os.path.basename(file_path)
|
||
commands += f"# 导入文件 {i}: {filename}\n"
|
||
commands += f"world._import_model_from_path('{file_path}')\n\n"
|
||
|
||
commands += "# 或者使用批量导入:\n"
|
||
commands += "files = [\n"
|
||
for file_path in files:
|
||
commands += f" '{file_path}',\n"
|
||
commands += "]\n"
|
||
commands += "for file_path in files:\n"
|
||
commands += " world._import_model_from_path(file_path)\n"
|
||
|
||
# 显示命令
|
||
self.command_text.delete(1.0, tk.END)
|
||
self.command_text.insert(1.0, commands)
|
||
|
||
messagebox.showinfo("完成", "导入命令已生成,请复制到demo.py控制台中执行")
|
||
|
||
def run(self):
|
||
"""运行应用"""
|
||
self.root.mainloop()
|
||
|
||
def main():
|
||
"""主函数"""
|
||
try:
|
||
app = SimpleDragDrop()
|
||
app.run()
|
||
except Exception as e:
|
||
print(f"启动拖拽工具失败: {e}")
|
||
input("按回车键退出...")
|
||
|
||
if __name__ == "__main__":
|
||
main() |