I now understand what you are trying to achieve. You want to group
selections of vertices that are connected.
The approach you were using had the caveat that you would need to select
the verts in connected order otherwise it was possible to have orphaned
ones that should be grouped (I fixed your code, tried it out and found
this).
Here is another way of doing it, I basically grab a single selected vert,
look if any of the verts around it are in our selection list, if they are,
add it to the group and add its connected verts to a list of verts
surrounding the group, growing the group one vert at a time. If there
aren’t any verts in the selection list that are on the edge of the group we
can make a new group.
import pymel.core as pm
unmatched_vtxs = pm.ls(sl=1, fl=True)
vtx_grps = []
while len(unmatched_vtxs) > 0:
# grab a vertex
vtx = unmatched_vtxs.pop()
vtx_grps.append([vtx])
# create a list that will contain all of the vertices around the
edge of the group, starting with all of the connected verts for the
start vert
edge_vtxs = [v for v in vtx.connectedVertices()]
matched_vtx = vtx
# continue to try to find more verts to add to the group if we add
a new vert, if not we can loop to the creating the next group
while matched_vtx is not None:
matched_vtx = None
for unmatched_vtx in unmatched_vtxs:
if unmatched_vtx in edge_vtxs:
matched_vtx = unmatched_vtx
# if one of the unmatched verts is on the edge of our
group add it to the group
vtx_grps[-1].append(matched_vtx)
# remove it from the edge list
edge_vtxs.remove(matched_vtx)
# add all of the new matched verts connected verts if
they aren't already in the list
for added_conn_vtx in matched_vtx.connectedVertices():
if added_conn_vtx not in edge_vtxs:
edge_vtxs.append(added_conn_vtx)
break
# remove the matched vert from our list of unmatched ones
if matched_vtx is not None:
unmatched_vtxs.remove(matched_vtx)
print vtx_grps
This is pretty slow in python for big selections as it runs in polynomial
time, it might be more useful as a command C++ plugin.
Jack.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/CAOz4VE1_eYp1_Jz21tZSsysD7ApjAZ_cX_adWGXYmfhHO2CDTw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.