[Python] how to have a fixed azimuth and elevation of a GLTextItem

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to remove the banner effect from the pyqtgraph.opengl.GLTextItem such that whenever the camera angle changes, the angle of the text item is fixed. I can get the size of the text item to be fixed, but I am unable to figure out how to stop the text item from facing the camera view.

    import numpy as np
    from PySide6 import QtWidgets, QtGui
    import pyqtgraph as pg
    from pyqtgraph.opengl import GLViewWidget, GLTextItem

    def euler_matrix(azimuth_deg, elevation_deg):
    a, e = np.radians([azimuth_deg, elevation_deg])
    ca, sa = np.cos(a), np.sin(a)
    ce, se = np.cos(e), np.sin(e)
    Rz = np.array([[ ca, -sa, 0],
    [ sa, ca, 0],
    [ 0, 0, 1]])
    Rx = np.array([[1, 0, 0],
    [0, ce, -se],
    [0, se, ce]])
    return Rx @ Rz

    class CustomText(GLTextItem):
    def __init__(self, *args, worldTextHeight=0.1, **kwargs):
    super().__init__(*args, **kwargs)
    self.worldTextHeight=worldTextHeight
    tf = np.eye(4)
    tf[:3, :3] = euler_matrix(0, 0)
    self.fixed=tf
    self.setTransform(tf)

    def update_text_rotation(self):
    self.setTransform(self.fixed)

    def update_text_size(self):
    unit_per_pixel = self.view().pixelSize(np.array(self.pos))
    pixel_height = max(1, int(self.worldTextHeight / unit_per_pixel))
    f = QtGui.QFont(self.font)
    f.setPixelSize(pixel_height)
    self.font = f

    def paint(self):
    self.update_text_rotation()
    self.update_text_size()
    super().paint()


    class CustomGLView(GLViewWidget):
    def __init__(self):
    super().__init__()
    self.text_item = CustomText(pos=(0,0,0), text="text item", worldTextHeight=1)
    self.addItem(self.text_item)

    self.addItem(pg.opengl.GLAxisItem())
    self.setCameraPosition(distance=5)


    if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    view = CustomGLView()
    view.show()
    app.exec()

    Continue reading...

Compartilhe esta Página