Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PySide2 Qt3D mesh does not show up

I'm diving into Qt3D framework and have decided to replicate a simplified version of this c++ example

Unfortunately, I don't see a torus mesh on application start. I've created all required entities and enabled a mesh in SceneModifier class.

What could be a problem with it? I thought that I've had a bad camera implementation, but it seems ok. Same with point light.

import sys
from PySide2 import QtWidgets, QtCore, QtGui
from PySide2.Qt3DCore import Qt3DCore
from PySide2.Qt3DExtras import Qt3DExtras
from PySide2.Qt3DRender import Qt3DRender


class SceneModifier(QtCore.QObject):
    def __init__(self, root_entity=None):
        super().__init__()

        # Scene entity
        self._m_root_entity = Qt3DCore.QEntity(root_entity)

        # Torus shape data
        self.m_torus = Qt3DExtras.QTorusMesh()
        self.m_torus.setRadius(1.0)
        self.m_torus.setMinorRadius(0.4)
        self.m_torus.setRings(100)
        self.m_torus.setSlices(20)

        # Torus transform
        torus_transform = Qt3DCore.QTransform()
        torus_transform.setScale(2.0)
        torus_transform.setRotation(QtGui.QQuaternion.fromAxisAndAngle(QtGui.QVector3D(0.0, 0.1, 0.0), 25.0))
        torus_transform.setTranslation(QtGui.QVector3D(0.0, 0.0, 0.0))

        # Torus material
        torus_mat = Qt3DExtras.QPhongMaterial()
        torus_mat.setDiffuse(QtGui.QColor(255, 102, 0))

        # Torus mesh
        self.m_torus_entity = Qt3DCore.QEntity(self._m_root_entity)
        self.m_torus_entity.addComponent(self.m_torus)
        self.m_torus_entity.addComponent(torus_mat)
        self.m_torus_entity.addComponent(torus_transform)

        self.m_torus_entity.setEnabled(True)


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

    view = Qt3DExtras.Qt3DWindow()
    view.defaultFrameGraph().setClearColor(QtGui.QColor(89, 89, 89))

    container = QtWidgets.QWidget.createWindowContainer(view)
    screen_size = QtCore.QSize(view.screen().size())
    container.setMinimumSize(QtCore.QSize(720, 680))
    container.setMaximumSize(QtCore.QSize(screen_size))

    root_entity = Qt3DCore.QEntity()

    camera_entity = Qt3DRender.QCamera(view.camera())
    camera_entity.lens().setPerspectiveProjection(45.0, 16.0/9.0, 0.1, 1000.0)
    camera_entity.setPosition(QtGui.QVector3D(0, 0, 20.0))
    camera_entity.setUpVector(QtGui.QVector3D(0, 1, 0))
    camera_entity.setViewCenter(QtGui.QVector3D(0, 0, 0))

    light_entity = Qt3DCore.QEntity(root_entity)
    point_light = Qt3DRender.QPointLight(light_entity)
    point_light.setColor("white")
    point_light.setIntensity(1)
    light_entity.addComponent(point_light)
    light_transform = Qt3DCore.QTransform(light_entity)
    light_transform.setTranslation(camera_entity.position())
    light_entity.addComponent(light_transform)

    cam_control = Qt3DExtras.QFirstPersonCameraController(root_entity)
    cam_control.setCamera(camera_entity)

    modifier = SceneModifier(root_entity=root_entity)

    view.setRootEntity(root_entity)

    widget = QtWidgets.QWidget()
    h_layout = QtWidgets.QHBoxLayout()
    h_layout.addWidget(container)
    widget.setLayout(h_layout)

    widget.show()

    sys.exit(app.exec_())

like image 728
Artem Avatar asked Mar 08 '20 09:03

Artem


People also ask

How to compile PySide and Qt on Windows?

Unlike with C++, there is absolutely nothing to compile. What you have to do is just to create a new file and write some lines of code. So to stay synchronized with this tutorial let's create a Python file at this location: The Qt window should appear. If yes then PySide and Qt framework are ready to go further on your computer.

How to install PySide2 and shiboken2 in Python?

Now that you have Python on your OS, let's install PySide2 and Shiboken2. It's actually quite easy, just open a console and type the following: You have now PySide2 and Shiboken2 ready to be used. Shiboken2 won't be used in this tutorial, indeed PySide2 comes already generated with all what we need.

How to use PySide2 to create badprogwindow in Python?

It’s where PySide2 will help us by providing some useful tools such as: To use it you have to open it from a console. So open a console in the following location (where you saved your UI file): Here we are saying to PySide2 to create a new Python file BadprogWindow.py from the UI file.


1 Answers

I have implemented the translation of example Qt 3D: Basic Shapes C++ Example into PySide2:

import sys
from PySide2 import QtWidgets, QtCore, QtGui
from PySide2.Qt3DCore import Qt3DCore
from PySide2.Qt3DExtras import Qt3DExtras
from PySide2.Qt3DRender import Qt3DRender
from PySide2.Qt3DInput import Qt3DInput


class SceneModifier(QtCore.QObject):
    def __init__(self, root_entity=None):
        super().__init__()
        self.m_rootEntity = root_entity

        self.m_torus = Qt3DExtras.QTorusMesh(
            radius=1.0, minorRadius=0.4, rings=100, slices=20
        )

        self.torusTransform = Qt3DCore.QTransform(
            scale=2.0,
            rotation=QtGui.QQuaternion.fromAxisAndAngle(
                QtGui.QVector3D(0.0, 1.0, 0.0), 25.0
            ),
            translation=QtGui.QVector3D(5.0, 4.0, 0.0),
        )

        self.torusMaterial = Qt3DExtras.QPhongMaterial(diffuse=QtGui.QColor("#beb32b"))

        self.m_torusEntity = Qt3DCore.QEntity(self.m_rootEntity)
        self.m_torusEntity.addComponent(self.m_torus)
        self.m_torusEntity.addComponent(self.torusMaterial)
        self.m_torusEntity.addComponent(self.torusTransform)

        self.cone = Qt3DExtras.QConeMesh(
            topRadius=0.5, bottomRadius=1, length=3, rings=50, slices=20
        )

        self.coneTransform = Qt3DCore.QTransform(
            scale=1.5,
            rotation=QtGui.QQuaternion.fromAxisAndAngle(
                QtGui.QVector3D(1.0, 4.0, -1.5), 45.0
            ),
            translation=QtGui.QVector3D(0.0, 4.0, -1.5),
        )

        self.coneMaterial = Qt3DExtras.QPhongMaterial(diffuse=QtGui.QColor("#928327"))

        self.m_coneEntity = Qt3DCore.QEntity(self.m_rootEntity)
        self.m_coneEntity.addComponent(self.cone)
        self.m_coneEntity.addComponent(self.coneMaterial)
        self.m_coneEntity.addComponent(self.coneTransform)

        self.cylinder = Qt3DExtras.QCylinderMesh(
            radius=1, length=3, rings=100, slices=20
        )

        self.cylinderTransform = Qt3DCore.QTransform(
            scale=1.5,
            rotation=QtGui.QQuaternion.fromAxisAndAngle(
                QtGui.QVector3D(1.0, 0.0, 0.0), 45.0
            ),
            translation=QtGui.QVector3D(-5.0, 4.0, -1.5),
        )

        self.cylinderMaterial = Qt3DExtras.QPhongMaterial(
            diffuse=QtGui.QColor("#928327")
        )

        self.m_cylinderEntity = Qt3DCore.QEntity(self.m_rootEntity)
        self.m_cylinderEntity.addComponent(self.cylinder)
        self.m_cylinderEntity.addComponent(self.cylinderMaterial)
        self.m_cylinderEntity.addComponent(self.cylinderTransform)

        self.cuboid = Qt3DExtras.QCuboidMesh()

        self.cuboidTransform = Qt3DCore.QTransform(
            scale=4.0, translation=QtGui.QVector3D(5.0, -4.0, 0.0),
        )

        self.cuboidMaterial = Qt3DExtras.QPhongMaterial(diffuse=QtGui.QColor("#665423"))

        self.m_cuboidEntity = Qt3DCore.QEntity(self.m_rootEntity)
        self.m_cuboidEntity.addComponent(self.cuboid)
        self.m_cuboidEntity.addComponent(self.cuboidMaterial)
        self.m_cuboidEntity.addComponent(self.cuboidTransform)

        self.planeMesh = Qt3DExtras.QPlaneMesh(width=2, height=2)

        self.planeTransform = Qt3DCore.QTransform(
            scale=1.3,
            rotation=QtGui.QQuaternion.fromAxisAndAngle(
                QtGui.QVector3D(1.0, 0.0, 0.0), 45.0
            ),
            translation=QtGui.QVector3D(0.0, -4.0, 0.0),
        )

        self.planeMaterial = Qt3DExtras.QPhongMaterial(diffuse=QtGui.QColor("#a69929"))

        self.m_planeEntity = Qt3DCore.QEntity(self.m_rootEntity)
        self.m_planeEntity.addComponent(self.planeMesh)
        self.m_planeEntity.addComponent(self.planeMaterial)
        self.m_planeEntity.addComponent(self.planeTransform)

        self.sphereMesh = Qt3DExtras.QSphereMesh(rings=20, slices=20, radius=2)

        self.sphereTransform = Qt3DCore.QTransform(
            scale=1.3, translation=QtGui.QVector3D(-5.0, -4.0, 0.0),
        )

        self.sphereMaterial = Qt3DExtras.QPhongMaterial(diffuse=QtGui.QColor("#a69929"))

        self.m_sphereEntity = Qt3DCore.QEntity(self.m_rootEntity)
        self.m_sphereEntity.addComponent(self.sphereMesh)
        self.m_sphereEntity.addComponent(self.sphereMaterial)
        self.m_sphereEntity.addComponent(self.sphereTransform)

    @QtCore.Slot(bool)
    def enableTorus(self, enabled):
        self.m_torusEntity.setEnabled(enabled)

    @QtCore.Slot(bool)
    def enableCone(self, enabled):
        self.m_coneEntity.setEnabled(enabled)

    @QtCore.Slot(bool)
    def enableCylinder(self, enabled):
        self.m_cylinderEntity.setEnabled(enabled)

    @QtCore.Slot(bool)
    def enableCuboid(self, enabled):
        self.m_cuboidEntity.setEnabled(enabled)

    @QtCore.Slot(bool)
    def enablePlane(self, enabled):
        self.m_planeEntity.setEnabled(enabled)

    @QtCore.Slot(bool)
    def enableSphere(self, enabled):
        self.m_sphereEntity.setEnabled(enabled)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    view = Qt3DExtras.Qt3DWindow()
    view.defaultFrameGraph().setClearColor(QtGui.QColor("#4d4d4f"))
    container = QtWidgets.QWidget.createWindowContainer(view)
    screenSize = view.screen().size()
    container.setMinimumSize(QtCore.QSize(200, 100))
    container.setMaximumSize(screenSize)

    widget = QtWidgets.QWidget()
    hLayout = QtWidgets.QHBoxLayout(widget)
    vLayout = QtWidgets.QVBoxLayout()
    vLayout.setAlignment(QtCore.Qt.AlignTop)
    hLayout.addWidget(container, 1)
    hLayout.addLayout(vLayout)

    widget.setWindowTitle("Basic shapes")

    input_ = Qt3DInput.QInputAspect()
    view.registerAspect(input_)

    rootEntity = Qt3DCore.QEntity()

    cameraEntity = view.camera()

    cameraEntity.lens().setPerspectiveProjection(45.0, 16.0 / 9.0, 0.1, 1000.0)
    cameraEntity.setPosition(QtGui.QVector3D(0, 0, 20.0))
    cameraEntity.setUpVector(QtGui.QVector3D(0, 1, 0))
    cameraEntity.setViewCenter(QtGui.QVector3D(0, 0, 0))

    lightEntity = Qt3DCore.QEntity(rootEntity)
    light = Qt3DRender.QPointLight(lightEntity)
    light.setColor("white")
    light.setIntensity(1)
    lightEntity.addComponent(light)

    lightTransform = Qt3DCore.QTransform(lightEntity)
    lightTransform.setTranslation(cameraEntity.position())
    lightEntity.addComponent(lightTransform)

    camController = Qt3DExtras.QFirstPersonCameraController(rootEntity)
    camController.setCamera(cameraEntity)

    modifier = SceneModifier(rootEntity)

    view.setRootEntity(rootEntity)

    info = QtWidgets.QCommandLinkButton()
    info.setText("Qt3D ready-made meshes")
    info.setDescription(
        "Qt3D provides several ready-made meshes, like torus, cylinder, cone, cube, plane and sphere."
    )
    info.setIconSize(QtCore.QSize(0, 0))

    torusCB = QtWidgets.QCheckBox(widget)
    torusCB.setChecked(True)
    torusCB.setText("Torus")

    coneCB = QtWidgets.QCheckBox(widget)
    coneCB.setChecked(True)
    coneCB.setText("Cone")

    cylinderCB = QtWidgets.QCheckBox(widget)
    cylinderCB.setChecked(True)
    cylinderCB.setText("Cylinder")

    cuboidCB = QtWidgets.QCheckBox(widget)
    cuboidCB.setChecked(True)
    cuboidCB.setText("Cuboid")

    planeCB = QtWidgets.QCheckBox(widget)
    planeCB.setChecked(True)
    planeCB.setText("Plane")

    sphereCB = QtWidgets.QCheckBox(widget)
    sphereCB.setChecked(True)
    sphereCB.setText("Sphere")

    vLayout.addWidget(info)
    vLayout.addWidget(torusCB)
    vLayout.addWidget(coneCB)
    vLayout.addWidget(cylinderCB)
    vLayout.addWidget(cuboidCB)
    vLayout.addWidget(planeCB)
    vLayout.addWidget(sphereCB)

    torusCB.stateChanged.connect(modifier.enableTorus)
    coneCB.stateChanged.connect(modifier.enableCone)
    cylinderCB.stateChanged.connect(modifier.enableCylinder)
    cuboidCB.stateChanged.connect(modifier.enableCuboid)
    planeCB.stateChanged.connect(modifier.enablePlane)
    sphereCB.stateChanged.connect(modifier.enableSphere)

    torusCB.setChecked(True)
    coneCB.setChecked(True)
    cylinderCB.setChecked(True)
    cuboidCB.setChecked(True)
    planeCB.setChecked(True)
    sphereCB.setChecked(True)

    widget.show()
    widget.resize(1200, 800)

    sys.exit(app.exec_())

enter image description here

like image 112
eyllanesc Avatar answered Oct 14 '22 05:10

eyllanesc