pythonraycastingmaya-api

How to fix 'TypeError: in method 'MFnMesh_anyIntersection', argument 4 of type 'MIntArray const *' error in Python in Maya


I wanted to try out some raycasting with Python in Maya using OpenMaya.MFnMesh.anyIntersection().

I just want to cast a ray from on object downwards and hit a plane, not go any further so I get only one raycasthit and get the translation values from the raycasthit.

I got the code from a video and made it a bit clearer to understand.

For the code to run properly in Maya you need an object that is higher in the Y-axis than a different one, preferably a polyPlane(for example: having a polySphere at position [0, 3, 0] and a polyPlane at position [0, 0, 0], select the polySphere and run the code)

import maya.OpenMaya as OpenMaya
import maya.cmds as cmds

def RayReposition(*args):
    direction = (0.0, -1, 0)
    sel = cmds.ls(sl = True)

    fromPositionRay = cmds.xform(sel[0], query = True, translation = True)
    selShape = cmds.listRelatives(shapes = True)
    meshes = cmds.ls(geometry = True)

    cmds.select(clear = True)

    for x in meshes:
        if x == selShape[0]:
            continue
        else:
            OpenMaya.MGlobal.selectByName(x)
            sList = OpenMaya.MSelectionList()

            OpenMaya.MGlobal.getActiveSelectionList(sList)
            item = OpenMaya.MDagPath()
            sList.getDagPath(0, item)
            item.extendToShape()

            fnMesh = OpenMaya.MFnMesh(item)

            raySource = OpenMaya.MFloatPoint(fromPositionRay[0], fromPositionRay[1], fromPositionRay[2], 1.0)
            rayDir = OpenMaya.MFloatVector(direction[0], direction[1], direction[2])
            faceIds = None
            triIds = None
            idsSorted = False
            testBothDirections = False
            worldSpace = OpenMaya.MSpace.kWorld
            maxParam = 999999
            accelParams = None
            sortHits = True
            hitPoints = OpenMaya.MFloatPointArray()

            hitRayParams = OpenMaya.MFloatArray()
            hitFaces = OpenMaya.MIntArray()
            hitTris = None
            hitBarys1 = None
            hitBarys2 = None
            tolerance = 0.0001

            hit = fnMesh.anyIntersection(raySource, rayDir, worldSpace, maxParam, testBothDirections, faceIds, triIds, idsSorted, accelParams, tolerance, hitPoints, hitRayParams, hitFaces, hitTris, hitBarys1, hitBarys2)

            OpenMaya.MGlobal.clearSelectionList()
            firstHit = (hitPoints[0].x, hitPoints[0].y, hitPoints[0].z)
            print firstHit

RayReposition()

I expected to get the translation values from the raycasthit but I get the following error:

TypeError: in method 'MFnMesh_anyIntersection', argument 4 of type 'MIntArray const *'

Using the OpenMaya.MFnMesh.allIntersections() function instead works perfectly fine but I get every single hit from the raycast, but I only want the first hit.

Links to the OpenMaya.MFnMesh API: link: https://help.autodesk.com/view/MAYAUL/2016/ENU/?guid=__py_ref_class_open_maya_1_1_m_fn_mesh_html


Solution

  • The main thing is that anyIntersection is looking for a single intersection it hits first, not multiple. So your out parameters are of the wrong types because they're arrays.

    I would also avoid clearing or making new selections in your loop as it would just slow down performance by having to redraw the viewports every time.

    Here's a working example that will create a locator on the first mesh it hits:

    import maya.OpenMaya as OpenMaya
    import maya.cmds as cmds
    
    
    def RayReposition(*args):
        direction = (0.0, -1, 0)
        sel = cmds.ls(sl=True)
    
        fromPositionRay = cmds.xform(sel[0], query=True, translation=True)
        selShape = cmds.listRelatives(shapes=True)
        meshes = cmds.ls(geometry=True)
    
        for x in meshes:
            if x == selShape[0]:
                continue
            else:
                sList = OpenMaya.MSelectionList()
                sList.add(x)
                item = OpenMaya.MDagPath()
                sList.getDagPath(0, item)
                item.extendToShape()
    
                fnMesh = OpenMaya.MFnMesh(item)
    
                raySource = OpenMaya.MFloatPoint(fromPositionRay[0], fromPositionRay[1], fromPositionRay[2], 1.0)
                rayDir = OpenMaya.MFloatVector(direction[0], direction[1], direction[2])
                worldSpace = OpenMaya.MSpace.kWorld
                maxParam = 999999
                testBothDirections = False
                faceIds = None
                triIds = None
                idsSorted = False
                accelParams = None
                sortHits = True
    
                hitPoints = OpenMaya.MFloatPoint()
                hitRayParams = None
                hitFaces = None
                hitTris = None
                hitBarys1 = None
                hitBarys2 = None
                tolerance = 0.0001
    
                hit = fnMesh.anyIntersection(
                    raySource, rayDir, faceIds, triIds, idsSorted, worldSpace, maxParam, testBothDirections, accelParams, 
                    hitPoints, hitRayParams, hitFaces, hitTris, hitBarys1, hitBarys2, tolerance)
    
                if hit:
                    firstHit = (hitPoints.x, hitPoints.y, hitPoints.z)
                    loc = cmds.spaceLocator()[0]
                    cmds.xform(loc, ws=True, t=firstHit)
                    print "Hit on {} at {}".format(x, firstHit)
                    break
    
    
    RayReposition()
    

    I find the c++ documentation a bit more clearer of what the method expects for parameters.