Blender 4.1 vs selection modes in view 3d

Hey there!
I just popped in something weird.
If i create a script in Text Editor with checking selection modes (like if in mode 0 then do this, if in mode 1 then do that) and it works perfectly if i execute the script from Text Editor, but if i try to run it in view 3d area (from macro via Pie Menu Editor) it is not working

Like this one (ok edge mode is invalid anyway, but vertex and face mode is working from Text Editor, but not if i run it from view 3d)

Do You have any idea what am i doing wrong please?

import bpy

# Check the selection mode
def get_selection_mode():
    if bpy.context.tool_settings.mesh_select_mode[0]:
        return "VERTEX"
    elif bpy.context.tool_settings.mesh_select_mode[1]:
        return "EDGE"
    elif bpy.context.tool_settings.mesh_select_mode[2]:
        return "FACE"
    else:
        return None

# Create vertex
def create_vertex():
    bpy.ops.mesh.primitive_vert_add()

# Create edge
def create_edge():
    bpy.ops.mesh.primitive_edge_add(size=2)

# Create face (plane)
def create_plane():
    bpy.ops.mesh.primitive_plane_add(size=2)

# Main function
def main():
    selection_mode = get_selection_mode()
    if selection_mode == "VERTEX":
        create_vertex()
    elif selection_mode == "EDGE":
        create_edge()
    elif selection_mode == "FACE":
        create_plane()
    else:
        print("Unknown selection mode or nothing selected.")

if __name__ == "__main__":
    main()

Is this your full code? I don’t see any operator related code.

Hey, I created a script that works with empty blender file too :slight_smile:
So the trick is to run the script in view 3d. Nothing happens to me.

Script should create a mesh item with 1 vertex, and then depending on selection mode (vertex, edge, face) it should create a box, a cylinder or a sphere.

If i run it from Text Editor, it works fine.

I’m running the script via Pie Menu Editor macro in View 3d, but i think it should executed the same way, anyhow it is executed in View 3d.

Question, what am I doing wrong please? :slight_smile:

import bpy

def create_cube():
    bpy.ops.mesh.primitive_cube_add(size=2)

def create_cylinder():
    bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=2, vertices=32)

def create_sphere():
    bpy.ops.mesh.primitive_uv_sphere_add(radius=1, segments=32, ring_count=16)

def create_vertex_at_origo():
    bpy.ops.mesh.primitive_vert_add()

def enter_edit_mode():
    bpy.ops.object.mode_set(mode='EDIT')

def main():
    # Check if any object is selected
    if bpy.context.object is not None:
        # Check selection mode
        mode = bpy.context.tool_settings.mesh_select_mode[:]
        if mode[0]:  # Vertex mode
            create_cube()
        elif mode[1]:  # Edge mode
            create_cylinder()
        elif mode[2]:  # Face mode
            create_sphere()
    else:
        create_vertex_at_origo()
        bpy.ops.object.mode_set(mode='EDIT')
        # Check selection mode
        mode = bpy.context.tool_settings.mesh_select_mode[:]
        if mode[0]:  # Vertex mode
            create_cube()
        elif mode[1]:  # Edge mode
            create_cylinder()
        elif mode[2]:  # Face mode
            create_sphere()

if __name__ == "__main__":
    main()

To be honest, I have no idea what that means… but usually you should create an operator out of your script AFAIK.

I put part of your code with small modifications into operator template that comes with Blender’s Text Editor, and executed it once (Run button in Text Editor), and then I ran it from operators menu (F3 for my config, in 3D view). That way it seems to works. This only prints the select mode of the active object into system console.

import bpy

def get_selection_mode(context):
    
    # there is no active object, return none
    if context.active_object == None:
        return None
    
    if bpy.context.tool_settings.mesh_select_mode[0]:
        return "VERTEX"
    elif bpy.context.tool_settings.mesh_select_mode[1]:
        return "EDGE"
    elif bpy.context.tool_settings.mesh_select_mode[2]:
        return "FACE"
    else:
        return None


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

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        
        # get the edit mode's select mode
        mode = get_selection_mode(context)

        # print it
        print(f"mode: {mode}")    
        
        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleOperator)

def unregister():
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()
    

" executed it once (Run button in Text Editor), and then I ran it from operators menu "
I executed your script from Text Editor, but nothing happens.
My script worked from Text Editor as You can see on the video i provided.

Can You tell me please where can i find “operators menu” in Blender?
I searched on google Blender operators menu but i only got the context menu of the tools are in Blender.

Thank You! :slight_smile:

You’ll have to press either F3 or Space (I think?) depending on your Blender keyboard configuration. I can’t remember what the default is, I’ve used this same setup for three or more years from version to version.

See this video - note you see the output only in the system console, which you can open with Ctrl + F12, or open it from menu - Window > Toggle System Console

See this page:
https://docs.blender.org/manual/en/2.82/interface/controls/templates/operator_search.html

Sorry i don’t have that Simple Object Operator in the search menu list, and nor google knows anything about it.