How can I activate an object in Blender with Python

2

From a Python script I want to activate an object in order to enter editing mode and edit it. To select the desired object I use

bpy.context.scene.objects["Nombre_objeto"].select = True  # (1)

What happens is that if before selecting the object another active object exists, when entering edit mode with bpy.ops.object.editmode_toggle() , it enters the editing mode of the object that was previously active and not the object selected with ( 1).

I would like to know how I can leave an active object to edit its properties.

I hope someone can help me, thanks for the attention given. Annex Python code to run in Blender, so you can see what I mean exactly.

###INICIO-CÓDIGO###

import bpy

bpy.ops.mesh.primitive_uv_sphere_add(size=1, location=(-1,-2,0))
bpy.ops.object.duplicate_move()
nombre = 'c1'
bpy.context.object.name = nombre
bpy.data.objects[nombre].location=(1, 2, 0)


bpy.context.scene.objects["Esfera"].select = True
bpy.ops.object.editmode_toggle()
##<-- EJECUTAR SCRIPT -->##

###FIN-CÓDIGO###
    
asked by Luis Gutiérrez 30.04.2018 в 18:55
source

1 answer

1

You must mark the selected object as active in that scene using the attribute. active of SceneObjects :

import bpy


bpy.context.scene.objects["Sphere"].select = True
bpy.context.scene.objects.active = bpy.data.objects['Sphere']
bpy.ops.object.editmode_toggle()

    
answered by 30.04.2018 в 19:23