diff --git a/main.py b/main.py index edf872b9..7008a509 100644 --- a/main.py +++ b/main.py @@ -838,19 +838,26 @@ def openProject(appw): world = appw.centralWidget().world return world.project_manager.openProject(appw) +def openProjectForPath(project_path, appw): + """打开项目 - 代理到project_manager""" + world = appw.centralWidget().world + return world.project_manager.openProjectForPath(project_path, appw) + def buildPackage(appw): """打包项目 - 代理到project_manager""" world = appw.centralWidget().world return world.project_manager.buildPackage(appw) - -if __name__ == "__main__": +def run(args = None): world = MyWorld() # 使用新的UI模块创建主窗口 from ui.main_window import setup_main_window - - app, main_window = setup_main_window(world) - + + app, main_window = setup_main_window(world, args) + # 启动应用程序 - sys.exit(app.exec_()) \ No newline at end of file + sys.exit(app.exec_()) + +if __name__ == "__main__": + run() \ No newline at end of file diff --git a/project/project_manager.py b/project/project_manager.py index cb55a175..5ed451ce 100644 --- a/project/project_manager.py +++ b/project/project_manager.py @@ -123,57 +123,157 @@ class ProjectManager: if not project_path: return False - + # 检查是否是有效的项目文件夹 config_file = os.path.join(project_path, "project.json") if not os.path.exists(config_file): QMessageBox.warning(parent_window, "警告", "选择的不是有效的项目文件夹!") return False - + # 读取项目配置 with open(config_file, "r", encoding="utf-8") as f: project_config = json.load(f) - + # 检查场景文件 scene_file = os.path.join(project_path, "scenes", "scene.bam") - if not os.path.exists(scene_file): - QMessageBox.warning(parent_window, "警告", "没有找到场景文件!") - return False - - # 加载场景 - if self.world.scene_manager.loadScene(scene_file): - # 更新项目配置 - project_config["last_modified"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") - project_config["scene_file"] = os.path.relpath(scene_file, project_path) - - with open(config_file, "w", encoding="utf-8") as f: - json.dump(project_config, f, ensure_ascii=False, indent=4) - - # 更新项目状态 - self.current_project_path = project_path - self.project_config = project_config - - # 保存当前项目路径到主窗口 - parent_window.current_project_path = project_path - - # 更新窗口标题 - project_name = os.path.basename(project_path) - self.updateWindowTitle(parent_window, project_name) - - # 更新文件浏览器 - if hasattr(parent_window, 'fileView') and hasattr(parent_window, 'fileModel'): - parent_window.fileView.setRootIndex(parent_window.fileModel.index(project_path)) - - QMessageBox.information(parent_window, "成功", "项目加载成功!") - return True - else: - QMessageBox.warning(parent_window, "错误", "加载场景失败!") - return False - + if os.path.exists(scene_file): + # 加载场景 + if self.world.scene_manager.loadScene(scene_file): + # 更新项目配置 + project_config["scene_file"] = os.path.relpath(scene_file, project_path) + + project_config["last_modified"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + with open(config_file, "w", encoding="utf-8") as f: + json.dump(project_config, f, ensure_ascii=False, indent=4) + + # 更新项目状态 + self.current_project_path = project_path + self.project_config = project_config + + # 保存当前项目路径到主窗口 + parent_window.current_project_path = project_path + + # 更新窗口标题 + project_name = os.path.basename(project_path) + self.updateWindowTitle(parent_window, project_name) + + # 更新文件浏览器 + if hasattr(parent_window, 'fileView') and hasattr(parent_window, 'fileModel'): + parent_window.fileView.setRootIndex(parent_window.fileModel.index(project_path)) + + QMessageBox.information(parent_window, "成功", "项目加载成功!") + return True + # 检查场景文件 + # scene_file = os.path.join(project_path, "scenes", "scene.bam") + # if not os.path.exists(scene_file): + # QMessageBox.warning(parent_window, "警告", "没有找到场景文件!") + # return False + # + # # 加载场景 + # if self.world.scene_manager.loadScene(scene_file): + # # 更新项目配置 + # project_config["last_modified"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + # project_config["scene_file"] = os.path.relpath(scene_file, project_path) + # + # with open(config_file, "w", encoding="utf-8") as f: + # json.dump(project_config, f, ensure_ascii=False, indent=4) + # + # # 更新项目状态 + # self.current_project_path = project_path + # self.project_config = project_config + # + # # 保存当前项目路径到主窗口 + # parent_window.current_project_path = project_path + # + # # 更新窗口标题 + # project_name = os.path.basename(project_path) + # self.updateWindowTitle(parent_window, project_name) + # + # # 更新文件浏览器 + # if hasattr(parent_window, 'fileView') and hasattr(parent_window, 'fileModel'): + # parent_window.fileView.setRootIndex(parent_window.fileModel.index(project_path)) + # + # QMessageBox.information(parent_window, "成功", "项目加载成功!") + # return True + # else: + # QMessageBox.warning(parent_window, "错误", "加载场景失败!") + # return False + except Exception as e: QMessageBox.critical(parent_window, "错误", f"加载项目时发生错误:{str(e)}") return False - + + def openProjectForPath(self, project_path, parent_window=None): + """通过路径打开项目 + + Args: + project_path: 项目路径 + parent_window: 父窗口对象(可选) + """ + try: + if not project_path: + return False + # 检查是否是有效的项目文件夹 + config_file = os.path.join(project_path, "project.json") + if not os.path.exists(config_file): + if parent_window: + QMessageBox.warning(parent_window, "警告", "选择的不是有效的项目文件夹!") + else: + print("警告: 选择的不是有效的项目文件夹!") + return False + + # 读取项目配置 + with open(config_file, "r", encoding="utf-8") as f: + project_config = json.load(f) + + # 检查场景文件 + scene_file = os.path.join(project_path, "scenes", "scene.bam") + if os.path.exists(scene_file): + # 加载场景 + if self.world.scene_manager.loadScene(scene_file): + # 更新项目配置 + project_config["scene_file"] = os.path.relpath(scene_file, project_path) + + project_config["last_modified"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + with open(config_file, "w", encoding="utf-8") as f: + json.dump(project_config, f, ensure_ascii=False, indent=4) + + # 更新项目状态 + self.current_project_path = project_path + self.project_config = project_config + + # 如果有父窗口,更新相关UI元素 + if parent_window: + # 保存当前项目路径到主窗口 + parent_window.current_project_path = project_path + + # 更新窗口标题 + project_name = os.path.basename(project_path) + self.updateWindowTitle(parent_window, project_name) + + # 更新文件浏览器 + if hasattr(parent_window, 'fileView') and hasattr(parent_window, 'fileModel'): + parent_window.fileView.setRootIndex(parent_window.fileModel.index(project_path)) + + QMessageBox.information(parent_window, "成功", "项目加载成功!") + + print(f"项目 '{project_path}' 加载成功!") + return True + else: + if parent_window: + QMessageBox.warning(parent_window, "错误", "加载场景失败!") + else: + print("错误: 加载场景失败!") + return False + + except Exception as e: + error_msg = f"加载项目时发生错误:{str(e)}" + if parent_window: + QMessageBox.critical(parent_window, "错误", error_msg) + else: + print(error_msg) + return False + def saveProject(self, parent_window): """保存项目""" try: diff --git a/ui/main_window.py b/ui/main_window.py index 3149265d..0f387532 100644 --- a/ui/main_window.py +++ b/ui/main_window.py @@ -606,10 +606,10 @@ class MainWindow(QMainWindow): self.addDockWidget(Qt.BottomDockWidgetArea, self.bottomDock) # 创建底部停靠控制台 - # self.consoleDock = QDockWidget("控制台", self) - # self.consoleView = CustomConsoleDockWidget(self.world) - # self.consoleDock.setWidget(self.consoleView) - # self.addDockWidget(Qt.BottomDockWidgetArea, self.consoleDock) + self.consoleDock = QDockWidget("控制台", self) + self.consoleView = CustomConsoleDockWidget(self.world) + self.consoleDock.setWidget(self.consoleView) + self.addDockWidget(Qt.BottomDockWidgetArea, self.consoleDock) def setupToolbar(self): """创建工具栏""" @@ -1864,7 +1864,7 @@ class MainWindow(QMainWindow): else: QMessageBox.warning(self, "错误", "高度图地形创建失败!") -def setup_main_window(world): +def setup_main_window(world,path = None): """设置主窗口的便利函数""" app = QApplication.instance() if app is None: @@ -1872,5 +1872,8 @@ def setup_main_window(world): main_window = MainWindow(world) main_window.show() - + from main import openProjectForPath + if path: + openProjectForPath(path,main_window) + return app, main_window \ No newline at end of file diff --git a/ui/widgets.py b/ui/widgets.py index f794ee07..fdd02226 100644 --- a/ui/widgets.py +++ b/ui/widgets.py @@ -607,8 +607,27 @@ class CustomAssetsTreeWidget(QTreeWidget): """获取项目根路径下的Resources文件夹,考虑跨平台""" import os - # 获取项目根路径 - project_root = os.getcwd() + # 获取当前文件所在目录,然后向上查找项目根目录 + current_dir = os.path.dirname(os.path.abspath(__file__)) + + # 向上查找直到找到项目根目录(包含特定标识文件或文件夹) + project_root = current_dir + max_depth = 10 # 限制向上查找的深度 + depth = 0 + + while depth < max_depth: + # 检查是否是项目根目录(可以根据实际情况调整判断条件) + if (os.path.exists(os.path.join(project_root, "main.py")) or + os.path.exists(os.path.join(project_root, "setup.py")) or + os.path.exists(os.path.join(project_root, ".git"))): + break + parent_dir = os.path.dirname(project_root) + if parent_dir == project_root: # 已经到达文件系统根目录 + # 回退到使用当前工作目录 + project_root = os.getcwd() + break + project_root = parent_dir + depth += 1 # 构建Resources文件夹路径(跨平台) resources_path = os.path.join(project_root, "Resources") @@ -625,6 +644,28 @@ class CustomAssetsTreeWidget(QTreeWidget): return resources_path + # def getProjectRootPath(self): + # """获取项目根路径下的Resources文件夹,考虑跨平台""" + # import os + # + # # 获取项目根路径 + # project_root = os.getcwd() + # + # # 构建Resources文件夹路径(跨平台) + # resources_path = os.path.join(project_root, "Resources") + # + # # 如果Resources文件夹不存在,创建它 + # if not os.path.exists(resources_path): + # try: + # os.makedirs(resources_path, exist_ok=True) + # print(f"创建Resources文件夹: {resources_path}") + # except OSError as e: + # print(f"无法创建Resources文件夹: {e}") + # # 如果无法创建,回退到项目根路径 + # return project_root + # + # return resources_path + def load_file_tree(self): """加载树形视图""" self.clear()