Draw lines in Qt3D in Python

113 views Asked by At

I'm tries genreate simple wireframe object in Qt3D and I fail every time. I'm very new in Qt3D and don't know many things, so even one simple example will be helpful.

I do not know if it will be helpful but i have AMD GPU (RX 6800) and DirectX 12

I writed that simple code, but when my window appear, is nothing in it and console displays: findBoundingVolumeComputeData: Position attribute not suited for bounding volume computation

My code thats doesn't work:

import sys
from PySide6.QtCore import QByteArray
from PySide6.QtWidgets import QApplication
from PySide6.Qt3DExtras import Qt3DExtras
from PySide6.Qt3DCore import Qt3DCore
from PySide6.Qt3DRender import Qt3DRender
from PySide6.QtGui import QColor, QGuiApplication, QVector3D

class LineMeshGeometry:
    def __init__(self, vertices, parent):
        self.geometry = Qt3DCore.QGeometry(parent)
        self._positionAttribute = Qt3DCore.QAttribute(self.geometry)
        self._vertexBuffer = Qt3DCore.QBuffer(self.geometry)
        self.vertexBufferData = QByteArray()
        self.float_size = sys.getsizeof(float())
        self.vertexBufferData.resize(len(vertices) * 3 * self.float_size)
        self.rawVertexArr = self.vertexBufferData.data()
        self.rawVertexArr = bytearray(self.rawVertexArr)
        self.rawVertexArr = memoryview(self.rawVertexArr).cast('f')
        self.idx = 0
        for v in vertices:
            self.rawVertexArr[self.idx] = v.x()
            self.rawVertexArr[self.idx+1] = v.y()
            self.rawVertexArr[self.idx+2] = v.z()
            self.idx = self.idx + 3

        self._vertexBuffer.setData(self.vertexBufferData)
        self._positionAttribute.setAttributeType(Qt3DCore.QAttribute.VertexAttribute)
        self._positionAttribute.setBuffer(self._vertexBuffer)
        self._positionAttribute.setName("Buff")

        self.lineEntity = Qt3DCore.QEntity(parent)

        self.line = Qt3DRender.QGeometryRenderer(parent)
        self.line.setGeometry(self.geometry)
        self.line.setRestartIndexValue(-1)
        self.line.setPrimitiveRestartEnabled(True)
        self.line.setPrimitiveType(Qt3DRender.QGeometryRenderer.LineStrip)
        self.material = Qt3DExtras.QDiffuseSpecularMaterial(parent)
        self.material.setAmbient(QColor(0,255, 50))
        self.transform = Qt3DCore.QTransform()
        self.transform.setScale(2.0)
        
        self.lineEntity.addComponent(self.line)
        self.lineEntity.addComponent(self.material)
        self.lineEntity.addComponent(self.transform)


class Window(Qt3DExtras.Qt3DWindow):
    def __init__(self):
        super().__init__()

        self.camera().lens().setPerspectiveProjection(45, 16 / 9, 0.1, 1000)
        self.camera().setPosition(QVector3D(0, 0, 15))
        self.camera().setViewCenter(QVector3D(0, 0, 0))

        self.createScene()
        self.camController = Qt3DExtras.QOrbitCameraController(self.rootEntity)
        self.camController.setLinearSpeed(50)
        self.camController.setLookSpeed(180)
        self.camController.setCamera(self.camera())

        self.setRootEntity(self.rootEntity)

    def createScene(self):
        self.rootEntity = Qt3DCore.QEntity()

        self.material = Qt3DExtras.QDiffuseSpecularMaterial(self.rootEntity)
        self.material.setDiffuse(QColor(100,155,34))
        self.material.setAmbient(QColor(255,255,255))

        self.cubeEntity = Qt3DCore.QEntity(self.rootEntity)
        self.cubeMesh = Qt3DExtras.QCuboidMesh()
        self.cubeMesh.setXExtent(1.0)
        self.cubeMesh.setYExtent(1.0)
        self.cubeMesh.setZExtent(1.0)

        self.transform = Qt3DCore.QTransform()
        self.transform.setScale(0.1)
        
        self.cubeEntity.addComponent(self.cubeMesh)
        self.cubeEntity.addComponent(self.material)
        self.cubeEntity.addComponent(self.transform)


if __name__ == "__main__":

    app = QGuiApplication(sys.argv)
    view = Window()
    view.show()
    vec = [QVector3D(0, 0, 0), QVector3D(1, 0, 0), QVector3D(1, 1, 0), QVector3D(0, 1, 0)]

    line = LineMeshGeometry(vec, view.rootEntity)

    sys.exit(app.exec())
0

There are 0 answers