Selecting an object with 2.8?

Hello

I’m new at scripting.

I know that it’s a basic question but I tried everything I could and nothing works.

So here’s my problem:

I want to create a shortcut that automatically sets up a mirror modifier to an empty and then my custom pie menu opens with extra modifier options.

The only problem is that the menu will show me the option only of the selected object, and in my series of code I’m stuck at selecting the empty. I just want to select the object I defined as “OB” and it’s done.

Here is the code:

import bpy

OB = bpy.context.selected_objects[0]
bpy.ops.object.modifier_add(type='MIRROR')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.empty_add(type='PLAIN_AXES', view_align=False, location=(bpy.context.scene.cursor_location))
bpy.context.object.name = "Mirror Axes"
OB.modifiers["Mirror"].mirror_object = bpy.data.objects["Mirror Axes"]

# HERE I NEED THE CODE THAT SELECTS "OB"

open_menu("D Mirror Menu")

so normaly in 2.79 you insert thoses two lines

bpy.context.scene.objects.active = OB
OB.select = True

so the result would be

import bpy

OB = bpy.context.selected_objects[0]
bpy.ops.object.modifier_add(type='MIRROR')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.empty_add(type='PLAIN_AXES', view_align=False, location=(bpy.context.scene.cursor_location))
bpy.context.object.name = "Mirror Axes"
OB.modifiers["Mirror"].mirror_object = bpy.data.objects["Mirror Axes"]
bpy.context.scene.objects.active = OB
OB.select = True
open_menu("D Mirror Menu")

and its working !






but they are some change in 2.8
https://en.blender.org/index.php/Dev:2.8/Source/LayersCollections/API-Changes
so the code dont work anymore

and they said its now

bpy.context.render_layer.objects.active = OB 
OB.select_set(action='SELECT')

so the result would be

import bpy

OB = bpy.context.selected_objects[0]
bpy.ops.object.modifier_add(type='MIRROR')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.empty_add(type='PLAIN_AXES', view_align=False, location=(bpy.context.scene.cursor_location))
bpy.context.object.name = "Mirror Axes"
OB.modifiers["Mirror"].mirror_object = bpy.data.objects["Mirror Axes"]
bpy.context.render_layer.objects.active = OB 
OB.select_set(action='SELECT')
open_menu("D Mirror Menu")

but it dont work for me
the empty is still selected and i want it to be “OB”

could someone help me ?

its thoses two line in 2.8

OB.select_set(state=True)
context.view_layer.objets.active = OB

but it dont make it active for me !

Try

OB.select_set(state=True)
context.view_layer.objects.active = OB

You’re missing a c in objets :stuck_out_tongue:

1 Like

bpy.context.view_layer.objects.active = OB
WORKING !!! YESSSSSSSSS

that’s feels good :smiling_face_with_three_hearts:

1 Like