2

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()
273K
  • 29,503
  • 10
  • 41
  • 64
woz523
  • 77
  • 1
  • 9
  • Your arrow is somewhat ambiguous, but in any case, it should be a matter of finding the correct rotation matrix (https://en.wikipedia.org/wiki/Rotation_matrix) and multiplying each point in your mesh by that matrix to get a new point. For example, if you want to rotate about the z axis, multiply the x and y coordinate of each point as shown in the example in the wiki article. – sg_man Jan 19 '21 at 01:09

2 Answers2

3

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))
woz523
  • 77
  • 1
  • 9
0

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.

Musabbir Arrafi
  • 744
  • 4
  • 18