简体   繁体   English

如何在 Maya 中的 Python 中修复 'TypeError: in method 'MFnMesh_anyIntersection', argument 4 of type 'MINtArray const *' 错误

[英]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() .我想在 Maya 中使用OpenMaya.MFnMesh.anyIntersection()尝试使用 Python 进行一些光线投射。

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.我只想从 object 向下投射光线并击中飞机,而不是 go 进一步所以我只得到一个 raycasthit 并从 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)为了使代码在 Maya 中正常运行,您需要一个 object,它在 Y 轴上高于另一个,最好是 polyPlane(例如:在 position [0, 3, 0] 处有一个 polySphere,在 Z4757FE07FD4982A8 处有一个 polyPlane 0, 0, 0], select polySphere 并运行代码)

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:我希望从 raycasthit 中获取翻译值,但出现以下错误:

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.使用OpenMaya.MFnMesh.allIntersections() function 可以很好地工作,但我从光线投射中获得了每一次命中,但我只想要第一次命中。

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 OpenMaya.MFnMesh API 的链接:链接: https://help.autodesk.com/view/MAYAUL/2016/ENU/?guid=__py_ref_class_open_maya_1_1_m_fn_meshhtml_

The main thing is that anyIntersection is looking for a single intersection it hits first, not multiple.主要的是anyIntersection正在寻找它首先命中的单个交叉点,而不是多个。 So your out parameters are of the wrong types because they're arrays.因此,您的输出参数类型错误,因为它们是 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.我发现c++ 文档更清楚地说明了该方法对参数的期望。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 修复这个错误? TypeError:“builtin_function_or_method”类型的参数不可迭代 - Fix this error? TypeError: argument of type 'builtin_function_or_method' is not iterable 如何修复geopy上的`TypeError:&#39;ArcGIS&#39;类型的参数不可迭代`错误? - How to fix `TypeError: argument of type 'ArcGIS' is not iterable` error on geopy? 如何修复 python 中的“TypeError:'NoneType' 类型的参数不可迭代” - How to fix 'TypeError: argument of type 'NoneType' is not iterable ' in python 如何在Python中修复&#39;TypeError:类型&#39;function&#39;的参数不可迭代&#39; - How to fix 'TypeError: argument of type 'function' is not iterable' in python TypeError:&#39;char const *&#39;类型的参数 - TypeError: argument of type 'char const *' 如何在Maya脚本的Python简单类中修复未定义的方法? - How to fix an undefined method in a simple class in Python for maya script? Python错误:TypeError“类型&#39;type&#39;的参数不可迭代 - Python Error: TypeError "argument of type 'type' not iterable Python错误:TypeError:类型“ type”的参数不可迭代 - Python Error: TypeError: argument of type 'type' is not iterable python 训练和测试中的错误:如何修复“TypeError: unhashable type: 'list'” - Error in python train and test : How to fix “TypeError: unhashable type: 'list'” 如何修复&#39;ctypes.ArgumentError:参数2: <type 'exceptions.TypeError'> :RaspberryPi中的错误类型错误 - How to fix 'ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: wrong type' error in RaspberryPi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM