wolph / numpy-stl

Simple library to make working with STL files (and 3D objects in general) fast and easy.
http://numpy-stl.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
624 stars 105 forks source link

Rotating stl file with QTimer #141

Closed omerenen closed 4 years ago

omerenen commented 4 years ago

I can rotate stl file that I've imported. However, when I try to rotate it with QTimer by random number every second, it is not working. Although, random numbers are being written, It is just rotating when I run it, after that it is not. How can I do it?

Code is here :

class Canvas(FigureCanvas, FuncAnimation):
    def __init__(self, parent=None, width=0, height=0, dpi=55):
        self.fig = Figure(figsize=(8, 7.5), dpi=57)
        self.fig.patch.set_facecolor('#efefef')
        self.fig.patch.set_alpha(0)

        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)
        self.ax6 = self.figure.add_subplot(111, projection='3d')
        self.ax6.get_xaxis().set_visible(False)
        self.ax6.set_xlim(-150,250)
        self.ax6.set_ylim(-200,200)
        self.ax6.set_zlim(0,400)
        self.ax6.set_facecolor('#efefef')

        self.m1 = mesh.Mesh.from_file('assets/uydu.stl')
        self.timerRotation = QtCore.QTimer()
        self.timerRotation.timeout.connect(self.rotator)
        self.timerRotation.start(1000)

    def rotator(self):
        self.degree = random.randint(0, 360)
        print(f"{self.degree} degree" )
        self.m1.rotate([0, 1, 0], math.radians(self.degree))
        self.ax6.add_collection3d(mplot3d.art3d.Poly3DCollection(self.m1.vectors,edgecolor='k'))
        scale = self.m1.points.flatten()
        self.ax6.auto_scale_xyz(scale-10, scale+10, scale)
wolph commented 4 years ago

It's really hard to say what's going wrong here, but I can take a wild guess. Are you sure you're redrawing after the rotation?

omerenen commented 4 years ago

It's really hard to say what's going wrong here, but I can take a wild guess. Are you sure you're redrawing after the rotation?

yes, I am

wolph commented 4 years ago

Perhaps you need to add an argument to QTimer? https://doc.qt.io/qtforpython/PySide2/QtCore/QTimer.html

The examples show a parent element being passed. I wouldn't expect it to be garbage collected here but I've had some funky issues with that in the past with Qt and Python.

omerenen commented 4 years ago

Perhaps you need to add an argument to QTimer? https://doc.qt.io/qtforpython/PySide2/QtCore/QTimer.html

The examples show a parent element being passed. I wouldn't expect it to be garbage collected here but I've had some funky issues with that in the past with Qt and Python.

I think, there is no problem when the py file is run because the STL file is rotating when it starts. I wonder why it does not continue to its rotating while the random numbers are being written on the console.

omerenen commented 4 years ago

image

image

Actually, it is rotating the STL file. I realized it when I change the 3D view position. It creates a new stl view every one second, instead of rotating it.

omerenen commented 4 years ago

Problem was solved.

Here is the solution:

`

        self.m1 = mesh.Mesh.from_file('assets/uydu.stl')

        self.timerSim = QtCore.QTimer()

        self.timerSim.timeout.connect(self.rotator)

        self.timerSim.start(1000)

        def rotator (self):

        self.m1.rotate([1, 0, 0], math.radians(pitch_array[-1]-pitch_array[-2]))
        self.m1.rotate([0, 1, 0], math.radians(roll_array[-1]-roll_array[-2]))
        self.m1.rotate([0, 0, 1], math.radians(yaw_array[-1]-yaw_array[-2]))
        self.canvas.ax6.add_collection3d(mplot3d.art3d.Poly3DCollection(self.m1.vectors, edgecolor='k'))
        self.canvas.ax6.auto_scale_xyz([-135, 135], [-135, 135], [-135, 135])
        self.canvas.draw()
        self.canvas.ax6.clear()

`