LoginSignup
10
13

More than 5 years have passed since last update.

[Blender] オブジェクトの頂点・辺・面の選択順序を取得する方法

Last updated at Posted at 2015-02-04

Blenderのプラグインの新規機能を追加した時に、オブジェクトの頂点・辺・面を選択した順序を取得する方法を学んだので紹介します。
右クリックで選択したもの(以降デフォルトのキー配置を想定)だけを対象としています。
Bキーなどでも同時に複数の頂点・辺・面を選択することが出来ますが、現時点でのBlenderでは選択順序を取得することができません。

ソースコード

現在選択されているオブジェクトについて、選択順序を取得するスクリプトを以下に示します。
blenderの内部構造にアクセスするためのbpyの他に、bmeshをインポートすることを忘れないようにしましょう。
blenderのバージョン依存の処理が含まれていますが、Blender 2.73から必要になったためです。
詳細は、http://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.73/Addonsをご覧ください。

面の選択順序を取得

get_face_selection_sequence.py
import bpy
import bmesh   # 追加でインポートが必要

obj = bpy.context.active_object

bpy.ops.object.mode_set(mode='EDIT')  # 処理はEDITモードで行う必要がある
bm = bmesh.from_edit_mesh(obj.data)

# blenderのバージョンが2.73以上の時に必要
if bpy.app.version[0] >= 2 and bpy.app.version[1] >= 73:
    bm.faces.ensure_lookup_table()

# 面の選択順序を表示
for e in bm.select_history:
    if isinstance(e, bmesh.types.BMFace) and e.select:
        print(repr(e))

辺の選択順序を取得

get_edge_selection_sequence.py
import bpy
import bmesh   # 追加でインポートが必要

obj = bpy.context.active_object

bpy.ops.object.mode_set(mode='EDIT')  # 処理はEDITモードで行う必要がある
bm = bmesh.from_edit_mesh(obj.data)

# blenderのバージョンが2.73以上の時に必要
if bpy.app.version[0] >= 2 and bpy.app.version[1] >= 73:
    bm.edges.ensure_lookup_table()

# 辺の選択順序を表示
for e in bm.select_history:
    if isinstance(e, bmesh.types.BMEdge) and e.select:
        print(repr(e))

頂点の選択順序を取得

get_vert_selection_sequence.py
import bpy
import bmesh   # 追加でインポートが必要

obj = bpy.context.active_object

bpy.ops.object.mode_set(mode='EDIT')  # 処理はEDITモードで行う必要がある
bm = bmesh.from_edit_mesh(obj.data)

# blenderのバージョンが2.73以上の時に必要
if bpy.app.version[0] >= 2 and bpy.app.version[1] >= 73:
    bm.verts.ensure_lookup_table()

# 頂点の選択順序を表示
for e in bm.select_history:
    if isinstance(e, bmesh.types.BMVert) and e.select:
        print(repr(e))

実行結果

実行した結果を以下に示します。

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>
>>>

参考情報

10
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
13