PyMEL's ls
seems to roll several related functions into one (match by type, name, regexp-pattern, etc.). There's no such function built in to Blender's Python API, so you must create selection list manually.
You have to know what element you need to process. If it's a list of MeshVertex
, MeshEdge
or MeshPolygon
, filter out objects whose type
isn't "MESH"
. It's also possible that edit-mode and object-mode data isn't synced, which will happen if the script is run when the object is in edit-mode (presumably while the user's selecting elements). To make the correct selection list regardless of the object's state, run update_from_editmode
beforehand. Then you can create the list:
import bpy
obj = bpy.context.object
mesh = obj.data # Assumed that obj.type == 'MESH'
obj.update_from_editmode() # Loads edit-mode data into object data
selected_polygons = [p for p in mesh.polygons if p.select]
selected_edges = [e for e in mesh.vertices if e.select]
selected_vertices = [v for v in mesh.vertices if v.select]
# ... process selection list...
This also applies to the newer mesh editing API bmesh
and also other object types (check the API doc's index if the element needed has a select*
attribute), though can be a bit more involving for some types. For example, a curve object can contain more than one spline, so iteration over each splines must be chained in some way to reach all BezierSplinePoint
:
import bpy
from itertools import chain
obj = bpy.context.object
curve = obj.data # Assumed that obj.type == 'CURVE'
obj.update_from_editmode() # Loads edit-mode data into object data
selected_cpoints = [p for p in chain(*[s.bezier_points for s in curve.splines])
if p.select_control_point]
# ... process selection list...