Notifications
Clear all

[Closed] Python getElementsUsingFace?

Are there any ways to get/select mesh elements using the SDK or mainly the Python API? I’m just starting to move from Maxscript to the SDK. Learning by doing, but I cannot find much information about sub-object elements.

Something equivalent to MaxScript getElementsUsingFace.

5 Replies

There is no build in method in the SDK, but you can check meshop.cpp and polyop.cpp found in the mxsagni folder.

Also here is a recent thread about this.

Alright, thanks! I’ll check it out

Hi, I had the same sort of issue for detaching by elements. Which is all in this thread ! Hope this helps

Hi, I took the liberty to translate denisT’s c++ code from the thread linked above to Python. I hope that is alright…
I do not know how it holds up speed wise, but at least it seems to work way better than MSX and without the major memory leaks… Maybe it could be helpful for those others that are not that set in c++ yet.

def GetElements(node):
    obj = node.GetObject()
    objTriMesh = obj.AsTriObject()
    objMesh = objTriMesh.GetMesh()

    numVerts = objMesh.GetNumVertices()
    numFaces = objMesh.GetNumFaces()
    
    allElements = []

    faces = MaxPlus.BitArray(numFaces)
    faces.SetAll()
    verts = [[] for i in range(numVerts)]

    for i in range(0, numFaces):
        face = objMesh.GetFace(i)
        for k in range(0,3):
            verts[face.GetVert(k)].append(i)

    for i in range(0, numFaces):    
        if faces[i]:
            
            element = []
            element.append(i)
            faceBits = MaxPlus.BitArray(numFaces)
            vertBits = MaxPlus.BitArray(numVerts)

            #for j in range(0, len(element)):
            j = 0
            while j < len(element):
                fi = element[j]
                j += 1

                if not faceBits[fi]:
                    face = objMesh.GetFace(fi)
                
                    for k in range(0,3):
                        v = face.GetVert(k)

                        if vertBits[v]:
                            continue

                        for singleFace in range(0, len(verts[v])):
                            element.append(verts[v][singleFace])
                            vertBits.Set(v, True)
                    
                    faceBits.Set(fi, True)
                    faces.Clear(fi)
                    
            allElements.append(MaxPlus.BitArray(faceBits))

    return allElements

Take a look here. It is a simple MXS algorithm and it might be a little faster.