How to get mesh selection mode

Hi there!

A Blender newbie question - just getting started with Blender, and first post in this area! I’m starting to teach myself Python, and I’ll be making a couple of very small (but helpful) scripts if possible. I already made some progress but now I got a question (will post results when/if I get them working properly).

I want to access/get Mesh object selection state.

  1. I know how to set mesh mode. bpy.ops.object.mode_set(mode=‘EDIT’) for example

  2. But how do I know in which mesh selection mode I’m in? How can I test this?
    Seems like google does not help much.

  • I know there are commands like “bpy.ops.object.mode_set”, but how can I test againts it, if it’s true?

  • Seems like bpy.ops commands are only used to set modes and such - have to say I just started yesterday!

  • There are also bpy.context.mode, that seem more suitable commands but I didn’t find anything there… mode only spits out ‘EDIT_MESH’ - which doesn’t help me.

I’m still having a bit hard time figuring out where the most up to date Python scripting information is and which of the manuals contain most up to date info. But I think it’s this:
http://www.blender.org/api/blender_python_api_2_70_5/

Any help is appreciated!

Thanks!

  • Sami

bookmark this: www.blender.org/documentation/250PythonDoc/
It should automatically point to the latest documentation.

Open the python console view, select an object… and run this code


>>> bpy.context.active_object.mode

# it will respond with
'OBJECT'   # or 'EDIT'

The best thing you can do to learn Blender’s Python API is to read the many existing python plugins located in the scripts folder. Just exposing them to you and reading other people’s code will give a lot of insight, even if you don’t immediately understand what the code is doing… you’ll start to recognize patterns.

The other thing to do is to better use the Search features of this forum, try searching for stuff – if you can think of it then it has probably already been asked. There are so many duplicated answers to questions on this forum it is almost embarrassing. Also do searches on http://blender.stackexchange.com/ . tonnes of helpful people there.

Even a literal search for "how to x, y , z in blender python " in Google will often return results for either this forum, Blenderstackexchange or a tonne of other blender related content.

zeffii:

Thanks but I already mentioned that I tried what you suggested.

Iit doesn’t give any specific info about if I’m in: vert, edge or face mode - This is what I’m after.

I got the impression that it might be doable (after all, you can at least set mode in UI) but I just can’t find anything about how to get mode you are in, it seem that this information is well hidden.

I tried all kinds of searches (get, mode, selection related words), I only resorted to asking since I already used 1h banging my head on the wall, searched manual I linked, searched BA and I tried googling quite a bit - I think I explained this.

I hope I don’t sound too lazy.

What I’m trying to do:

  1. Check that I’ve got a mesh selection (done)
  2. Check that I’m in edit mode (done)
  3. Find out selection mode (vert, edge, face) - NOT DONE!
  4. Do stuff to selected components, now that I know type

Thanks anyway!

  • Sami

I did a search for “set blender vertex edge face selection mode” and about the 5th result down

scroll down.

or “current mesh selection mode blender python” ,


# while in edit mode
>>> bpy.context.tool_settings.mesh_select_mode[:]

You might have to know what to look for… that’s true. It’s about gaming the search engines some times :slight_smile:

1 Like

At least now you have a link that redirects to the latest documentation, hopefully making my response not entirely useless.


>>> bpy.context.tool_settings.mesh_select_mode[:]
(True, False, False)   # for vertex selection mode

The colon in brackets will make a copy of the value and return a tuple of 3 booleans – each representing whether or not that selection type is active. It is possible to have all three active at once, then it returns (True, True, True), or just faces will return (False, False, True). From this you can extrapolate the rest.

Zeffii,

big thanks, seems like I didn’t hit the target with my search keys! And yes, that link was enough, tuple value is enough for me to find out what mode I’m in. Haven’t used tuple or Python much at all, but I know enough to make a comparison!

BTW, I’m making a small script that deletes any sub-object/mesh component type, I already got it to point that I can just:

  1. Go to edit mode
  2. Select verts, edges or faces
  3. Press a shortcut
  • Elements get deleted

This way I don’t have go through “press key - open menu - pick a menu item” routine all the time.
Maybe there is already an addon with similar feature, didn’t search as this is for both learning and making my workflow a bit smoother/a lot more similar to what I’m used to (3ds Max…)

Thanks again!