PySide2 Qt3D mesh does not show up

2024/11/16 1:45:04

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 Qt3DRenderclass SceneModifier(QtCore.QObject):def __init__(self, root_entity=None):super().__init__()# Scene entityself._m_root_entity = Qt3DCore.QEntity(root_entity)# Torus shape dataself.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 transformtorus_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 materialtorus_mat = Qt3DExtras.QPhongMaterial()torus_mat.setDiffuse(QtGui.QColor(255, 102, 0))# Torus meshself.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_())
Answer

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 Qt3DInputclass SceneModifier(QtCore.QObject):def __init__(self, root_entity=None):super().__init__()self.m_rootEntity = root_entityself.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

https://en.xdnf.cn/q/71402.html

Related Q&A

Unable to import module lambda_function: No module named psycopg2._psycopg aws lambda function

I have installed the psycopg2 with this command in my package folder : pip install --target ./package psycopg2 # Or pip install -t ./package psycopg2now psycopg2 module is in my package and I have crea…

RestrictedPython: Call other functions within user-specified code?

Using Yuri Nudelmans code with the custom _import definition to specify modules to restrict serves as a good base but when calling functions within said user_code naturally due to having to whitelist e…

TypeError: object of type numpy.int64 has no len()

I am making a DataLoader from DataSet in PyTorch. Start from loading the DataFrame with all dtype as an np.float64result = pd.read_csv(dummy.csv, header=0, dtype=DTYPE_CLEANED_DF)Here is my dataset cla…

VS Code Pylance not highlighting variables and modules

Im using VS Code with the Python and Pylance extensions. Im having a problem with the Pylance extension not doing syntax highlight for things like modules and my dataframe. I would expect the modules…

How to compute Spearman correlation in Tensorflow

ProblemI need to compute the Pearson and Spearman correlations, and use it as metrics in tensorflow.For Pearson, its trivial :tf.contrib.metrics.streaming_pearson_correlation(y_pred, y_true)But for Spe…

Pytorch loss is nan

Im trying to write my first neural network with pytorch. Unfortunately, I encounter a problem when I want to get the loss. The following error message: RuntimeError: Function LogSoftmaxBackward0 return…

How do you debug python code with kubernetes and skaffold?

I am currently running a django app under python3 through kubernetes by going through skaffold dev. I have hot reload working with the Python source code. Is it currently possible to do interactive deb…

Discrepancies between R optim vs Scipy optimize: Nelder-Mead

I wrote a script that I believe should produce the same results in Python and R, but they are producing very different answers. Each attempts to fit a model to simulated data by minimizing deviance usi…

C++ class not recognized by Python 3 as a module via Boost.Python Embedding

The following example from Boost.Python v1.56 shows how to embed the Python 3.4.2 interpreter into your own application. Unfortunately that example does not work out of the box on my configuration with…

Python NET call C# method which has a return value and an out parameter

Im having the following static C# methodpublic static bool TryParse (string s, out double result)which I would like to call from Python using the Python NET package.import clr from System import Double…