Point Cloud plotted using GLScatterPlotItem inside a GLWidget only appears for a second and then becomes invisible

43 views Asked by At

I am trying to load a point cloud and plot using GLScatterPointItem which inside a GLWidget which is inside a QFrame which is inside a QWindow(for obvious reasons) . However it only appears for a brief second then becomes invisible. The item is present in memory but is not displayed. I can manipulate the data and plot. I have also checked the color value after the item is created and the alpha is 255 i.e. completely opaque but still nothing is visible on the widget.

Here is the code

import sys
import os
import math
import numpy as np

import open3d 

from PyQt5 import QtWidgets
import PyQt5.QtGui as Qgui
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QMainWindow, QHBoxLayout, QVBoxLayout, QFileDialog, QWidget, QFrame, QToolBar, QAction, QSlider)
import pyqtgraph.opengl as gl


class myWindow(QMainWindow):
    fileInput = []
    XYZ = []
    fileName = ""
    pcd = []

    def __init__(self):
        global fileInput
        super(myWindow,self).__init__() 
        fileInput = []
        # self.pcd = open3d.geometry.PointCloud()
        self.path = os.getcwd()
        self.setGeometry(100, 50, 800, 800)
        self.setWindowTitle('Graph viewer')
        self.wid = QtWidgets.QWidget()
        self.setCentralWidget(self.wid)

        self.initUI()

    def initUI(self):

        # change font size
        self.font = Qgui.QFont()
        self.font.setPointSize(12)


        self.vboxMain= QtWidgets.QVBoxLayout(self)

        #### Create a ToolBar
        self.toolbar = QtWidgets.QToolBar('First ToolBar',self)
        self.toolbar.setFont(self.font)
        
        ## First tool
        addFile = QAction(Qgui.QIcon(),'Add File',self)
        addFile.triggered.connect(self.load_ply)
        self.toolbar.addAction(addFile)

        #### Create a FRAME for displaying plotlib
        self.frame = QtWidgets.QFrame(self)
        self.frame.setLineWidth(1)
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)

        # create a layout inside the frame
        self.Hbox = QtWidgets.QHBoxLayout(self.frame)

        # create glwidget for viewing
        self.glwidget = gl.GLViewWidget(self.frame)
        self.glwidget.setBackgroundColor('k')
        self.glwidget.opts['distance'] = 100
        self.glwidget.opts['fov']=1

        # add the widget to layout
        self.Hbox.addWidget(self.glwidget) 

        self.vboxMain.addWidget(self.toolbar)
        self.vboxMain.addWidget(self.frame)

        self.wid.setLayout(self.vboxMain)

    def load_ply(self):
        global fileName,pcd
        fileName,_ = QtWidgets.QFileDialog.getOpenFileName(self.wid,"Open a Point Cloud", self.path,"PLY Files (*.ply) ")
        if fileName:
            pcd = open3d.io.read_point_cloud(fileName)
            points = pcd.points
            x = np.asarray(points)[:,0]
            y = np.asarray(points)[:,1]
            z = np.asarray(points)[:,2]
            print(f'the length of x,y,z is {len(x),len(y),len(z)}')
            scatter = gl.GLScatterPlotItem(pos=np.column_stack((x,y,z)),size=1,color=((255,255,255,255)))
            self.glwidget.addItem(scatter)
            print(scatter.color)
        else:
            print('The file does not exist or filename is incorrect')

def window():
    app = QApplication(sys.argv)
    win = myWindow()

    win.show()
    sys.exit(app.exec_())

window()

0

There are 0 answers