0

I'm trying to find the closest point on a mesh using the OpenMaya MMeshIntersector class but I want to limit the distance it searches. I'm using this so I can get the barycentricCoords to get a more accurate value.

Whenever I enter in a value to limit the distance, I get a RuntimeError: (kFailure): Unexpected Internal Failure. However if I enter in a value higher than 47.0, it works, but that's too high a value for my usage, I'm hoping to use values lower than 1. I'm not sure how to use the maxDistance flag, and there isn't much documentation on how to use it. Does anyone have any links or info on how to use it?

I've google searched and gone through the maya documents, haven't had much of luck getting any concrete info

import maya.OpenMaya as OpenMaya

def getBarycentricCoords(sourceMesh, targetMesh, distanceThreshold):
    selectionList = OpenMaya.MSelectionList()
    selectionList.add(sourceMesh)
    sourceMeshDag = OpenMaya.MDagPath()
    selectionList.getDagPath(0, sourceMeshDag)

    selectionList = OpenMaya.MSelectionList()
    selectionList.add(targetMesh)
    targetMeshDag = OpenMaya.MDagPath()
    selectionList.getDagPath(0, targetMeshDag)


    mObj = OpenMaya.MObject()
    currentFace = OpenMaya.MItMeshPolygon( sourceMeshDag, mObj )

    targetMeshMPointArray = OpenMaya.MPointArray()
    targetMeshMFnMesh = OpenMaya.MFnMesh(targetMeshDag)
    targetMeshMFnMesh.getPoints(targetMeshMPointArray, OpenMaya.MSpace.kWorld)

    matrix = sourceMeshDag.inclusiveMatrix() 
    node = sourceMeshDag.node()
    intersector = OpenMaya.MMeshIntersector()
    intersector.create( node, matrix )

    pointInfo = OpenMaya.MPointOnMesh()
    uUtil = OpenMaya.MScriptUtil(0.0)
    uPtr = uUtil.asFloatPtr()
    vUtil = OpenMaya.MScriptUtil(0.0)
    vPtr = vUtil.asFloatPtr()
    pointArray = OpenMaya.MPointArray()
    vertIdList = OpenMaya.MIntArray()

    for idx in range(targetMeshMPointArray.length()):

        intersector.getClosestPoint( targetMeshMPointArray[idx], pointInfo, distanceThreshold )
        pointInfo.getBarycentricCoords(uPtr,vPtr)

I expect any float value as the maxDistance should work, but I get RuntimeError: (kFailure): Unexpected Internal Failure from maya that doesn't really help me debug the error itself.

Green Cell
  • 4,677
  • 2
  • 18
  • 49
dege
  • 65
  • 9
  • 1
    I can confirm the same error with certain maxDistance values. Interesting enough all examples seem to NOT be using the maxDistance parameter. It seems to work fine without maxDistance so you could omit it then check the distances yourself (performance likely will take a hit), or you can wrap a `try` `except RuntimeError` around it, but that's a very dirty solution. I'll try to dig more into it. – Green Cell Apr 05 '19 at 07:52
  • 1
    Even this example wraps it with `try` `except`..! It's a cheap solution but maybe it's good enough. https://programtalk.com/python-examples/maya.OpenMaya.MPointOnMesh/ – Green Cell Apr 05 '19 at 07:59
  • Oh wow so basically the maxDistance flag can fail if it doesn't hit something at the 1st run. The try except method worked, at this point I don't mind doing it that way, thanks for that! Funny though that without the flag it works, probably have to flag this to autodesk and hope they fix it – dege Apr 05 '19 at 16:50

0 Answers0