Create a button that selects objects blender python?

Hi Everyone,

I’m trying to create a UI button that will select an object that I tell it to. For instance, This:

import bpy

bpy.ops.object.select_pattern(pattern="Cube")

will select the default cube, but I need to make a button to execute that command.

Thanks!

import bpy


def select_object(self):


    bpy.ops.object.select_pattern(pattern="Cube")




class select_object_operator(bpy.types.Operator):
    """Select Object"""
    bl_idname = "mesh.objectselect"
    bl_label = "Select Cube"
    bl_options = {'REGISTER', 'UNDO'}
    
    
    
    
    
    
    
    def execute(self, context):
        select_object(self)
        return {'FINISHED'}

class select_object_Panel(bpy.types.Panel):
    
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    #bl_context = "editmode"
    bl_label = "Select Object"


    def draw(self, context):
        
        layout = self.layout
        row = layout.row(align=True)
        
        row.operator(select_object_operator.bl_idname)

 
def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()

There’s no need to write an operator to wrap the select_pattern operator. If you don’t want the props dialog to pop up, use this:

import bpy

class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"


    def draw(self, context):
        layout = self.layout


        row = layout.row()
        row.operator_context = 'EXEC_DEFAULT'
        props = row.operator("object.select_pattern", text="Select Pattern 'Cube'")
        props.pattern = "Cube"


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


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


if __name__ == "__main__":
    register()

Note that you could also use something like:
bpy.ops.wm.context_set_id(data_path=“scene.objects.active”, value=“Cube”)

and its layout counterpart:
props = layout.operator(“wm.context_set_id”)
props.data_path = “scene.objects.active”
props.value = “Cube”

But that will change the active state only. What you usually want is to change the selection state as well - which could be solved by a custom operator:

import bpy




def main(context):
    for ob in context.scene.objects:
        print(ob)




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


    def execute(self, context):
        ob = bpy.data.objects.get("Cube")
        if ob is not None:
            ob.select = True
            context.scene.objects.active = ob
        return {'FINISHED'}




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




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




if __name__ == "__main__":
    register()


    # test call
    bpy.ops.object.simple_operator()

It will not clear a previous selection, but would be trivial to add.

Thank you both so much! Super helpful, I got it working. Thanks again!