保存打包优化

This commit is contained in:
ayuan9957 2026-03-19 11:26:43 +08:00
parent 1aa21c2680
commit b9053b6a28
2 changed files with 3 additions and 149 deletions

View File

@ -28,7 +28,6 @@ from project.scene_description import (
build_scene_description_from_world,
build_runtime_manifest,
build_runtime_scene,
build_scene_description_from_cache,
load_json,
normalize_scene_description,
save_json,
@ -93,18 +92,12 @@ class ProjectManager:
elif not normalized_path.lower().endswith(SCENE_DESCRIPTION_EXTENSION):
normalized_path = f"Scenes/{scene_name}{SCENE_DESCRIPTION_EXTENSION}"
normalized_cache = str(scene_entry.get("cache_path", "") or "").replace("\\", "/").strip()
if normalized_cache.lower().endswith("scene.bam"):
normalized_cache = ""
normalized_entry = {
"guid": scene_guid,
"name": scene_name,
"path": normalized_path,
"enabled_in_build": bool(scene_entry.get("enabled_in_build", True)),
}
if normalized_cache:
normalized_entry["cache_path"] = normalized_cache
return normalized_entry
def _upgrade_scene_description_files(self, project_path, project_config):
@ -531,77 +524,6 @@ class ProjectManager:
return {}
return asset_database.register_asset(asset_path, copy_into_assets=False)
def _migrate_legacy_project_if_needed(self, project_path, project_config):
project_path = normalize_path(project_path)
if int(project_config.get("schema_version", 1) or 1) >= PROJECT_SCHEMA_VERSION:
return project_config
print(f"开始迁移旧项目到 v{PROJECT_SCHEMA_VERSION}: {project_path}")
self._create_legacy_backup(project_path)
layout = ProjectLayout(project_path)
ensure_project_directories(layout)
asset_database = AssetDatabase(project_path, world=self.world)
legacy_sources = [
os.path.join(project_path, "models"),
os.path.join(project_path, "textures"),
os.path.join(project_path, "Resources"),
os.path.join(project_path, "scripts"),
]
for source_root in legacy_sources:
if not os.path.exists(source_root):
continue
for root, _, files in os.walk(source_root):
# 跳过缓存目录与无意义的中间文件
if os.path.basename(root) == "__pycache__":
continue
for file_name in files:
if file_name.endswith((".meta", ".pyc", ".pyo")):
continue
source_file = os.path.join(root, file_name)
asset_database.import_asset(source_file)
scene_guid = generate_guid()
scene_name = "Main"
scene_entry = self._build_scene_entry(scene_guid, scene_name)
scene_paths = self._scene_paths(scene_entry, project_path=project_path)
legacy_scene_file = os.path.join(project_path, "scenes", "scene.bam")
if os.path.exists(legacy_scene_file):
legacy_gui = legacy_scene_file.replace(".bam", "_gui.json")
legacy_lui = legacy_scene_file.replace(".bam", "_lui.json")
if os.path.exists(legacy_gui):
shutil.copy2(legacy_gui, scene_paths["scene_gui_file"])
if os.path.exists(legacy_lui):
shutil.copy2(legacy_lui, scene_paths["scene_lui_file"])
migrated_config = self._create_default_project_config(project_path, project_config.get("name") or os.path.basename(project_path))
migrated_config["created_at"] = project_config.get("created_at") or project_config.get("created") or migrated_config["created_at"]
migrated_config["last_modified"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
migrated_config["scenes"] = [scene_entry]
migrated_config["startup_scene_guid"] = scene_guid
migrated_config["last_open_scene_guid"] = scene_guid
if os.path.exists(legacy_scene_file):
scene_description = build_scene_description_from_cache(
world=self.world,
asset_database=asset_database,
project_root=project_path,
scene_guid=scene_guid,
scene_name=scene_name,
scene_file_rel=scene_entry["path"],
cache_bam_path=legacy_scene_file,
cache_gui_path=scene_paths["scene_gui_file"],
cache_lui_path=scene_paths["scene_lui_file"],
camera_state=self._capture_camera_state(),
)
save_json(os.path.join(project_path, scene_entry["path"].replace("/", os.sep)), scene_description)
self._write_project_config(project_path, migrated_config)
print(f"✓ 旧项目迁移完成: {project_path}")
return migrated_config
def _retarget_live_scene_asset_paths(self):
asset_database = self.get_asset_database()
if not asset_database or not getattr(self.world, "render", None):
@ -783,7 +705,6 @@ class ProjectManager:
previous_project_path = self.current_project_path
previous_project_config = self.project_config
project_config = self._migrate_legacy_project_if_needed(project_path, project_config)
ensure_project_directories(ProjectLayout(project_path))
project_config = self._ensure_v2_project_defaults(project_path, project_config)
_, project_config = self._upgrade_scene_description_files(project_path, project_config)
@ -841,30 +762,6 @@ class ProjectManager:
"""
return self.openProject(project_path)
def _write_scene_description_from_cache(self, scene_entry, project_path):
scene_paths = self._scene_paths(scene_entry, project_path=project_path)
asset_database = self.get_asset_database(project_path, reload=True)
if asset_database:
asset_database.ensure_project_assets_registered()
legacy_cache_rel = str((scene_entry or {}).get("cache_path", "") or "").replace("/", os.sep)
legacy_cache_file = os.path.join(project_path, legacy_cache_rel) if legacy_cache_rel else ""
scene_description = build_scene_description_from_cache(
world=self.world,
asset_database=asset_database,
project_root=project_path,
scene_guid=scene_entry["guid"],
scene_name=scene_entry["name"],
scene_file_rel=scene_entry["path"],
cache_bam_path=legacy_cache_file,
cache_gui_path=scene_paths["scene_gui_file"],
cache_lui_path=scene_paths["scene_lui_file"],
camera_state=self._capture_camera_state(),
)
scene_file_path = os.path.join(project_path, scene_entry["path"].replace("/", os.sep))
save_json(scene_file_path, scene_description)
return scene_description
def _capture_scene_sidecar_snapshots(self, scene_paths):
gui_snapshot = self._capture_gui_snapshot_from_world()
lui_snapshot = self._capture_lui_snapshot_from_world()
@ -999,7 +896,7 @@ class ProjectManager:
gui_snapshot, lui_snapshot = self._capture_scene_sidecar_snapshots(scene_paths)
root_nodes = self._collect_live_scene_root_nodes()
if not root_nodes:
return self._write_scene_description_from_cache(scene_entry, project_path)
raise RuntimeError("当前场景没有可保存的根节点")
scene_description = build_scene_description_from_world(
asset_database=asset_database,
@ -1757,10 +1654,11 @@ class ProjectManager:
legacy_scene_bams.append(os.path.join(root, file_name))
validation["checks"].append(
{
"name": "no_project_scene_bam",
"name": "no_scene_bam_in_scene_pipeline",
"path": ", ".join(legacy_scene_bams),
"ok": not legacy_scene_bams,
"required": True,
"detail": "scene.bam 仅允许作为模型资源/模型缓存存在,不允许进入项目场景与运行时场景链路",
}
)
if legacy_scene_bams:

View File

@ -6,8 +6,6 @@ import json
import os
from datetime import datetime
from panda3d.core import Filename
from project.project_schema import (
ENGINE_NAME,
MANIFEST_SCHEMA_VERSION,
@ -370,48 +368,6 @@ def _build_scene_description_payload(
"nodes": node_entries,
}
return description
def build_scene_description_from_cache(
*,
world,
asset_database,
project_root: str,
scene_guid: str,
scene_name: str,
scene_file_rel: str,
cache_bam_path: str,
cache_gui_path: str = "",
cache_lui_path: str = "",
camera_state: dict | None = None,
):
gui_elements = load_json(cache_gui_path, [])
lui_snapshot = load_json(cache_lui_path, {})
scene_np = world.loader.loadModel(Filename.fromOsSpecific(cache_bam_path))
if not scene_np or scene_np.isEmpty():
raise RuntimeError(f"无法从缓存场景生成描述: {cache_bam_path}")
try:
root_nodes = list(scene_np.getChildren())
except Exception:
root_nodes = []
return _build_scene_description_payload(
asset_database=asset_database,
project_root=project_root,
scene_guid=scene_guid,
scene_name=scene_name,
scene_file_rel=scene_file_rel,
root_nodes=root_nodes,
cache_bam_path=cache_bam_path,
cache_gui_path=cache_gui_path,
cache_lui_path=cache_lui_path,
gui_elements=gui_elements,
lui_snapshot=lui_snapshot,
camera_state=camera_state,
)
def build_scene_description_from_world(
*,
asset_database,