Get edge loop PyMel


#1

If I have an edge object, how can I get an edge loop with Python?


#2

you can use this command to get edge loops…
http://help.autodesk.com/cloudhelp/2018/ENU/Maya-Tech-Docs/CommandsPython/polySelect.html


#3

Assuming you are running polySelect from a selection, you need a way to get the int of the edge before running it.

Something like this?

import pymel.core
body = pm.PyNode('BodyShape')
selectedEdge = pm.selected()[0].indices()[0]
# since you already have an object selected, passing the shape isn't necessary. But you can.
pm.polySelect(body, edgeLoop=selectedEdge)

#4

Thanks, will try this out. Might work (as well as polySelectSp()). But if possible I wish to avoid selection operations.


#5

Also you can’t seem to pass a list to that flag, but this seems to work for multiple edge selections.

selectedEdges = [x.indices() for x in pm.selected(flatten=True)]
[pm.polySelect(edgeLoop=edge, add=True) for edge in selectedEdges]

If you already know the index, then just pass the int directly to the flag. No selection required. (Edit: Oh yeah, I see what you mean by selections…)


#6

I have an edge as a PyMel object, so I have an index.
pm.polySelect() selects an edge loop, I can handle this, but more preferred option would be to get edge loop as an object as well (list of pyMel edge objects) without selecting anything.


#7

pm.polySelect() followed by pm.selected(flatten=True)??


#8

What do you mean by followed by?

import pymel.core as pm

x = pm.polyPlane(n='grid')[0]
edges = x.edges
edge = edges[111]
pm.polySelect(x, edgeLoop=edge.index())

The polySelect() selects the edge loop of “grid” edge number 111 in the viewport, while getting “grid” edge 111 do not require any selection.


#9
import pymel.core as pm

x = pm.polyPlane(n='grid')[0]
edges = x.edges
edge = edges[111]
pm.polySelect(x, edgeLoop=edge.index())
loop = pm.selected(flatten=True)

#10

I mean maybe there is alternative command to polySelect(), which allows to get edges without selection.


#11

None that I’m aware of.
Sadly some operations in Maya involved mucking about in selections like that. I don’t really like having to do it either.

Your other option would be to recreate the edge loop detection algorithm yourself.


#12

Ok, at current stage its working fine with selection.

Checked edge loop selection algorithm, should not be that hard to implement in Maya https://stackoverflow.com/questions/6999893/selecting-edge-loops

Issue is that I don`t need to select loop, I just use loops to detect necessary area of my geometry so in my case it would not be the best option.

Anyway, thanks for your support, Mr White! And other folks.


#13

Based on previous replies I provided a not so elegant solution to your problem. I’m sure there are better ways of doing this, but here you go.

import pymel.core as pm

x = pm.polyPlane(n='grid')[0]
edges = x.edges
edge = edges[111]
loop = pm.polySelect(x, edgeLoop=edge.index(), ns=True)
storeEdges = []

for item in loop[::-1]:
	for edge in edges:
		edgeNum = edge.split("[")[1].split("]")[0]
		if item == long(edgeNum):
			storeEdges.append(edge)

This stores all the edges in the edge loop into the “storeEdges” list. You add the “ns” flag to make sure nothing is selected from polySelect and save the edge indexes in the variable “loop” and then loop through all of the meshes edges to find a match. You can probably do this by looping through the first edge’s connected edges, and then move on to the next connected edge to make it faster.