且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Python在Maya中使用distanceDimension旋转

更新时间:2023-02-02 16:57:25

您可能会发现,使用动画约束比使用数学约束更容易做到.您将以相等的重量将光点约束到两个定位器上,这会将其放置在两个定位器之间.然后将其瞄准以指向其中一个对象,使世界朝上作为侧面矢量",并旋转旋转以防止光线发生翻转(这是区域光线,对吗?)

You'll probably find this easier to do with animation constraints rather than doing it with math. You would point-constrain the light to both locators at equal weight -- that will place it halfway between them. Then aim-constrain it to point at one of them with the world up as the 'side vector' and a rotation offset to keep the light from flipping (it's an area light, yes?)

def area_light_between(target1, target2):
    lightShape = cmds.createNode('areaLight')
    lightXform = cmds.listRelatives(lightShape, p=True)

    #You can get the distance manually without creating a distance dimension:
    start = cmds.xform(target1, q=True, t=True)
    end = cmds.xform(target2, q=True, t=True)
    difference = [end[k] - start[k] for k in range(3)] 
    distance = math.sqrt(difference[0]**2 + difference[1]**2 +  difference[2]**2) 
    cmds.xform(lightXform, s = (distance / 2.0, 0.1, 1))


    # constrain the light between the targets
    # use the mo=False flag so you don't keep the offset
    cmds.pointConstraint(target1, target2,lightXform,  mo = False)

    # add the aim constraint (your world-up vector may vary)
    cmds.aimConstraint(target2, lightXform, worldUpVector = (0,0,1), aimVector=(1,0,0), upVector = (0,0,1), mo = False) 

    return lightXform

area_light_between('pSphere1', 'pSphere2')

我的场景是Z-Up,所以我必须指定一个世界向上矢量(0,0,1)-如果您使用香草玛雅Y-up,则worldUpVector将为(0,1,0).与任何目标约束情况一样,如果目标向量太接近向上向量,则将发生翻转–如果您以这种方式或使用纯数学方法进行处理,则必须进行处理.

My scene is Z-Up so I have to specify a world up vector of (0,0,1) -- if you're using vanilla Maya Y-up your worldUpVector will be (0,1,0). As with any aim constraint situation you'll get flipping if the aim vector gets too close to the up vector -- you'll have to handle that if you do it this way or with pure math.

这样做的一个好处是,您可以在事后编辑约束,以在保持整体关系的同时抵消位置或目标.

One nice advantage of this is that you can edit the constraints after the fact to offset the position or aim while retaining the overall relationship.