[PYTHON] [Blender] How to get the selection order of vertices, edges and faces of an object

When I added a new function of the Blender plugin, I learned how to get the selected order of vertices, edges, and faces of an object, so I will introduce it. Only the one selected by right-clicking (the default key layout is assumed hereafter) is targeted. You can select multiple vertices, edges, and faces at the same time with the B key, but you cannot get the selection order with Blender at this time.

Source code

The script to get the selection order for the currently selected object is shown below. Don't forget to import bmesh in addition to bpy to access the internal structure of blender. It includes version-dependent processing of blender, which is required from Blender 2.73. For more information, see [http://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.73/Addons](http://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.73] / Addons).

Get face selection order

get_face_selection_sequence.py


import bpy
import bmesh   #Needs additional import

obj = bpy.context.active_object

bpy.ops.object.mode_set(mode='EDIT')  #Processing must be done in EDIT mode
bm = bmesh.from_edit_mesh(obj.data)

#Blender version is 2.Required when 73 or above
if bpy.app.version[0] >= 2 and bpy.app.version[1] >= 73:
    bm.faces.ensure_lookup_table()

#Show face selection order
for e in bm.select_history:
    if isinstance(e, bmesh.types.BMFace) and e.select:
        print(repr(e))

Get the selection order of edges

get_edge_selection_sequence.py


import bpy
import bmesh   #Needs additional import

obj = bpy.context.active_object

bpy.ops.object.mode_set(mode='EDIT')  #Processing must be done in EDIT mode
bm = bmesh.from_edit_mesh(obj.data)

#Blender version is 2.Required when 73 or above
if bpy.app.version[0] >= 2 and bpy.app.version[1] >= 73:
    bm.edges.ensure_lookup_table()

#Display the selection order of edges
for e in bm.select_history:
    if isinstance(e, bmesh.types.BMEdge) and e.select:
        print(repr(e))

Get the selection order of vertices

get_vert_selection_sequence.py


import bpy
import bmesh   #Needs additional import

obj = bpy.context.active_object

bpy.ops.object.mode_set(mode='EDIT')  #Processing must be done in EDIT mode
bm = bmesh.from_edit_mesh(obj.data)

#Blender version is 2.Required when 73 or above
if bpy.app.version[0] >= 2 and bpy.app.version[1] >= 73:
    bm.verts.ensure_lookup_table()

#Show the selection order of vertices
for e in bm.select_history:
    if isinstance(e, bmesh.types.BMVert) and e.select:
        print(repr(e))

Execution result

The result of the execution is shown below.

result.py


>>> for e in bm.select_history:
...     if isinstance(e, bmesh.types.BMFace) and e.select:
...         print(repr(e))
<BMFace(0x108482cf0), index=4, totverts=4>
<BMFace(0x108482c80), index=2, totverts=4>
>>>
>>> for e in bm.select_history:
...     if isinstance(e, bmesh.types.BMEdge) and e.select:
...         print(repr(e))
<BMEdge(0x11241f380)>, index=11, verts=(0x10849a960/6, 0x10849a998/7)>
<BMEdge(0x11241f1a0)>, index=5, verts=(0x10849a880/2, 0x10849a8b8/3)>
>>>
>>> for e in bm.select_history:
...     if isinstance(e, bmesh.types.BMVert) and e.select:
...         print(repr(e))
<BMVert(0x10849a960), index=6>
<BMVert(0x10849a8f0), index=4>
>>>

Reference information

Recommended Posts

[Blender] How to get the selection order of vertices, edges and faces of an object
[Blender] How to dynamically set the selection of EnumProperty
Get the attributes of an object
How to know the internal structure of an object in Python
[Python] How to get the first and last days of the month
[Blender x Python] How to create an original object
An introduction to object orientation-let's change the internal state of an object
How to get all the keys and values in the dictionary
How to get an overview of your data in Pandas
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
How to get and set the NTP server name by DHCP
How to get rid of the "Tags must be an array of hashes." Error in the qiita api
How to get the pixel value of the point from the satellite image by specifying the latitude and longitude
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
How to get followers and followers from python using the Mastodon API
[EC2] How to install chrome and the contents of each command
How to get the Python version
Blender has no object selection order
How to get the vertex coordinates of a feature in ArcPy
I summarized how to change the boot parameters of GRUB and GRUB2
I made an npm package to get the ID of the IC card with Raspberry Pi and PaSoRi
[Python Kivy] How to get the file path by dragging and dropping
[Python] How to specify the window display position and size of matplotlib
How to get the date and time difference in seconds with python
Python Note: When you want to know the attributes of an object
Click the Selenium links in order to get the elements of individual pages
__Getattr__ and __getattribute__ to customize the acquisition of object attributes by dots
Try to separate the background and moving object of the video with OpenCV
How to check the version of Django
How to get colored output to the console
How to save the feature point information of an image in a file and use it for matching
How to make VS Code aware of the venv environment and its benefits
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
How to count the number of elements in Django and output to a template
How to find the coefficient of the trendline that passes through the vertices in Python
I want to store the result of% time, %% time, etc. in an object (variable)
Note: How to get the last day of the month with python (added the first day of the month)
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to calculate the volatility of a brand
How to find the area of the Voronoi diagram
How to get help in an interactive shell
How to get dictionary type elements of Python 2.7
How to get the files in the [Python] folder
[Blender x Python] How to make an animation
To get the path of the currently running python.exe
How to achieve access by attribute and retention of insertion order in Python dict
How to get started with Visual Studio Online ~ The end of the environment construction era ~
How to confirm the Persival theorem using the Fourier transform (FFT) of matplotlib and scipy
Pass an array from PHP to PYTHON and do numpy processing to get the result
Get the title of yahoo news and analyze sentiment
How to create an article from the command line
How to know the port number of the xinetd service
How to get the variable name itself in python
How to know the current directory in Python in Blender
How to create a submenu with the [Blender] plugin
How to visualize the decision tree model of scikit-learn
[Blender] Summary of how to install / update / uninstall add-ons
Get to know the feelings of gradient boosting trees
I checked out the versions of Blender and Python
How to use the grep command and frequent samples