python三维转换教程_如何使用Python在Maya之类的软件中将三维矢量转换为Euler旋转...

There is a given three-dimensional vector.

I want to convert this vector to a rotation which could make one of local axis (like X axis) of that cube to match the vector's direction.

I prepared two pictures. But I can't paste them because of my reputation. I am a freshman.

I tried so many times but still can't get it to work. I think this may involve the rotation order of the cube when I rotate the cube.

解决方案

You can probably get what you want using an aim constraint. If you want to do it as a one-time operation (not having the constraint active in your scene, you can just do:

lc = cmds.aimConstraint( 'your_object_here', 'target_object_here')

cmds.delete(*lc)

However, if you want to this mathematically in code you can do it without euler angles. Eulers are hard to construct because there are many euler combinations that are equally valid for any given orientation. It's easier to do it by directly setting the matrix of the object so it's local axes point the way you want.

In maya, a matrix works likes this:

Xx Xy Xz 0

Yx Yy Yz 0

Zx Zy Zz 0

Tx Ty Tz 1

where

(Xx, Xy, Xz) is the local X vector of the matrix

(Yx, Yy, Yz) is the local Y vector of the matrix

(Zx, Zy, Zz) is the local Z vector of the matrix

(Tx, Ty, Tz) is the translation of the matrix

Scale, if any, is encoded in the size of the vectors; for a matrix at scale 1 and no rotation, the vectors are normalized so the matrix would be

1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

You can make a matrix using Pymel like so, with 4 tuples representing the rows:

import pymel.core as pm

# in practice these vectors would reflect the orientation you want

new_mat = pm.core.matrix (

( 1, 0, 0, 0),

( 0, 1, 0, 0),

( 0, 0, 1, 0),

( 0, 0, 0, 1)

)

And apply it to an object using the xform command:

pm.xform(my_object, matrix = new_mat)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值