Get Surface Info

Download getSurfaceInfo.py

python, pyfuncs, maya

This function query the position / rotation of a point at given coordinates U and V and returns a dictionnary containing both informations in the form {'t' : position, 'r' : rotation}

Here is the full code ;

select
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def get_surface_info(surf, u, v):
    """
    Returns the transforms of a given point over a NURBS surface
    :param surf: surface name
    :param    u: U parameter
    :param    v: V parameter
    :return    : dict {'t' : position, 'r' : rotation}
    """
    # we create the pointOnSurfaceInfo and connect it
    posi = cmds.shadingNode('pointOnSurfaceInfo', au=True, n='tmp_pOSI')
    cmds.connectAttr('%s.ws' % surf, '%s.is' % posi)
    cmds.setAttr('%s.parameterU' % posi, u)
    cmds.setAttr('%s.parameterV' % posi, v)
    # we create three empty groups to define the correct point  position / orient
    point, aim, up = [cmds.group(n=s, em=True) for s in ('tmp', 'aim', 'up')]
    pV = cmds.getAttr('%s.r.p' % posi)[0]
    uV = [pV[i] + tu for i, tu in enumerate(cmds.getAttr('%s.r.tu' % posi)[0])]
    aV = [pV[i] + tv for i, tv in enumerate(cmds.getAttr('%s.r.tv' % posi)[0])]
    # we set the position to each point
    for pt, vec in ((point, pV), (aim, aV), (up, uV)):
        cmds.setAttr('%s.t' % pt, *vec, type='double3')
    aC = cmds.aimConstraint(aim, point, aim=[0, 1, 0],
                              u=[1, 0, 0], wut='object', wuo=up)[0]
    
    infos = {'t': cmds.xform(point, q=True, t=True, ws=True),
             'r': cmds.xform(point, q=True, ro=True, ws=True)}
    # cleaning
    cmds.delete([aC, aim, up, point, posi])
    del point, aim, up, uV, aV

    return infos

If you have any questions... =)