This commit is contained in:
Hector 2025-08-14 11:38:21 +08:00
parent 3261163ec8
commit 29621d5c18
10 changed files with 132 additions and 19 deletions

Binary file not shown.

View File

@ -142,20 +142,121 @@ class InterfaceManager:
lightItem = QTreeWidgetItem(sceneRoot,['灯光'])
BLACK_LIST={'','**','temp','collision'}
def should_skip(node):
name = node.getName()
return name in BLACK_LIST or name.startswith('__')
def addNodeToTree(node,parentItem,force=False):
if not force and should_skip(node):
# 用于跟踪已添加的节点,避免重复
added_nodes = set()
# 检查是否有图标配置
if not hasattr(self, 'item_icons'):
self.item_icons = {
'model': None,
'geometry': None,
'empty': None,
'default': None
}
def _add_children_recursive(parent_item, node_path):
"""递归添加子节点到树项 - 使用用户提供的有效方法"""
# 防止重复添加
node_id = id(node_path)
if node_id in added_nodes:
print(f"⚠️ 跳过重复节点: {node_path.getName()}")
return
nodeItem = QTreeWidgetItem(parentItem, [node.getName()])
nodeItem.setData(0,Qt.UserRole,node)
for child in node.getChildren():
addNodeToTree(child,nodeItem,force=False)
added_nodes.add(node_id)
# 获取所有子节点
children = node_path.getChildren()
for i in range(children.getNumPaths()):
child_node = children.getPath(i)
# 跳过黑名单节点
child_name = child_node.getName()
if child_name in {'', '**', 'temp', 'collision'} or child_name.startswith('__'):
continue
# 创建子项
child_item = QTreeWidgetItem(parent_item)
display_name = child_name or f"Child_{i}"
child_item.setText(0, display_name)
# 检查是否是几何节点并设置图标
if child_node.find("**/+GeomNode").isEmpty():
# 不是几何节点
if hasattr(self, 'item_icons') and self.item_icons.get('model'):
child_item.setIcon(0, self.item_icons.get('model', self.item_icons['default']))
display_name += " 📦" # 模型节点标记
else:
# 是几何节点
if hasattr(self, 'item_icons') and self.item_icons.get('empty'):
child_item.setIcon(0, self.item_icons.get('empty', self.item_icons['default']))
display_name += " 🔷" # 几何节点标记
# 更新显示名称
child_item.setText(0, display_name)
# 存储NodePath引用
child_item.setData(0, Qt.UserRole, child_node)
# 添加节点信息到提示
node_info = []
if hasattr(child_node, 'node') and child_node.node():
node_info.append(f"类型: {type(child_node.node()).__name__}")
if child_node.getNumChildren() > 0:
node_info.append(f"子节点: {child_node.getNumChildren()}")
if hasattr(child_node, 'getTags') and child_node.getTags():
tags = [f"{k}:{v}" for k, v in child_node.getTags().items()]
node_info.append(f"标签: {', '.join(tags)}")
if node_info:
child_item.setToolTip(0, "\n".join(node_info))
print(f"+ 添加节点: {display_name}")
# 递归处理子节点的子节点
if child_node.getNumChildren() > 0:
_add_children_recursive(child_item, child_node)
def addNodeToTree(node, parentItem, force=True):
"""添加单个节点到树 - 包装函数"""
node_id = id(node)
if node_id in added_nodes:
print(f"⚠️ 跳过重复根节点: {node.getName()}")
return
added_nodes.add(node_id)
# 创建根节点项
display_name = node.getName() or "<Unknown>"
# 检查是否为GLB模型
is_glb_model = False
if hasattr(node, 'getTags'):
tags = node.getTags() if hasattr(node, 'getTags') else {}
is_glb_model = any('glb' in str(v).lower() or 'gltf' in str(v).lower() for v in tags.values())
if not is_glb_model:
node_name = node.getName().lower() if node.getName() else ""
is_glb_model = '.glb' in node_name or '.gltf' in node_name
if is_glb_model:
display_name += " 🎯 GLB"
print(f"🎯 GLB模型检测到: {display_name}")
else:
display_name += " 📦"
print(f"📦 常规模型: {display_name}")
nodeItem = QTreeWidgetItem(parentItem, [display_name])
nodeItem.setData(0, Qt.UserRole, node)
# 添加根节点信息
node_info = []
if hasattr(node, 'node') and node.node():
node_info.append(f"类型: {type(node.node()).__name__}")
if node.getNumChildren() > 0:
node_info.append(f"子节点: {node.getNumChildren()}")
if node_info:
nodeItem.setToolTip(0, "\n".join(node_info))
# 使用改进的递归方法处理子节点
_add_children_recursive(nodeItem, node)
# 递归添加节点及其子节点
# def addNodeToTree(node, parentItem):
@ -177,9 +278,16 @@ class InterfaceManager:
# else:
# print(f"跳过节点: {child.getName()}")
# 清空去重集合
added_nodes.clear()
# 添加所有模型及其子节点
for model in self.world.models:
addNodeToTree(model, modelsItem,force=True)
print(f"\n=== 开始处理 {len(self.world.models)} 个模型 ===")
for i, model in enumerate(self.world.models):
model_name = model.getName() or f"Model_{i}"
print(f"\n--- 处理模型 {i+1}: {model_name} ---")
addNodeToTree(model, modelsItem, force=True)
print("=== 模型处理完成 ===")
# 添加所有GUI元素
for gui_element in self.world.gui_elements:
@ -191,10 +299,15 @@ class InterfaceManager:
guiElementItem.setData(0, Qt.UserRole, gui_element)
print(f"添加GUI元素: {display_name}")
for light_element in self.world.Spotlight:
addNodeToTree(light_element,lightItem)
for light_element in self.world.Pointlight:
addNodeToTree(light_element,lightItem)
# 添加聚光灯
if hasattr(self.world, 'Spotlight'):
for light_element in self.world.Spotlight:
addNodeToTree(light_element, lightItem, force=True)
# 添加点光源
if hasattr(self.world, 'Pointlight'):
for light_element in self.world.Pointlight:
addNodeToTree(light_element, lightItem, force=True)
# 添加地板节点
if hasattr(self.world, 'ground'):