ZoneMaestro_code / eval /respace /debug_rendering.py
kkkkiiii's picture
Add files using upload-large-folder tool
ebc85b3 verified
Raw
History Blame Contribute Delete
10.8 kB
"""
调试渲染问题:检查3D资产的着色情况
"""
import os
import json
import trimesh
import numpy as np
from pathlib import Path
from src.respace import ReSpace
from src.utils import get_pth_mesh
from dotenv import load_dotenv
load_dotenv(".env")
def check_environment():
"""检查环境变量和路径"""
print("=== 环境检查 ===")
pth_assets = os.getenv("PTH_3DFUTURE_ASSETS")
print(f"PTH_3DFUTURE_ASSETS: {pth_assets}")
if pth_assets and os.path.exists(pth_assets):
print(f"✅ 资产路径存在")
# 列出几个资产示例
assets = [d for d in os.listdir(pth_assets) if os.path.isdir(os.path.join(pth_assets, d))][:5]
print(f"资产示例: {assets}")
else:
print(f"❌ 资产路径不存在")
return pth_assets
def check_specific_asset(jid):
"""检查特定资产的详细信息"""
print(f"\n=== 检查资产: {jid} ===")
try:
mesh_path = get_pth_mesh(jid)
print(f"资产路径: {mesh_path}")
if not os.path.exists(mesh_path):
print(f"❌ 资产文件不存在: {mesh_path}")
return None
print(f"✅ 资产文件存在")
# 加载mesh
mesh = trimesh.load(mesh_path)
print(f"Mesh类型: {type(mesh)}")
if isinstance(mesh, trimesh.Scene):
print(f"场景包含 {len(mesh.geometry)} 个几何体")
for i, (name, geom) in enumerate(mesh.geometry.items()):
print(f" 几何体 {i}: {name}")
check_mesh_materials(geom, f"{jid}-{i}")
else:
print("单个mesh")
check_mesh_materials(mesh, jid)
return mesh
except Exception as e:
print(f"❌ 加载资产失败: {e}")
return None
def check_mesh_materials(mesh, mesh_name):
"""检查mesh的材质信息"""
print(f" 材质检查 ({mesh_name}):")
if hasattr(mesh, 'visual'):
print(f" ✅ 有visual属性")
if hasattr(mesh.visual, 'material'):
material = mesh.visual.material
print(f" ✅ 有material属性: {type(material)}")
# 检查各种材质属性
if hasattr(material, 'baseColorTexture') and material.baseColorTexture is not None:
texture = material.baseColorTexture
print(f" ✅ 有baseColorTexture: {type(texture)}, shape: {getattr(texture, 'shape', 'N/A')}")
else:
print(f" ❌ 没有baseColorTexture")
if hasattr(material, 'image') and material.image is not None:
print(f" ✅ 有image属性: {type(material.image)}")
else:
print(f" ❌ 没有image属性")
if hasattr(material, 'main_color'):
print(f" main_color: {material.main_color}")
if hasattr(material, 'baseColorFactor'):
print(f" baseColorFactor: {material.baseColorFactor}")
else:
print(f" ❌ 没有material属性")
else:
print(f" ❌ 没有visual属性")
def process_scene_data(scene_data):
"""
处理场景数据,统一格式,参考render_view_from_group_jsons.py中的逻辑
Args:
scene_data: 原始场景数据
Returns:
处理后的场景数据
"""
if not scene_data:
return None
# 处理房间包络 - 支持两种格式
# 格式1: 嵌套在 room_envelop/room_envelope 中
if "room_envelop" in scene_data:
envelop_data = scene_data["room_envelop"]
if isinstance(envelop_data, dict) and "bounds_top" in envelop_data and "bounds_bottom" in envelop_data:
scene_data["bounds_top"] = envelop_data["bounds_top"]
scene_data["bounds_bottom"] = envelop_data["bounds_bottom"]
print("检测到嵌套格式的 room_envelop")
elif "room_envelope" in scene_data:
envelope_data = scene_data["room_envelope"]
if isinstance(envelope_data, dict) and "bounds_top" in envelope_data and "bounds_bottom" in envelope_data:
scene_data["bounds_top"] = envelope_data["bounds_top"]
scene_data["bounds_bottom"] = envelope_data["bounds_bottom"]
print("检测到嵌套格式的 room_envelope")
# 格式2: 直接在根级别的 bounds_top 和 bounds_bottom
elif "bounds_top" in scene_data and "bounds_bottom" in scene_data:
# 数据已经在正确位置,无需处理
print("检测到直接格式的 bounds_top/bounds_bottom")
else:
print("警告: 未找到有效的房间边界数据")
# 验证边界数据格式
if "bounds_top" in scene_data and "bounds_bottom" in scene_data:
bounds_top = scene_data["bounds_top"]
bounds_bottom = scene_data["bounds_bottom"]
# 检查是否为有效的坐标列表
if (isinstance(bounds_top, list) and isinstance(bounds_bottom, list) and
len(bounds_top) > 0 and len(bounds_bottom) > 0 and
all(isinstance(point, list) and len(point) >= 3 for point in bounds_top) and
all(isinstance(point, list) and len(point) >= 3 for point in bounds_bottom)):
print(f"房间边界验证通过: top={len(bounds_top)}点, bottom={len(bounds_bottom)}点")
else:
print("警告: 房间边界数据格式不正确")
# 展平objects - 处理分组格式
if "groups" in scene_data:
scene_data["objects"] = []
print("检测到分组格式的objects,开始展平...")
for i, group in enumerate(scene_data["groups"]):
if isinstance(group, dict) and "objects" in group:
group_objects = group["objects"]
if isinstance(group_objects, list):
scene_data["objects"].extend(group_objects)
print(f"从第{i+1}个组中添加了{len(group_objects)}个物体")
print(f"总共展平了{len(scene_data['objects'])}个物体")
elif "objects" in scene_data and isinstance(scene_data["objects"], list):
print(f"检测到直接格式的objects: {len(scene_data['objects'])}个物体")
else:
print("警告: 未找到有效的objects数据")
return scene_data
def test_scene_objects(scene_path):
"""测试场景中的对象"""
print(f"\n=== 测试场景对象: {scene_path} ===")
with open(scene_path, "r") as f:
scene = json.load(f)
# 处理场景数据
scene = process_scene_data(scene)
if not scene:
print("❌ 场景数据处理失败")
return
objects = scene.get("objects", [])
print(f"场景包含 {len(objects)} 个对象")
for i, obj in enumerate(objects):
jid = obj.get("jid") or obj.get("sampled_asset_jid")
if jid:
print(f"\n对象 {i+1}: {obj.get('desc', 'No description')[:50]}...")
check_specific_asset(jid)
else:
print(f"\n对象 {i+1}: ❌ 没有jid")
def test_rendering_process():
"""测试渲染过程"""
print(f"\n=== 测试渲染过程 ===")
# 使用附件中的场景数据作为测试
test_scene_path = "/home/v-meiszhang/amlt-project/respace/dataset-ssr3dfront_stage1/0a9c667d-033d-448c-b17c-dc55e6d3c386-0.json"
# 备选场景路径
backup_scene_path = "/home/v-meiszhang/amlt-project/respace/eval/viz/qwen7b_vs_respace/a167ca18-be65-4a03-8f88-030dfbc21e57-400e5a85-68bc-4b07-9b9d-8f2e5785f6c5/gt/scene_data.json"
scene_path = test_scene_path if os.path.exists(test_scene_path) else backup_scene_path
if not os.path.exists(scene_path):
print(f"❌ 测试场景不存在: {scene_path}")
return
print(f"✅ 使用测试场景: {scene_path}")
try:
respace = ReSpace()
print(f"✅ ReSpace初始化成功")
with open(scene_path, "r") as f:
scene = json.load(f)
# 处理场景数据格式
print(f"🔄 处理场景数据格式...")
scene = process_scene_data(scene.copy())
if not scene:
print("❌ 场景数据处理失败")
return
# 检查是否需要重新采样资产
objects = scene.get("objects", [])
need_resampling = False
for obj in objects:
if "sampled_asset_jid" not in obj and "jid" in obj:
need_resampling = True
break
if need_resampling:
print(f"🔄 需要重新采样资产...")
scene = respace.resample_all_assets(scene, is_greedy_sampling=True)
print(f"✅ 资产重新采样完成")
# 测试渲染
output_dir = Path("./eval/viz/debug_render")
output_dir.mkdir(parents=True, exist_ok=True)
print(f"🎨 开始渲染...")
respace.render_scene_frame(scene, filename="debug_test", pth_viz_output=output_dir)
print(f"✅ 渲染完成,输出目录: {output_dir}")
except Exception as e:
print(f"❌ 渲染过程失败: {e}")
import traceback
traceback.print_exc()
def main():
"""主函数"""
print("🔍 开始调试渲染问题...")
# 1. 检查环境
pth_assets = check_environment()
if not pth_assets:
print("❌ 环境配置有问题,无法继续")
return
# 2. 测试场景对象 - 优先使用附件中的场景
test_scene_path = "/home/v-meiszhang/amlt-project/respace/eval/viz/qwen7b_vs_respace/a167ca18-be65-4a03-8f88-030dfbc21e57-400e5a85-68bc-4b07-9b9d-8f2e5785f6c5/gt/scene_data.json"
backup_scene_path = "/home/v-meiszhang/amlt-project/respace/eval/viz/qwen7b_vs_respace/a167ca18-be65-4a03-8f88-030dfbc21e57-400e5a85-68bc-4b07-9b9d-8f2e5785f6c5/gt/scene_data.json"
scene_path = test_scene_path if os.path.exists(test_scene_path) else backup_scene_path
if os.path.exists(scene_path):
test_scene_objects(scene_path)
# 3. 测试渲染过程
test_rendering_process()
print(f"\n🎯 调试完成!")
print(f"\n可能的问题原因:")
print(f"1. PTH_3DFUTURE_ASSETS环境变量未设置或路径不正确")
print(f"2. 3D资产文件缺失或损坏")
print(f"3. 资产没有正确的材质/纹理信息")
print(f"4. 资产采样过程中出现问题")
print(f"5. 渲染过程中材质处理失败")
print(f"6. 场景数据格式需要处理(room_envelope/groups展平)")
if __name__ == "__main__":
main()