Hello, thanks for the reply.

I'll show you a simple example of the Python code I'm currently writing.

```
from maya.api import OpenMaya

DIRECTION_VEC = [OpenMaya.MFloatVector(0, 1, 0), OpenMaya.MFloatVector(0, 
-1, 0), OpenMaya.MFloatVector(1, 0, 0), OpenMaya.MFloatVector(-1, 0, 0)]
mesh = "pCube1" # Deformed
selectionList = OpenMaya.MGlobal.getSelectionListByName(mesh)
mDagPath = selectionList.getDagPath(0)
meshFn = OpenMaya.MFnMesh(mDagPath)

def isIntersectPoints(meshFn, point):
    sourcePt = OpenMaya.MFloatPoint(list(point))
        for directionVec in DIRECTION_VEC:
            if not meshFn.anyIntersection(sourcePt, directionVec, 
OpenMaya.MSpace.kWorld, 9999, False):
                return False;
    return True

idsList = []
index = 0
for point in positions: # positions e.g) [(0, 0, 0), (0.5, 0.5, 0.5) ... ] 
count : 10,000
    if isIntersectPoints(meshFn, point):
        idsList.append(index)
    index += 1
```

Thank you.
2022년 9월 23일 금요일 오후 3시 50분 3초 UTC+9에 Marcus Ottosson님이 작성:

> Hello! I can shed some light on these.
>
> I tried to work the Python Loop part with C++ MPxCommand, but I couldn’t 
> find a way to return data by executing the Arguments in the form of 
> Float3Array (Point Positions) and the corresponding command. (Return : 
> IntArray)
>
> If you are certain that looping isn’t going to be fast enough in Python, 
> which I don't doubt, then yes this is what I would do. You won’t be able to 
> return data outside of simple types like integers, floats and strings. But 
> what you can do is serialise the data into e.g. JSON and output a string, 
> and then deserialise it from Python. This is what I normally do. At a 
> certain point, deserialising can become a bottleneck, at which point you 
> have a JSON deserialiser in PySide2 which may be faster.
>
> It is said that the part can be written in C++ using PyBind, but I know 
> that the For Loop speed is limited due to the same GIL issue. Is there any 
> way to solve it??
>
> This point was what prompted me to reply - don’t do it! :D
>
> Both MPxCommand and PyBind can work outside of any GIL - they are both 
> called outside of Python - and while PyBind *can* return more complex 
> types like vectors and even Python objects it is terribly inconvenient to 
> work with in Maya. Because Maya - and Python in general - cannot reload or 
> unload a binary Python module. Yes, you read that right. Once you import 
> myBinaryModule you cannot unload it. At all. The only recourse is 
> restarting Maya.
>
> Other than that, if that isn’t a problem, then PyBind is a good option for 
> this. You can even pass a NumPy data structure, which would bypass Python 
> even further and keep all data - especially heavy data - in C++ and be as 
> fast as it can be.
>
> It seems that there is no way to increase the speed of the current Python 
> Loop by raising the speed of the loop as much as possible.
>
> Hard to say without having an example. Loops in Python shouldn’t be a 
> bottleneck in general; but it’s possible the repeated access to a Maya API 
> class could. Are you able to share a small reproducible? Would be curious 
> to see what the problem may be.
>
> Best,
> Marcus
>
> On Fri, 23 Sept 2022 at 07:20, daeseok chae <cds...@gmail.com> wrote:
>
>> Hello,
>>
>> I am writing a script that extracts a Point located in a mesh using 
>> anyIntersection of MFnMesh.
>>
>> However, there are too many points, so the performance does not come out 
>> while looping.
>>
>> So I'm trying to find another way, but it's not easy to find example code.
>> Here's the method I've tried:
>>
>> 1. I tried to work the Python Loop part with C++ MPxCommand, but I 
>> couldn't find a way to return data by executing the Arguments in the form 
>> of Float3Array (Point Positions) and the corresponding command. (Return : 
>> IntArray)
>> 2. It is said that the part can be written in C++ using PyBind, but I 
>> know that the For Loop speed is limited due to the same GIL issue. Is there 
>> any way to solve it??
>> 3. It seems that there is no way to increase the speed of the current 
>> Python Loop by raising the speed of the loop as much as possible.
>>
>> Loop Count : 1,000,0000 Under
>> anyIntersection: 4 Direction (Up, Left, Right, Down)
>>
>> thank you
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Python Programming for Autodesk Maya" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to python_inside_m...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/python_inside_maya/cc20157f-7440-454a-a230-e5cd1d84e47en%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/python_inside_maya/cc20157f-7440-454a-a230-e5cd1d84e47en%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/fb93a06b-4cdb-4c94-bf1b-f42d2b8918dan%40googlegroups.com.

Reply via email to