Skip to content

Instantly share code, notes, and snippets.

@warmonkey
Created September 11, 2023 07:11
Show Gist options
  • Save warmonkey/883e07097a5332384dd30cf5867ec0e6 to your computer and use it in GitHub Desktop.
Save warmonkey/883e07097a5332384dd30cf5867ec0e6 to your computer and use it in GitHub Desktop.
3d attitude display with pyqt5
import sys
import math
import numpy as np
from stl import mesh # pip install numpy-stl
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import pyqtgraph as pg
from pyqtgraph.opengl import GLViewWidget, GLGridItem, MeshData, GLMeshItem
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(0, 0, 700, 900)
self.initUI()
def initUI(self):
centerWidget = QWidget()
self.setCentralWidget(centerWidget)
layout = QVBoxLayout()
centerWidget.setLayout(layout)
self.viewer = GLViewWidget()
layout.addWidget(self.viewer, 1)
self.viewer.setCameraPosition(distance=100)
pg.setConfigOption('antialias', True)
g = GLGridItem()
g.setSize(200, 200)
g.setSpacing(5, 5)
self.viewer.addItem(g)
self.model = self.addSTL('v2.stl')
def addSTL(self, filename):
points, faces = self.loadSTL(filename)
meshdata = MeshData(vertexes=points, faces=faces)
mesh = GLMeshItem(meshdata=meshdata, smooth=True, drawFaces=False, drawEdges=True, edgeColor=(0.0, 0.5, 0.0, 0.3))
mesh.setGLOptions('additive')
self.viewer.addItem(mesh)
return mesh
def loadSTL(self, filename):
m = mesh.Mesh.from_file(filename)
shape = m.points.shape
points = m.points.reshape(-1, 3)
faces = np.arange(points.shape[0]).reshape(-1, 3)
return points, faces
def setAttitude(self, q):
trans = QMatrix4x4()
trans.rotate(QQuaternion(q[0], q[1], q[2], q[3]))
self.model.setTransform(trans)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
q = QQuaternion.fromEulerAngles(0, 90, 0).toVector4D()
window.setAttitude(q)
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment