PySide2 Qt3D mesh does not show up - python

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_())

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_())

Related

Why is the second QWidget in a layout disappearing? [duplicate]

I am trying to use PyQt5 to show two widgets, the first one is a plot of sin, cos and tan function. I am using the pyqtgraph and used the code that was found in the answer of this question. I am also using another widget that draws a cube using PyOpenGL, by taking the example found in this link. I am trying to show this two widgets in one main widget, which is the main window. My approach is the following
Take a main widget.
In the main widget, use a QVBoxLayout()
In the QVBoxLayout, at two widgets mentioned above
But when I am running the code, only the plot that is using the pyqtgraph is shown but not the cube that is drawn using PyOpenGL. After a little bit debugging, I was able to find out that the height of the cube widget is setting to 0 by default. I am not sure why this is hapenning. I tried calling glWidget.resize(640,480). But it didn't work. I am new on working with PyQt and PyOpenGL. I think I am missing some details that will allow the height of the glWidget to be greater than 0, if my assumption is correct. Also I am not sure if this is actually possible to do. My current code is given below, it is a little bit messy.
import sys
from OpenGL.GL.images import asWrapper
from PyQt5.QtWidgets import QApplication, QGridLayout
from PyQt5 import QtWidgets
import pyqtgraph as pg
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt5 import QtGui
from PyQt5.QtOpenGL import *
from PyQt5 import QtCore, QtWidgets
import pyqtgraph as pg
import numpy as np
from PyQt5 import QtOpenGL
import OpenGL.GL as gl
from OpenGL import GLU
from OpenGL.arrays import vbo
class TimeLine(QtCore.QObject):
frameChanged = QtCore.pyqtSignal(int)
def __init__(self, interval=60, loopCount=1, parent=None):
super(TimeLine, self).__init__(parent)
self._startFrame = 0
self._endFrame = 0
self._loopCount = loopCount
self._timer = QtCore.QTimer(self, timeout=self.on_timeout)
self._counter = 0
self._loop_counter = 0
self.setInterval(interval)
def on_timeout(self):
if self._startFrame <= self._counter < self._endFrame:
self.frameChanged.emit(self._counter)
self._counter += 1
else:
self._counter = 0
self._loop_counter += 1
if self._loopCount > 0:
if self._loop_counter >= self.loopCount():
self._timer.stop()
def setLoopCount(self, loopCount):
self._loopCount = loopCount
def loopCount(self):
return self._loopCounts
interval = QtCore.pyqtProperty(int, fget=loopCount, fset=setLoopCount)
def setInterval(self, interval):
self._timer.setInterval(interval)
def interval(self):
return self._timer.interval()
interval = QtCore.pyqtProperty(int, fget=interval, fset=setInterval)
def setFrameRange(self, startFrame, endFrame):
self._startFrame = startFrame
self._endFrame = endFrame
#QtCore.pyqtSlot()
def start(self):
self._counter = 0
self._loop_counter = 0
self._timer.start()
class GLWidget(QtOpenGL.QGLWidget):
def __init__(self, parent = None):
self.parent = parent
QtOpenGL.QGLWidget.__init__(self, parent)
self.resizeGL(640,800)
def initializeGL(self):
self.qglClearColor(QtGui.QColor(0,0,255))
gl.glEnable(gl.GL_DEPTH_TEST)
self.initGeometry()
self.rotX = 0.0
self.rotY = 0.0
self.rotZ = 0.0
def resizeGL(self, width, height):
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
print(width, height)
aspect = width / float(height)
GLU.gluPerspective(45.0, aspect, 1.0, 100.0)
gl.glMatrixMode(gl.GL_MODELVIEW)
def paintGL(self):
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glPushMatrix()
gl.glTranslate(0.0, 0.0, -50.0)
gl.glScale(20.0, 20.0, 20.0)
gl.glRotate(self.rotX, 1.0, 0.0, 0.0)
gl.glRotate(self.rotY, 0.0, 1.0, 0.0)
gl.glRotate(self.rotZ, 0.0, 0.0, 1.0)
gl.glTranslate(-0.5, -0.5, -0.5)
gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
gl.glEnableClientState(gl.GL_COLOR_ARRAY)
gl.glVertexPointer(3, gl.GL_FLOAT, 0, self.vertVBO)
gl.glColorPointer(3, gl.GL_FLOAT, 0, self.colorVBO)
gl.glDrawElements(gl.GL_QUADS, len(self.cubeIdxArray), gl.GL_UNSIGNED_INT, self.cubeIdxArray)
gl.glDisableClientState(gl.GL_VERTEX_ARRAY)
gl.glDisableClientState(gl.GL_COLOR_ARRAY)
gl.glPopMatrix()
def initGeometry(self):
self.cubeVtxArray = np.array(
[[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
[0.0, 1.0, 1.0]])
self.vertVBO = vbo.VBO(np.reshape(self.cubeVtxArray,
(1, -1)).astype(np.float32))
self.vertVBO.bind()
self.cubeClrArray = np.array(
[[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
[0.0, 1.0, 1.0 ]])
self.colorVBO = vbo.VBO(np.reshape(self.cubeClrArray,
(1, -1)).astype(np.float32))
self.colorVBO.bind()
self.cubeIdxArray = np.array(
[0, 1, 2, 3,
3, 2, 6, 7,
1, 0, 4, 5,
2, 1, 5, 6,
0, 3, 7, 4,
7, 6, 5, 4 ])
def setRotX(self, val):
self.rotX = np.pi * val
def setRotY(self, val):
self.rotY = np.pi * val
def setRotZ(self, val):
self.rotZ = np.pi * val
class MainGui(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.resize(600,600)
self.cube = GLWidget(self)
self.setupUI()
def setupUI(self):
central_widget = QtWidgets.QWidget()
central_layout = QtWidgets.QVBoxLayout()
central_widget.setLayout(central_layout)
self.setCentralWidget(central_widget)
pg.setConfigOption('background',0.95)
pg.setConfigOptions(antialias=True)
self.plot = pg.PlotWidget()
self.plot.setAspectLocked(lock = True, ratio = 0.01)
#self.cube = GLWidget(self)
#self.cube.resize(200,200)
central_layout.addWidget(self.cube)
central_layout.addWidget(self.plot)
self._plots = [self.plot.plot([], [], pen=pg.mkPen(color=color, width=2)) for color in ('g', 'r', 'y')]
self._timeline = TimeLine(loopCount = 0, interval = 10)
self._timeline.setFrameRange(0,720)
self._timeline.frameChanged.connect(self.generate_data)
self._timeline.start()
def plot_data(self, data):
for plt, val in zip(self._plots, data):
plt.setData(range(len(val)),val)
#QtCore.pyqtSlot(int)
def generate_data(self, i):
ang = np.arange(i, i + 720)
cos_func = np.cos(np.radians(ang))
sin_func = np.sin(np.radians(ang))
tan_func = sin_func/cos_func
tan_func[(tan_func < -3) | (tan_func > 3)] = np.NaN
self.plot_data([sin_func, cos_func, tan_func])
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
gui = MainGui()
gui.show()
sys.exit(app.exec_())
It seems that QGLWidget (which, by the way, is deprecated, and QOpenGLWidget should be used instead) doesn't implement sizeHint(), so it returns an invalid size (QSize(-1, -1)), which means that the widget can be possibly resized to a 0 width and/or height.
Since the plot widget has an expanding size policy (and dynamically reimplements sizeHint()) the result is that the gl widget is completely hidden, having 0 height.
A possible solution is to add the widgets with a proper stretch argument to the layout.
If you want both widgets to have the same height, you can do the following:
central_layout.addWidget(self.cube, stretch=1)
central_layout.addWidget(self.plot, stretch=1)
Note that the stretch is ratio-based (only integers are considered), so, if you want the cube have half the height of the plot:
central_layout.addWidget(self.cube, stretch=1)
central_layout.addWidget(self.plot, stretch=2)
Alternatively, you can use setMinimumHeight() for the gl widget, but since the plot has an expanding policy (which normally takes precedence), that gl widget will always have that height. A better solution would be to set an expanding policy for the gl widget and implement QSizeHint, but remember that the plot widget has a dynamic size hint, so it will always take some amount of "size priority".
class GLWidget(QtOpenGL.QGLWidget):
def __init__(self, parent = None):
QtOpenGL.QGLWidget.__init__(self, parent)
self.setSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
def sizeHint(self):
return QtCore.QSize(300, 150)
# ...
There should be no need to manually call resizeGL() in the __init__, and you should also always use the dynamic access parent() function to get the parent.

Point Cloud Distortion when Drawing with modern OpenGL

I found this nice tutorial of drawing and rotating a cube with PyQt and modern OpenGL. My objective was to adapt the script for point clouds, by doing the following (see also code below):
Load point cloud using Open3D and extract coordinates & colors as numpy arrays
Create Vertex Buffer Objects (VBOs) from the arrays
Change the drawing function to gl.glDrawElements(gl.GL_POINTS, ...)
Unfortunately then the point cloud is very distorted and thin (see screenshot). It should actually be a room with chairs and walls.
Do you see if I made a mistake with the VBOs or drawing? Or is there a better way of loading a point cloud?
I tested the example with the old fixed pipeline (glBegin(GL_POINTS) ... glEnd()) and there the point cloud is correctly drawn (but also the performance really bad!).
from PyQt5 import QtCore # core Qt functionality
from PyQt5 import QtGui # extends QtCore with GUI functionality
from PyQt5 import QtOpenGL # provides QGLWidget, a special OpenGL QWidget
from PyQt5 import QtWidgets
import OpenGL.GL as gl # python wrapping of OpenGL
from OpenGL import GLU # OpenGL Utility Library, extends OpenGL functionality
from OpenGL.arrays import vbo
import numpy as np
import open3d as o3d
import sys
# Loading the point cloud from file
def load_pointcloud():
pcd = o3d.io.read_point_cloud("../pointclouds/0004.ply")
print(pcd)
print("Pointcloud Center: " + str(pcd.get_center()))
points = np.asarray(pcd.points)
colors = np.asarray(pcd.colors)
return points, colors
#### here was only the GUI code (slider, ...) , which works fine! ####
class GLWidget(QtOpenGL.QGLWidget):
def __init__(self, parent=None):
self.parent = parent
QtOpenGL.QGLWidget.__init__(self, parent)
def initializeGL(self):
self.qglClearColor(QtGui.QColor(250, 250, 250)) # initialize the screen to blue
gl.glEnable(gl.GL_DEPTH_TEST) # enable depth testing
self.initGeometryPC()
self.rotX = 0.0
self.rotY = 0.0
self.rotZ = 0.0
def setRotX(self, val):
self.rotX = val
def setRotY(self, val):
self.rotY = val
def setRotZ(self, val):
self.rotZ = val
def resizeGL(self, width, height):
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
aspect = width / float(height)
#GLU.gluPerspective(45.0, aspect, 1.0, 100.0) #GLU.gluPerspective(45.0, aspect, 1.0, 100.0)
gl.glOrtho(-2.0, 2.0, -2.0, 2.0, 1.0, 100.0)
gl.glMatrixMode(gl.GL_MODELVIEW)
def paintGL(self):
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glPushMatrix() # push the current matrix to the current stack
gl.glTranslate(0.0, 0.0, -5.0) # third, translate cube to specified depth
#gl.glScale(.5, .5, .5) # second, scale point cloud
gl.glRotate(self.rotX, 1.0, 0.0, 0.0)
gl.glRotate(self.rotY, 0.0, 1.0, 0.0)
gl.glRotate(self.rotZ, 0.0, 0.0, 1.0)
gl.glTranslate(-0.5, -0.5, -0.5) # first, translate point cloud center to origin
gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
gl.glEnableClientState(gl.GL_COLOR_ARRAY)
gl.glVertexPointer(3, gl.GL_FLOAT, 0, self.vertVBO)
gl.glColorPointer(3, gl.GL_FLOAT, 0, self.colorVBO)
gl.glPointSize(2)
gl.glDrawElements(gl.GL_POINTS, len(self.pointsIdxArray), gl.GL_UNSIGNED_INT, self.pointsIdxArray)
gl.glDisableClientState(gl.GL_VERTEX_ARRAY)
gl.glDisableClientState(gl.GL_COLOR_ARRAY)
gl.glPopMatrix() # restore the previous modelview matrix
# Push geometric data to GPU
def initGeometryPC(self):
points, colors = load_pointcloud()
self.pointsVtxArray = points
self.vertVBO = vbo.VBO(np.reshape(self.pointsVtxArray, (1, -1)).astype(np.float32))
self.vertVBO.bind()
self.pointsClrArray = colors
self.colorVBO = vbo.VBO(np.reshape(self.pointsClrArray, (1, -1)).astype(np.float32))
self.colorVBO.bind()
self.pointsIdxArray = np.arange(len(points))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
After a long search I came upon this stackoverflow-post. I adapted my code to that answer by storing point coordinates and colors together in one vbo-object (gl.glGenBuffers(1)). Then I define the vertex and color pointer with the specific stride and offset:
gl.glVertexPointer(3, gl.GL_FLOAT, 6*4, None)
Stride= 24 bytes: [x, y, z, r, g, b] * sizeof(float)
gl.glColorPointer(3, gl.GL_FLOAT, 6*4, ctypes.c_void_p(3*4))
Offset= 12 bytes: the rgb color starts after the 3 coordinates x, y, z
And finally I use gl.glDrawArrays(gl.GL_POINTS, 0, noOfVertices) for drawing the point cloud.
The full code can be seen below (marked with ### NEW ### comments):
from PyQt5 import QtCore # core Qt functionality
from PyQt5 import QtGui # extends QtCore with GUI functionality
from PyQt5 import QtOpenGL # provides QGLWidget, a special OpenGL QWidget
from PyQt5 import QtWidgets
import OpenGL.GL as gl # python wrapping of OpenGL
from OpenGL import GLU # OpenGL Utility Library, extends OpenGL functionality
from OpenGL.arrays import vbo
import numpy as np
import open3d as o3d
import ctypes
import sys # we'll need this later to run our Qt application
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self) # call the init for the parent class
self.resize(300, 300)
self.setWindowTitle('Hello OpenGL App')
self.glWidget = GLWidget(self)
self.initGUI()
timer = QtCore.QTimer(self)
timer.setInterval(20) # period, in milliseconds
timer.timeout.connect(self.glWidget.updateGL)
timer.start()
def initGUI(self):
central_widget = QtWidgets.QWidget()
gui_layout = QtWidgets.QVBoxLayout()
central_widget.setLayout(gui_layout)
self.setCentralWidget(central_widget)
gui_layout.addWidget(self.glWidget)
sliderX = QtWidgets.QSlider(QtCore.Qt.Horizontal)
sliderX.valueChanged.connect(lambda val: self.glWidget.setRotX(val))
sliderY = QtWidgets.QSlider(QtCore.Qt.Horizontal)
sliderY.valueChanged.connect(lambda val: self.glWidget.setRotY(val))
sliderZ = QtWidgets.QSlider(QtCore.Qt.Horizontal)
sliderZ.valueChanged.connect(lambda val: self.glWidget.setRotZ(val))
gui_layout.addWidget(sliderX)
gui_layout.addWidget(sliderY)
gui_layout.addWidget(sliderZ)
class GLWidget(QtOpenGL.QGLWidget):
def __init__(self, parent=None):
self.parent = parent
QtOpenGL.QGLWidget.__init__(self, parent)
def initializeGL(self):
self.qglClearColor(QtGui.QColor(100, 100, 100)) # initialize the screen to blue
gl.glEnable(gl.GL_DEPTH_TEST) # enable depth testing
self.initGeometry()
self.rotX = 0.0
self.rotY = 0.0
self.rotZ = 0.0
def setRotX(self, val):
self.rotX = val
def setRotY(self, val):
self.rotY = val
def setRotZ(self, val):
self.rotZ = val
def resizeGL(self, width, height):
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
aspect = width / float(height)
GLU.gluPerspective(45.0, aspect, 1.0, 100.0)
gl.glMatrixMode(gl.GL_MODELVIEW)
def paintGL(self):
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glPushMatrix() # push the current matrix to the current stack
gl.glTranslate(0.0, 0.0, -3.0) # third, translate cube to specified depth
#gl.glScale(20.0, 20.0, 20.0) # second, scale cube
gl.glRotate(self.rotX, 1.0, 0.0, 0.0)
gl.glRotate(self.rotY, 0.0, 1.0, 0.0)
gl.glRotate(self.rotZ, 0.0, 0.0, 1.0)
gl.glTranslate(-0.5, -0.5, -0.5) # first, translate cube center to origin
# Point size
gl.glPointSize(3)
### NEW ###
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vbo)
stride = 6*4 # (24 bates) : [x, y, z, r, g, b] * sizeof(float)
gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
gl.glVertexPointer(3, gl.GL_FLOAT, stride, None)
gl.glEnableClientState(gl.GL_COLOR_ARRAY)
offset = 3*4 # (12 bytes) : the rgb color starts after the 3 coordinates x, y, z
gl.glColorPointer(3, gl.GL_FLOAT, stride, ctypes.c_void_p(offset))
noOfVertices = self.noPoints
gl.glDrawArrays(gl.GL_POINTS, 0, noOfVertices)
gl.glDisableClientState(gl.GL_VERTEX_ARRAY)
gl.glDisableClientState(gl.GL_COLOR_ARRAY)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0)
### NEW ###
gl.glPopMatrix() # restore the previous modelview matrix
def initGeometry(self):
vArray = self.LoadVertices()
self.noPoints = len(vArray) // 6
print("No. of Points: %s" % self.noPoints)
self.vbo = self.CreateBuffer(vArray)
### NEW ###
def LoadVertices(self):
pcd = o3d.io.read_point_cloud("../pointclouds/0004.ply")
print(pcd)
print("Pointcloud Center: " + str(pcd.get_center()))
points = np.asarray(pcd.points).astype('float32')
colors = np.asarray(pcd.colors).astype('float32')
attributes = np.concatenate((points, colors),axis=1)
print("Attributes shape: " + str(attributes.shape))
return attributes.flatten()
def CreateBuffer(self, attributes):
bufferdata = (ctypes.c_float*len(attributes))(*attributes) # float buffer
buffersize = len(attributes)*4 # buffer size in bytes
vbo = gl.glGenBuffers(1)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vbo)
gl.glBufferData(gl.GL_ARRAY_BUFFER, buffersize, bufferdata, gl.GL_STATIC_DRAW)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0)
return vbo
### NEW ###
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
However, I still did not find the correct parameters for initial approach above with two separate VBOs for coordinate and color. So I am happy for further comments.

