Rotate stl file in matplotlib 3d

3.4k Views Asked by At

I'm using numpy-stl for open stl file in plot. It is open stl file. But i have a problem. I want to rotate stl file in plot like this image:

enter image description here

Code is here:

from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot
from math import sin,cos,pi
import numpy as np

# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)

m1 = mesh.Mesh.from_file('filea.stl')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(m1.vectors))

# Auto scale to the mesh size
scale = m1.points.flatten()
axes.auto_scale_xyz(scale-10, scale+10, scale)

# Show the plot to the screen
pyplot.show()
2

There are 2 best solutions below

0
woz523 On BEST ANSWER

I solved this problem. If you have question about numpy-stl rotation problem or another problems, you can ask me without hesitation. Here is my solution:

self.m1 = mesh.Mesh.from_file('assets/file.stl')
self.m1.rotate([x-axis, y-axis, z-axis], math.radians(angle))

code:

self.m1.rotate([1, 0, 0], math.radians(angleinxaxis))
self.m1.rotate([0, 1, 0], math.radians(angleinyaxis))
self.m1.rotate([0, 0, 1], math.radians(angleinzaxis))
0
Musabbir Arrafi On

Firstly, this isn't exactly an answer to your question and I am sorry for that in advance. But a much better way is to use the open3d package to 3D visualize stl files. It's completely intractable with generated triangle_mesh.

Here's how you can do it:

  • Installation
pip install open3d
  • Usage
import open3d as o3d

mesh = o3d.io.read_triangle_mesh("Body_Kylo_Ren_fixed.stl")
mesh = mesh.compute_vertex_normals()
o3d.visualization.draw_geometries([mesh],window_name="STL", left=1000, top=200, width=800, height=650)

Output: You will see an interactable 3D stl file

enter image description here

Check out open3d documentation to know more.