如何在pyqtgraph中绘制.obj文件?

2024-04-26 02:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

这行代码呈现一行

import pywavefront
import pyqtgraph.opengl as gl
from pyqtgraph.Qt import QtWidgets
import numpy as np
import sys

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = gl.GLViewWidget()

    xx = 0
    yx = 0
    zx = 0

    xy = 1
    yy = 0
    zy = 0

    Xdot = (xx, yx, zx)
    Ydot = (xy, yy, zy)

    pts = np.array([Xdot, Ydot])
    sh1 = gl.GLLinePlotItem(pos=pts, width=1, antialias=False)
    w.addItem(sh1)
    w.show()
    app.exec()

如何让pyqtgraph呈现.obj文件?添加下面的行

scene = pywavefront.Wavefront('./mogaze/meshfiles/cup.obj',create_materials=True)
w.addItem(scene)

返回错误

QWindowsContext: OleInitialize() failed:  "COM error 0xffffffff80010106 RPC_E_CHANGED_MODE (Unknown error 0x080010106)"
Unimplemented OBJ format statement 's' on line 's 1'
Traceback (most recent call last):
  File "draw-human-objects-pred.py", line 137, in <module>
    w.addItem(scene)
  File "C:\Users\ha_ha\anaconda3\envs\pywavefront\lib\site-packages\pyqtgraph\opengl\GLViewWidget.py", line 64, in addItem
    item._setView(self)
AttributeError: 'Wavefront' object has no attribute '_setView'

Tags: importappasnpsyslinesceneopengl
2条回答

使用pyqtgraph's{}{a1}但是需要有3个点的顶点

您可以使用pywavefront加载.obj文件,以获得每个面具有3个顶点的.obj文件。 https://github.com/pywavefront/PyWavefront/issues/87,然后用pyqtgraph加载它

我认为它不适合添加到小部件中

下面是我如何加载和显示多维数据集的示例:

enter image description here

root_path = os.path.dirname(__file__)
scene = pywavefront.Wavefront(os.path.join(root_path, 'data/box/box-V3F.obj'))
pts2 = np.array(scene.vertices)
sh2 = gl.GLLinePlotItem(pos=pts2, mode='line_strip')
sh3 = gl.GLScatterPlotItem(pos=pts2)
w.addItem(sh2)
w.addItem(sh3)

这是box-V3F.obj文件的内容,我在其中注释掉了第 s off行:

# Blender v2.78 (sub 0) OBJ File: 'box.blend'
# www.blender.org
mtllib box.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl Material
#s off
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8

我对这个库没有太多的经验,但我希望这对您有所帮助

相关问题 更多 >