Python help for returning data please?


Hi there! Could someone help me on a python command?

I have a function to get the point on the surface of the object, but I would like to return the locator that is created on each click… how can I do that?

def hit():

    vpX, vpY, _ = cmds.draggerContext(ctx, query=True, anchorPoint=True)
    position = om.MPoint() 
    direction = om.MVector()

    omui.M3dView().active3dView().viewToWorld(int(vpX), int(vpY), position, direction)

    selectionList = om.MSelectionList()
    dagPath = selectionList.getDagPath(0)
    fnMesh = om.MFnMesh(dagPath)

    intersection = fnMesh.closestIntersection(

    hitPoint, hitRayParam, hitFace, hitTriangle, \
        hitBary1, hitBary2 = intersection

    x, y, z, _ = hitPoint
    if (x, y, z) != (0.0, 0.0, 0.0):
        # I want to return this point
        point = cmds.spaceLocator()
        cmds.move(x, y, z)
ctx = "Click2dTo3dCtx"

if cmds.draggerContext(ctx, exists=True):

cmds.draggerContext(ctx, pressCommand=hit, name=ctx, cursor="crossHair")

thanks a lot!!!


why can’t you just use “return point”?


Because the function is called in this line:

cmds.draggerContext(ctx, pressCommand=hit, name=ctx, cursor=“crossHair”)

where can I assign the returned value?


Instead of return, you could use cmds.fileInfo. Store the name of the locator in fileInfo

cmds.fileInfo('locator', point)

You could manage it a bit better if you have more than 1 locator, I don’t know your exact needs.

Recall it back with:

locator = cmds.fileInfo(q=1, 'locator')


Sounds great! Thank you very much, I will look for this function!
Meanwhile I appended each locator in a list, but maybe is better to use fileInfo.

thanks a lot!