从轴获得面法线的旋转

2024-04-28 08:26:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直试图得到欧拉角到一个面法向量的每个轴,但到目前为止我还没有成功。 我尝试对每个轴使用方向余弦,但由于返回值完全关闭,所以没有成功。 这是我目前为止的代码,我很确定我的逻辑有问题,特别是我的数学,但我真的搞不懂。 我用的是python和Maya。我正在修改操纵器轴来检查结果。 提前谢谢! 我的代码是:

import maya.cmds as cmds
import math as math

def Difference(a,b):
    return list(set(a)- set(b))

def VertPosition(vert, axis):
    return cmds.xform(vert,q = 1,ws = 1, t = 1)[axis]

def GetFaceNormal(face):
        cmds.select(face,r=1)
        faceNormal = cmds.polyInfo(fn=True)
        return faceNormal

def GetNumericNormalValue(normalInfoString):
    faceNormalDirection = [0,0,0]
    normalInfoString = str(normalInfoString).split(' ')
    faceNormalDirection[0] = float(normalInfoString[-3])
    faceNormalDirection[1] = float(normalInfoString[-2])
    faceNormalDirection[2] = float(normalInfoString[-1][:-5])
    return faceNormalDirection

def NormalizeVector(vector):
    vLength = VLength(vector)
    for axis in range(0,3):
        vector[axis] = vector[axis]/vLength
    return vector

def VLength(vector):
    vLength = 0
    for axis in range(0,3):
        vLength += pow(vector[axis],2)
    vLength = math.sqrt(vLength)
    return vLength

def GetAngleToAxis(vector):
    vLength = VLength(vector)
    angleToAxis = [0,0,0]
    for axis in range(0,3):
        angleToAxis[axis] += math.degrees(math.acos(vector[axis]/vLength))
    return angleToAxis

faceSelection = cmds.filterExpand(sm=34)
normal = GetNumericNormalValue(GetFaceNormal(faceSelection))
normal = NormalizeVector(normal)
normalAngles=GetAngleToAxis(normal)
cmds.manipPivot(o=normalAngles)

Tags: inforreturndefrangemathfloatvector
1条回答
网友
1楼 · 发布于 2024-04-28 08:26:18

有几种方法可以解释数据,但是如果给定一个向量,你需要每个长轴各自的旋转,你可以取向量分量的反切线。轴决定组件的旋转:

inVec = /(input vector)/
rotZ = atan(inVec.y/inVec.x)
rotY = atan(inVec.x/inVec.z)
rotX = atan(inVec.y/inVec.z)

结果以弧度表示。在

相关问题 更多 >