Quantcast
Channel: mel wiki
Viewing all articles
Browse latest Browse all 610

How can I find edge-connected verts based on a source vert?

$
0
0
In both examples, you pass in a mesh\vertex ID, it returns back the names of all the verts connected to that one based on common edges. The API version is a lot less code... half of it is just getting an MObject...

Via the Maya API:

import maya.OpenMaya as om

def getConnectedVerts(mesh, vtx):
    """
    Parameters:
    mesh : string : Name of the mesh to query.
    vtx : int :The specific vert index to query.

    return : list : List of full mesh.vtx[#] items that connect.
    """
    # Get an MObject for the passed in mesh:
    selList = om.MSelectionList()
    selList.add(mesh)
    mObject = om.MObject()
    selList.getDependNode(0, mObject)

    ret = []
    iterVert = om.MItMeshVertex(mObject)
    while not iterVert.isDone():
        if iterVert.index() == vtx:
            intArray = om.MIntArray()
            iterVert.getConnectedVertices(intArray)
            ret = ['%s.vtx[%s]'%(mesh, intArray[i]) for i in range(intArray.length())]
            break
        iterVert.next()

    return ret
mesh = 'pPlane1'
vtx = 61
print getConnectedVerts(mesh, vtx)

# ['pPlane1.vtx[62]', 'pPlane1.vtx[50]', 'pPlane1.vtx[60]', 'pPlane1.vtx[72]']

Via Maya commands:

The polyInfo command returns back its info in a really weird way, so we have to chop it up to make it usable.
import re
import maya.cmds as mc

def getConnectedVerts(mesh, vtx):
    """
    Parameters:
    mesh : string : Name of the mesh to query.
    vtx : int :The specific vert index to query.

    return : list : List of full mesh.vtx[#] items that connect.
    """
    sourceV = '%s.vtx[%s]'%(mesh,vtx)
    eStr = mc.polyInfo(sourceV, vertexToEdge=True)[0].strip()
    edges = ['%s.e[%s]'%(mesh, item) for item in re.split('[: ]', eStr.strip().split(':')[-1])[1:] if item]
    vStrs = mc.polyInfo(edges, edgeToVertex=True)

    verts = []
    for vStr in vStrs:
        ids = []
        for item in re.split('[: ]', vStr.strip().split(':')[-1])[1:]:
            if not item:
                continue
            try:
                # I once saw a values passed in as 'Hard': only ints allowed:
                ids.append(int(item))
            except:
                continue
        # Remove any dupes:
        ids = list(set(ids))
        tempVs = ['%s.vtx[%s]'%(mesh, item) for item in ids]
        for item in tempVs:
            if item not in verts:
                verts.append(item)

    verts.remove(sourceV)
    return verts
mesh = 'pPlane1'
vtx = 61
print getConnectedVerts(mesh, vtx)

# ['pPlane1.vtx[72]', 'pPlane1.vtx[60]', 'pPlane1.vtx[50]', 'pPlane1.vtx[62]']

Viewing all articles
Browse latest Browse all 610

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>