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

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

我想在 Maya 中使用 OpenMaya.MFnMesh.anyIntersection().

尝试使用 Python 进行一些光线投射

我只是想从物体向下投射一条光线并击中一个平面,而不是再进一步,所以我只得到一个光线投射并从光线投射中获得平移值。

我从视频中获取了代码,使其更加清晰易懂。

为了在 Maya 中正确地 运行 代码,您需要一个在 Y 轴上高于另一个对象的对象,最好是一个 polyPlane(例如:在位置 [0, 3] 处有一个 polySphere , 0] 和位置 [0, 0, 0] 处的多边形平面,select 多边形球体和 运行 代码)

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

我希望从 raycasthit 获得翻译值,但我收到以下错误:

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

改用 OpenMaya.MFnMesh.allIntersections() 函数效果非常好,但我从光线投射中得到了每一次命中,但我只想要 首先命中。

指向 OpenMaya.MFnMesh API 的链接: link: https://help.autodesk.com/view/MAYAUL/2016/ENU/?guid=__py_ref_class_open_maya_1_1_m_fn_mesh_html

最主要的是 anyIntersection 正在寻找一个 单个 交点,它首先到达,而不是多个。所以你的输出参数是错误的类型,因为它们是数组。

我也会避免在循环中清除或进行新的选择,因为每次都必须重新绘制视口,这只会降低性能。

这是一个工作示例,它将在它命中的第一个网格上创建定位器:

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

我发现 c++ documentation 更清楚该方法对参数的期望。