Rotating an 3D object based on previous rotations, similar to the editor's rotation widget

Godot Version

4.5

Question

I am trying to make a simple 3D model editor, but I am running into a problem with rotation. when I rotate an object 90 degrees in the x axis, 90 in the y axis then -90 in the x again it doesn’t rotate as I want it to. I want it to rotate how it does in the editor:
Rotation
but instead when I move it the second time in the x axis the object just goes back to being vertical. I have tried a couple different ways to fix this, mainly quaternions (which I don’t really understand), but I couldn’t get anything to work. What is the best way to solve this?

Don’t use euler rotation angles. Work directly with the basis. If you want to rotate the object around its current axes you can use Node3D::rotate(axis, angle) and specify one of its global basis vectors as the axis.