Python help for returning data please?


#1

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()
    selectionList.add(mesh)
    dagPath = selectionList.getDagPath(0)
    fnMesh = om.MFnMesh(dagPath)

    intersection = fnMesh.closestIntersection(
        om.MFloatPoint(position),  
        om.MFloatVector(direction),  
        om.MSpace.kWorld,  
        99999,  
        False)  


    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.deleteUI(ctx)

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

thanks a lot!!!


#2

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


#3

Hi!
Because the function is called in this line:

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

where can I assign the returned value?


#4

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')


#5

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!