How do I set up PyOpenGL with PyQt5?

I am wanting to set up several 3D mathematical projects with python. The best way I can see to render these is with PyOpenGL. I also want to run it in PyQt5, so that I can have GUI’s along side the render. All the information I can find is either using PyGame or QtDesigner. I would like to work without QtDesigner. Does anyone know where I could find a tutorial on how to set this up?
EDIT:
I managed to get some web scrounging done. I found the following code at https://pythonprogramming.net/community/37/Cube%20rotation%20with%20pyopengl%20and%20pyqt/ where the author asks for help regarding it not running. he says the following about it:
I'm very new to python. I have a problem with my code, it's a very simple rotating cube. I don't have any problem with this cube code with pygame screen but when I use it with pyqt (or Qt designer widgets), it runs but it shows nothing!!!
I copied his code into my IDe, then saved it as cubes.py. I opened up a CMD instance in thae directory of the file and called it. It opened a tiny black Qt window in the middle of the screen:
When I try to resize the window by dragging the corner, it throws a deep traceback:
File "C:\Users\aweso\Documents\Python\cubes\cubes.py", line 1, in <module>
from OpenGL.GL import *
ModuleNotFoundError: No module named 'OpenGL'
C:\Users\aweso\Documents\Python\cubes>cubes.py
Traceback (most recent call last):
File "C:\Users\aweso\Documents\Python\cubes\cubes.py", line 56, in paintGL
glEnd()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\OpenGL\latebind.py", line 63, in __call__
return self.wrapperFunction( self.baseFunction, *args, **named )
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\OpenGL\GL\exceptional.py", line 45, in glEnd
return baseFunction( )
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\OpenGL\platform\baseplatform.py", line 415, in __call__
return self( *args, **named )
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\OpenGL\error.py", line 234, in glCheckError
baseOperation = baseOperation,
OpenGL.error.GLError: GLError(
err = 1280,
description = b'invalid enumerant',
baseOperation = glEnd,
cArguments = ()
)
Here is his code, unmodified:
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL import *
from PyQt5.QtOpenGL import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
import sys,time
class MainWindow(QGLWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.widget = glWidget(self)
mainLayout = QVBoxLayout()
mainLayout.addWidget(self.widget)
self.setLayout(mainLayout)
class glWidget(QGLWidget):
def __init__(self, parent):
QGLWidget.__init__(self, parent)
#self.setMinimumSize(400, 400)
self.verticies = (
(1,-1,-1),
(1,1,-1),
(-1,1,-1),
(-1,-1,-1),
(1,-1,1),
(1,1,1),
(-1,-1,1),
(-1,1,1))
self.edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7))
def paintGL(self):
while True:
#glRotatef(1,3,1,1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glBegin(GL_LINE)
for self.edge in self.edges:
for self.vertex in self.edge:
glVertex3fv(self.verticies[self.vertex])
glEnd()
glFlush()
time.sleep(1)
def resizeGL(self, w, h):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-50, 50, -50, 50, -50.0, 50.0)
glViewport(0, 0, w, h)
def initializeGL(self):
#glClearColor(0.0, 0.0, 0.0, 1.0)
gluPerspective(45,800/600,0.1,50.0)
glTranslatef(0.0,0.0,-5)
glRotatef(0,0,0,0)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
I once developed an interactive 3D program using PyOpenGL and PyQt5.
Here's the spike code of that time.
I wish this could be helpful to you.
import sys
import math
from array import array
from OpenGL import GL
from PyQt5.QtCore import pyqtSignal, QPoint, QSize, Qt
from PyQt5.QtGui import QColor, QImage
from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QWidget)
from PyQt5.QtOpenGL import QGLWidget
from shader import ShaderProgram
class Window (QWidget):
def __init__(self):
super(Window, self).__init__()
self.glWidget = GLWidget()
mainLayout = QHBoxLayout()
mainLayout.addWidget(self.glWidget)
self.setLayout(mainLayout)
self.setWindowTitle('Hello')
class GLWidget (QGLWidget):
def __init__(self, parent=None):
super(GLWidget, self).__init__(parent)
def sizeHint(self):
return QSize(1200, 1000)
def initializeGL(self):
GL.glClearColor(0.0, 0.0, 1.0, 0.0) # it's also possible.
GL.glEnable(GL.GL_DEPTH_TEST)
# GL.glEnable(GL.GL_VERTEX_ARRAY)
self._createVertexBuffer()
self.program = ShaderProgram('hello.vert', 'hello.frag')
self.program.use()
self.frontTexture = self._createTexture('tex.png')
self.backTexture = self._createTexture('back-tex.jpg')
# set texture units
GL.glActiveTexture(GL.GL_TEXTURE0)
GL.glBindTexture(GL.GL_TEXTURE_2D, self.frontTexture)
GL.glUniform1i(GL.glGetUniformLocation(self.program.getProgram(), b'frontTexture'), 0)
GL.glActiveTexture(GL.GL_TEXTURE1)
GL.glBindTexture(GL.GL_TEXTURE_2D, self.backTexture)
GL.glUniform1i(GL.glGetUniformLocation(self.program.getProgram(), b'backTexture'), 1)
def paintGL(self):
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
self._draw()
def resizeGL(self, width, height):
side = min(width, height)
if side < 0:
return
GL.glViewport((width - side) // 2, (height - side) // 2, side, side)
def _createTexture(self, texFilePath):
qImage = QImage(texFilePath)
texture = QGLWidget.bindTexture(self, qImage, GL.GL_TEXTURE_2D, GL.GL_RGBA)
GL.glGenerateMipmap(GL.GL_TEXTURE_2D)
return texture
def _createVertexBuffer(self):
vertices = array('f', [-1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0, -1.0, 1.0, 0.0]).tobytes()
colors = array('f', [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0]).tobytes()
indices = [0, 1, 2, 0, 3, 2]
texCoords = array('f', [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]).tobytes()
self.vertices = vertices
self.colors = colors
self.indices = indices
self.texCoords = texCoords
def _draw(self):
GL.glEnableVertexAttribArray(0)
GL.glEnableVertexAttribArray(1)
GL.glEnableVertexAttribArray(2)
GL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, self.vertices)
GL.glVertexAttribPointer(1, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, self.colors)
GL.glVertexAttribPointer(2, 2, GL.GL_FLOAT, GL.GL_FALSE, 0, self.texCoords)
GL.glDrawElements(GL.GL_TRIANGLES, 6, GL.GL_UNSIGNED_INT, self.indices)
GL.glDisableVertexAttribArray(0)
GL.glDisableVertexAttribArray(1)
GL.glDisableVertexAttribArray(2)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

