0
$\begingroup$

Im not good at python script, but I want to run two actitons when I change to edit mode. or I want to know register any Hoykey for run this scripts?

import bpy
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')

This is to switch to Face Selection mode at the same time as you change to the edit mode (as in Maya). I don't know how to do this.

$\endgroup$
3
  • 1
    $\begingroup$ Do you really need a script for this? <kbd>TAB</kbd> let's you switch between object and edit mode and once in edit mode <kbd>3</kbd> switches to face select. Did I misunderstand you and this is meant to be used in an add-on instead of your general workflow when switching to edit mode? $\endgroup$ Commented Sep 8, 2019 at 15:05
  • $\begingroup$ @rjg yeah, I used to work with my laptop so I need this script indeed.... $\endgroup$
    – Jin K
    Commented Sep 8, 2019 at 15:07
  • $\begingroup$ Doesn't matter whether you're using a laptop or workstation. Once you press TAB and set the selection to Faces, the setting is stored as long you change it. Don't get your point... Can you elaborate please why do you think this is nice to have? $\endgroup$
    – brockmann
    Commented Sep 8, 2019 at 17:04

3 Answers 3

1
$\begingroup$

Not sure if this is what you are looking for (and maybe there is a way to do this without scripting).

This add-on will let you activate edit mode (with face select) by pressing ctrl + shift + Q in 3d viewport (see comment in code to change hotkey).

I have modified the Operator Simple template (Text Editor, Templates->Python):

import bpy


bl_info = {
    "name": "Set Edit Mode",
    "description": "Set edit mode with face selection",
    "category": "Object",
    "blender": (2, 80, 0),
}

def main(context):
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        o = context.active_object
        return o is not None  and o.type == 'MESH' and \
            context.mode != 'EDIT_MESH'

    def execute(self, context):
        main(context)
        return {'FINISHED'}

keymap = None

def register():
    bpy.utils.register_class(SimpleOperator)
    c = bpy.context.window_manager.keyconfigs.addon
    if(c):
        m = c.keymaps.new(name='3D View', space_type='VIEW_3D')   
        # Hotkey - ctrl + shift + Q
        i = m.keymap_items.new("object.simple_operator", 'Q', 'PRESS', \
            shift = True, ctrl = True)
        keymap = (m, i)

def unregister():
    if(keymap != None):
        keymap[0].keymap_items.remove(keymap[1])

    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()
$\endgroup$
1
$\begingroup$

You can use bpy.msgbus to identify when mode is changing:

handle/owner is a way to identify what has been added by you.

subscribe_to is a rna path to what you want to be informed about.

Though, subscribing to bpy.context.mode does not work, it seems.

import bpy

handle = object() 

def notify_test(*args):
    print( "Mode:", bpy.context.mode )

subscribe_to = bpy.types.Object, "mode"

bpy.msgbus.subscribe_rna(
    key=subscribe_to,
    owner=handle,
    args=(1, 2, 3),
    notify=notify_test,
    )

From brockmann answer here.

And initial example here.

$\endgroup$
1
$\begingroup$

Adding to the existing answers:

In the upcoming Blender 2.81 there is a new operator that let's you do this, which means you can have a single function call or a shortcut to switch to edit mode and enabled face select.

bpy.ops.object.mode_set_with_submode(mode='EDIT', mesh_select_mode={"FACE"})

The shortcut can be included in your keymap by setting the identifier to object.mode_set_with_submode. Set the mode to Edit Mode and enabled Face select. The tricky part is to decide what the shortcut should be.

Keymap

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .