Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This script builds a rotation matrix based on 3 selected verts. It then calculates the euler angles.
#python
import lx
import lxu.vector
import math
# this something I got from stackoverflow some time ago
# if you'd like to know more about it have a look at this: http://www.soi.city.ac.uk/~sbbh653/publications/euler.pdf
def getRot2(m):
if m[0][0] == 1.0:
x = math.atan2(m[0][2],m[2][3])
y = 0
z = 0
elif m[0][0] == -1.0:
x = math.atan2(m[0][2],m[2][3])
y = 0
z = 0
else:
x = math.atan2(-m[2][0],m[0][0])
y = math.asin(m[1][0])
z = math.atan2(-m[1][2],m[1][1])
return ( math.degrees(z), math.degrees(x), math.degrees(y))
def getRotAngles():
# 'select' layer for layerservice
mLayer = lx.eval('query layerservice layers ? main')
# get selected verts
lx.eval('select.type vertex')
selVerts = lx.evalN('query layerservice verts ? selected')
# check if there are 3 verts selected
if len(selVerts) != 3:
# replace with a dialog to make it more user friendly, I'm to lazy right now ;)
lx.out('Please select exactly 3 vertices!')
return
# get vert positions
v0Pos = lx.eval('query layerservice vert.wPos ? %s'%selVerts[0])
v1Pos = lx.eval('query layerservice vert.wPos ? %s'%selVerts[1])
v2Pos = lx.eval('query layerservice vert.wPos ? %s'%selVerts[2])
# !!! you should check at this point that the vectors are not collinear !!!
# this means that they are not in a straight line, again I'm to lazy ;)
# create rotation matrix
# calculate vec0
vec0 = lxu.vector.normalize(lxu.vector.sub(v1Pos,v0Pos))
# calculate a temporary tvec1
tVec1 = lxu.vector.sub(v2Pos,v0Pos)
# cross product of vec0 and tvec1 will calc vec2 which is perpendicular to vec0 and tvec1 as well as vec1
vec2 = lxu.vector.normalize(lxu.vector.cross(vec0,tVec1))
# cross product of vec0 and vec2 will calc vec1
vec1 = lxu.vector.normalize(lxu.vector.cross(vec0,vec2))
# add them to a rotation matrix
rotMat = (vec0,vec1,vec2)
lx.out(getRot2(rotMat))
getRotAngles()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.