How to select point in a line using vtk?

In VTK, I have a surface and a line, and the line is included by the surface. Then, I need to pick one point in the line. I implement myself interactor and get the world coordinate by a right button click. I hope the select point could be located in the line. I show the selected line in the renderer when right button release. However, I find I can not select a point in the line. My code is:
import vtk, os, sys
import numpy as np
from PyQt5.QtWidgets import *
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk
def numpyToVtk(data, type=vtk.VTK_FLOAT):
flat_data_array = data.transpose(2,1,0).flatten()
vtk_data_array = numpy_to_vtk(flat_data_array)
vtk_data = numpy_to_vtk(num_array=vtk_data_array, deep=True, array_type=type)
img = vtk.vtkImageData()
img.GetPointData().SetScalars(vtk_data)
img.SetDimensions(data.shape)
img.SetOrigin(0, 0, 0)
img.SetSpacing(1, 1, 1)
return img
class ourInteractor(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self, renderer=None, renWindow=None):
super(ourInteractor, self).__init__()
self.AddObserver("RightButtonReleaseEvent", self.OnRightButtonUp)
self.ren = renderer
self.renWin = renWindow
def OnRightButtonUp(self, obj, event):
super(ourInteractor, self).OnRightButtonUp()
pos = self.GetInteractor().GetEventPosition()
coordinate = vtk.vtkCoordinate()
coordinate.SetCoordinateSystemToDisplay()
coordinate.SetValue(pos[0], pos[1], 0)
worldCoor = coordinate.GetComputedWorldValue(
self.GetInteractor().GetRenderWindow().GetRenderers().GetFirstRenderer())
print('screen coor: ', pos, 'world coor: ', worldCoor)
points = vtk.vtkPoints()
vertices = vtk.vtkCellArray()
id = points.InsertNextPoint(worldCoor[0], worldCoor[1], worldCoor[2])
vertices.InsertNextCell(1)
vertices.InsertCellPoint(id)
point = vtk.vtkPolyData()
point.SetPoints(points)
point.SetVerts(vertices)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(point)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetPointSize(10)
actor.GetProperty().SetColor(0, 1, 0)
self.ren.AddActor(actor)
self.renWin.Render()
class AirwaySkeleton(QMainWindow):
def __init__(self, parent=None):
super(AirwaySkeleton, self).__init__(parent=parent)
self.setWindowTitle("Airway Skeleton")
widget = QWidget()
self.setCentralWidget(widget)
layout = QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
widget.setLayout(layout)
self.mainLayout = layout
frame = QFrame()
vtkWidget = QVTKRenderWindowInteractor(frame)
self.mainLayout.addWidget(vtkWidget)
ren = vtk.vtkRenderer()
vtkWidget.GetRenderWindow().AddRenderer(ren)
iren = vtkWidget.GetRenderWindow().GetInteractor()
style = ourInteractor(renderer=ren, renWindow=vtkWidget.GetRenderWindow())
iren.SetInteractorStyle(style)
ren.SetBackground(0, 0, 0)
self.ren = ren
mask = np.zeros(shape=[200, 200, 200], dtype=np.uint8)
mask[20:80, 50:150, 50:150] = 1
mask[80:150, 80:120, 80:120] = 1
mask[150:170, 50:150, 50:150] = 1
xs = np.arange(20, 170, 0.1)
line = []
for x in xs:
line.append([x, 100, 100])
actors = self.createActorsForLines([np.array(line)])
vtkMask = numpyToVtk(data=mask, type=vtk.VTK_CHAR)
mesh = self.maskToMesh(vtkMask)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(mesh.GetOutputPort())
mapper.ScalarVisibilityOff()
actor = vtk.vtkLODActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(1, 1, 1)
actor.GetProperty().SetOpacity(0.4)
self.ren.AddActor(actor)
for lineActor in actors:
self.ren.AddActor(lineActor)
self.renWin = vtkWidget.GetRenderWindow()
iren.Initialize()
self.iren = iren
def maskToMesh(self, mask):
contour = vtk.vtkDiscreteMarchingCubes()
contour.SetInputData(mask)
contour.SetValue(0, 1)
contour.Update()
smoother = vtk.vtkWindowedSincPolyDataFilter()
smoother.SetInputConnection(contour.GetOutputPort())
smoother.SetNumberOfIterations(30)
smoother.BoundarySmoothingOff()
smoother.NonManifoldSmoothingOn()
smoother.NormalizeCoordinatesOn()
smoother.Update()
triangleCellNormals = vtk.vtkPolyDataNormals()
triangleCellNormals.SetInputConnection(smoother.GetOutputPort())
triangleCellNormals.ComputeCellNormalsOn()
triangleCellNormals.ComputePointNormalsOff()
triangleCellNormals.ConsistencyOn()
triangleCellNormals.AutoOrientNormalsOn()
triangleCellNormals.Update()
return triangleCellNormals
def createActorsForLines(self, lines):
actors = []
endPoints = vtk.vtkPoints()
for line in lines:
n = line.shape[0]
endPoints.InsertNextPoint(line[0, 0], line[0, 1], line[0, 2])
endPoints.InsertNextPoint(line[-1, 0], line[-1, 1], line[-1, 2])
points = vtk.vtkPoints()
vtkLines = vtk.vtkCellArray()
vtkLines.InsertNextCell(n)
for i in range(n):
points.InsertNextPoint(line[i, 0], line[i, 1], line[i, 2])
vtkLines.InsertCellPoint(i)
polygonPolyData = vtk.vtkPolyData()
polygonPolyData.SetPoints(points)
polygonPolyData.SetLines(vtkLines)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(polygonPolyData)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(1, 0, 0)
actors.append(actor)
polyData = vtk.vtkPolyData()
polyData.SetPoints(endPoints)
sphereSource = vtk.vtkSphereSource()
sphereSource.SetRadius(1)
glyph3D = vtk.vtkGlyph3D()
glyph3D.SetSourceConnection(sphereSource.GetOutputPort())
glyph3D.SetInputData(polyData)
glyph3D.Update()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(glyph3D.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(0, 0, 1)
actors.append(actor)
return actors
if __name__ == '__main__':
app = QApplication(sys.argv)
window = AirwaySkeleton()
window.show()
sys.exit(app.exec_())
What's wrong with my code? Any suggestion is appreciated!
In addition, how can I pick the point in the surface?
Solution with vtkplotter is simply:
from vtkplotter import *
import numpy as np
mask = np.zeros(shape=[200,200,200], dtype=np.uint8)
mask[ 20:80, 50:150, 50:150] = 1
mask[ 80:150, 80:120, 80:120] = 1
mask[150:170, 50:150, 50:150] = 1
vol = Volume(mask) # returns vtkVolume
iso = vol.isosurface(threshold=1).c('grey').alpha(0.3).pickable(0)
smoothed_iso = iso.smoothLaplacian(niter=30)
aline = Line((20,100,100), (170,100,100), lw=10) # vtkActor
def onLeftClick(mesh):
printc("clicked 3D point:", mesh.picked3d, c='red')
vp.add(Sphere(pos=mesh.picked3d, r=2, c="green"))
vp = Plotter(verbose=0, axes=8, bg='black')
vp.mouseLeftClickFunction = onLeftClick
vp.show(smoothed_iso, aline)
can be embedded in Qt following examples here.

Python VTK polydata not updating

I'm trying use VTK to plot points, then interactively update their locations with a given set of point locations.
I can interactively use a polydata object to plot points, however they do not update when I call self.polydata.Update(). The points will update when I call self.polydata.GetCellData().SetScalars(someCharArray)
Is this a bug in VTK, or am I not updating the point coordinates correctly?
I have included an example script. If you comment out self.polydata.GetCellData().SetScalars(someCharArray) in sliderCallback, the plot will not update the point's coordinates when you use the slider. However they will update if you leave that line in.
Thanks!
import numpy as np
import vtk
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt4 import QtGui
import sys
class ViewerWithScrollBar(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ViewerWithScrollBar, self).__init__(parent)
# Define the renderer and Qt window ------------------------
self.frame = QtGui.QFrame()
self.hl = QtGui.QHBoxLayout()
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.hl.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
self.iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
self.ren.ResetCamera()
self.frame.setLayout(self.hl)
self.setCentralWidget(self.frame)
# Point coordinate data ---------------------------------
self.coordData = {}
self.coordData[0] = np.array([[0,0,0], [1,0,0], [1,1,0]])
self.coordData[1] = self.coordData[0] + np.array([[0.2, 0.1, -0.05], [0,0,0], [0,0,0]])
self.coordData[2] = self.coordData[1] + np.array([[0.2, 0.1, -0.05], [0,0,0], [0,0,0]])
# Define the slider bar and add it to the window ---------------
slider = QtGui.QSlider()
slider.setAccessibleName('Time index')
slider.setRange(0, len(self.coordData)-1)
slider.valueChanged.connect(self.sliderCallback)
self.hl.addWidget(slider)
# Create the polydata object -----------------------------
points = vtk.vtkPoints()
points.SetNumberOfPoints(len(self.coordData[0]))
self.polydata = vtk.vtkPolyData()
for i in range(len(self.coordData[0])):
points.SetPoint(i, self.coordData[0][i])
self.polydata.SetPoints(points)
ptsFilter = vtk.vtkVertexGlyphFilter()
ptsFilter.SetInputConnection(self.polydata.GetProducerPort())
ptsMapper = vtk.vtkPolyDataMapper()
ptsMapper.SetInputConnection(ptsFilter.GetOutputPort())
ptsActor = vtk.vtkActor()
ptsActor.SetMapper(ptsMapper)
ptsActor.GetProperty().SetPointSize(10)
self.ren.AddActor(ptsActor)
self.show()
self.iren.Initialize()
def sliderCallback(self):
index = self.sender().value() # The index that the slider bar is currently on
someCharArray = vtk.vtkUnsignedCharArray()
points = self.polydata.GetPoints()
for i in range(len(self.coordData[index])):
points.SetPoint(i, self.coordData[index][i])
self.polydata.GetCellData().SetScalars(someCharArray) # For some reason the polydata won't update unless this is called.
# self.polydata.Update()
self.iren.Render()
return
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = ViewerWithScrollBar()
sys.exit(app.exec_())
With lib's advice, I modified the code. Calling self.polydata.Modified() in the sliderCallback method fixed the problem
import numpy as np
import vtk
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt4 import QtGui
import sys
class ViewerWithScrollBar(QtGui.QMainWindow):
def __init__(self, parent=None):
super(ViewerWithScrollBar, self).__init__(parent)
# Define the renderer and Qt window ------------------------
self.frame = QtGui.QFrame()
self.hl = QtGui.QHBoxLayout()
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.hl.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
self.iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
self.ren.ResetCamera()
self.frame.setLayout(self.hl)
self.setCentralWidget(self.frame)
# Point coordinate data ---------------------------------
self.coordData = {}
self.coordData[0] = np.array([[0,0,0], [1,0,0], [1,1,0]])
self.coordData[1] = self.coordData[0] + np.array([[0.2, 0.1, -0.05], [0,0,0], [0,0,0]])
self.coordData[2] = self.coordData[1] + np.array([[0.2, 0.1, -0.05], [0,0,0], [0,0,0]])
# Define the slider bar and add it to the window ---------------
slider = QtGui.QSlider()
slider.setAccessibleName('Time index')
slider.setRange(0, len(self.coordData)-1)
slider.valueChanged.connect(self.sliderCallback)
self.hl.addWidget(slider)
# Create the polydata object -----------------------------
points = vtk.vtkPoints()
points.SetNumberOfPoints(len(self.coordData[0]))
self.polydata = vtk.vtkPolyData()
for i in range(len(self.coordData[0])):
points.SetPoint(i, self.coordData[0][i])
self.polydata.SetPoints(points)
ptsFilter = vtk.vtkVertexGlyphFilter()
ptsFilter.SetInputData(self.polydata)
ptsMapper = vtk.vtkPolyDataMapper()
ptsMapper.SetInputConnection(ptsFilter.GetOutputPort())
ptsActor = vtk.vtkActor()
ptsActor.SetMapper(ptsMapper)
ptsActor.GetProperty().SetPointSize(10)
self.ren.AddActor(ptsActor)
self.show()
self.iren.Initialize()
def sliderCallback(self):
index = self.sender().value() # The index that the slider bar is currently on
points = self.polydata.GetPoints()
for i in range(len(self.coordData[index])):
points.SetPoint(i, self.coordData[index][i])
self.polydata.Modified()
self.iren.Render()
return
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = ViewerWithScrollBar()
sys.exit(app.exec_())
I revised it to the version of PyQt5 and ran and tested the code above using latest vtk 8.1.0 and PyQt5 5.10.1 under Windows 10, PyCharm 2018.1 (Community Edition). Look like "points" needs to be modified instead of "self.polydata". Otherwise, it won't get the updated point shown.
import numpy as np
import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt5 import Qt
import sys
class ViewerWithScrollBar(Qt.QMainWindow):
def __init__(self, parent=None):
super(ViewerWithScrollBar, self).__init__(parent)
# Define the renderer and Qt window ------------------------
self.frame = Qt.QFrame()
self.hl = Qt.QHBoxLayout()
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.hl.addWidget(self.vtkWidget)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
self.iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
self.ren.ResetCamera()
self.frame.setLayout(self.hl)
self.setCentralWidget(self.frame)
# Point coordinate data ---------------------------------
self.coordData = {}
self.coordData[0] = np.array([[0,0,0], [1,0,0], [1,1,0]])
self.coordData[1] = self.coordData[0] + np.array([[0.2, 0.0, -0.05], [0,2,0], [0,0,3.5]])
self.coordData[2] = self.coordData[1] + np.array([[0.2, 10.0, -0.05], [0,5.0,0], [0,0,0]])
# Define the slider bar and add it to the window ---------------
slider = Qt.QSlider()
slider.setAccessibleName('Time index')
slider.setRange(0, len(self.coordData)-1)
slider.valueChanged.connect(self.sliderCallback)
self.hl.addWidget(slider)
# Create the polydata object -----------------------------
points = vtk.vtkPoints()
points.SetNumberOfPoints(len(self.coordData[0]))
self.polydata = vtk.vtkPolyData()
for i in range(len(self.coordData[0])):
points.SetPoint(i, self.coordData[0][i])
self.polydata.SetPoints(points)
self.ptsFilter = vtk.vtkVertexGlyphFilter()
self.ptsFilter.SetInputData(self.polydata)
ptsMapper = vtk.vtkPolyDataMapper()
ptsMapper.SetInputConnection(self.ptsFilter.GetOutputPort())
self.ptsActor = vtk.vtkActor()
self.ptsActor.SetMapper(ptsMapper)
self.ptsActor.GetProperty().SetPointSize(10)
self.ren.AddActor(self.ptsActor)
self.show()
self.iren.Initialize()
self.iren.Start()
def sliderCallback(self):
index = self.sender().value() # The index that the slider bar is currently on
points = self.polydata.GetPoints()
for i in range(len(self.coordData[index])):
points.SetPoint(i, self.coordData[index][i])
points.Modified() # Here you need to call Modified for points
self.show()
self.iren.Render()
return
if __name__ == "__main__":
app = Qt.QApplication(sys.argv)
window = ViewerWithScrollBar()
sys.exit(app.exec_())

Categories