You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ursina引擎中'panda3d.core.NodePath'无'uvs'属性错误求助

Ursina复刻Minecraft物品栏时AttributeError修复方案

问题说明

使用Ursina引擎复刻Minecraft制作物品栏位时,触发AttributeError,提示'panda3d.core.NodePath' object has no attribute 'uvs',该错误出现在Item类的set_texture方法中。

现有代码

Item类__init__方法

def __init__(self):
    super().__init__()
    self.model = "quad"
    # Hotspot.scalar来自其他类,运行正常
    self.scale_x = Hotspot.scalar * 0.9
    self.scale_y = self.scale_x
    self.color = color.white
    self.texture = load_texture("texture_atlas_3.png")
    self.texture_scale *= 64 / self.texture.width

set_texture方法

def set_texture(self):
    # 用字典获取UV坐标,minerals是存储所有方块类型x/y值的字典
    uu = minerals[self.blockType][0]
    uv = minerals[self.blockType][1] 
    basemod = load_model("block.obj", use_deepcopy=True)
    cb = copy(basemod.uvs)
    del cb[:-33]
    self.model.uvs = [Vec2(uu, uv) + u for u in cb]
    self.model.generate()
    self.rotation_z = 180

报错信息

File "c:\Users\alber\OneDrive\Documents\Dev\Minecraft\inventory_sys.py", line 54, in set_texture
    self.model.uvs = [Vec2(uu, uv) + u for u in cb]
    ^^^^^^^^^^^^^^
AttributeError: 'panda3d.core.NodePath' object has no attribute 'uvs'

修复方案

核心原因

当给Entity的model赋值为字符串(如"quad")时,Ursina会自动将其转换为Panda3D的NodePath对象,而NodePath没有uvs属性。只有Ursina的Model类实例才支持直接操作uvs。

具体修复步骤

  1. 导入Ursina的Model类:
from ursina import Model
  1. 修改Item类的__init__方法,将self.model = "quad"改为创建Model实例:
def __init__(self):
    super().__init__()
    self.model = Model("quad")  # 改为Model实例而非字符串
    self.scale_x = Hotspot.scalar * 0.9
    self.scale_y = self.scale_x
    self.color = color.white
    self.texture = load_texture("texture_atlas_3.png")
    self.texture_scale *= 64 / self.texture.width
  1. 保持set_texture方法逻辑不变,此时self.model是Model实例,可正常访问uvs属性。

内容的提问来源于stack exchange,提问作者user22392244

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 09:33:24