Can an object be automatically rotated?

I have a pistol model whose rotation is a few degrees off:

Does Blender have a function that would automatically fix the rotation and "align" it according to the object's geometry, perhaps like the 3D cursor to Selection option?

Thank you!

• – batFINGER Jul 4 at 2:13

Palkonimo has the basics of it right, but you can accomplish the same thing more quickly using Blender's built in snap options.

1. Begin by picking two points on your model that you want to line up with an axis. Move the 3D cursor to one of those points.

2. Duplicate that point, and move it away along the axis you wish to align to.

3. Select the other of your chosen points on the model as the active element, expand your selection of the model using L or Ctrl +.

4. Rotate, using vertex snap to active element, and target the vertex you duplicated earlier.

• Neat... You could also add that you can define the axis you want to align to, (if it's not conveniently world X,Y or Z,) by creating a Custom Orientation from an existing edge, and moving your snap-target-vertex in Y of that orientation. – Robin Betts Jul 4 at 6:46

I think there's no way of automatically doing it. Yet would be a nice addition, though you'd still have to select an edge and an axis that ought to be aligned.

Nevertheless, you can do it manually. As you can see in the image, there's a plane rotated a bit. I want to align it "according to it's geometry". This means that I have to select an edge and an axis that I want to align. In my case I want to align the selected edge with the y-axis. And from the drawn triangle we can derive the angle that the plane has to be rotated.

We just need to know the lengths of two of the triangle's sides. Since you know the position of the vertices, which are also two of the corners of the triangle, we can just calculate them:

In my case the length of the red vertical line is the difference between the y-coordinates of the vertices: 0.759317 - (-1.193079) = 1.952396

The length of the red horizontal line is the difference between the x-coordinates of the vertices: -1.193079 - (-0.759317) = -0.433762

Now we just have to use some trigonometry. With a being the angle we are looking for: a = arctan(-0.433762/1.952396) = -12.525905490885°

In this gif you can see that it is now aligned after rotating it that exact amount:

Now, this is just an example. In your specific case you'd probably choose an edge on the pistol's side and align it to the z-axis. Then do the same math: get the difference of the z-coordinates for one, the difference of the x-coordinates for the other side. Then use the arctan to get the angle and rotate your weapon along the y-axis.