2.92 Problem with active object

Hi.

I won’t bother you with all the long ( sh*tty ) things i write for modifying the ivy addon, but just bring you here the problem i got:

In an execute function, i print the active object, create an object, make it active and quit. Printing the active object before quit gives me the object i created. all is ok.

At next execute, when the active object is print, it shows the previously active object, before the one i created.

Is there a way to tell blender to really make active the object i asked to be active ?

Note that in the 3Dview, the active object is the one i created, and all seems ok…

for info here’s the execute, called by properties changes and button press:

    def execute(self, context):

        print("active when enter:",context.view_layer.objects.active)

        bpy.ops.mesh.primitive_cube_add()
        #resize the cube
        bpy.ops.transform.resize(value=(.1,.1,.1))

        # get the cube
        Ivy3DObject = bpy.context.selected_objects[0]
                
        # deselect all
        for ob in bpy.context.selected_objects:
            ob.select_set(False)
            
        #select the cube and make it active
        Ivy3DObject.select_set(True)
        context.view_layer.objects.active = Ivy3DObject
        print("active when quit =",context.view_layer.objects.active)

        return {'FINISHED'}

Any idea ?

please help !

Thanks and happy blending !

seems to work fine to me. not really sure what results you’re expecting

active when enter: <bpy_struct, Object(“Cube”) at 0x0000020C44509198>
active when quit = <bpy_struct, Object(“Cube.001”) at 0x0000020C44507A58>
active when enter: <bpy_struct, Object(“Cube.001”) at 0x0000020C44507A58>
active when quit = <bpy_struct, Object(“Cube.002”) at 0x0000020C0323F948>
active when enter: <bpy_struct, Object(“Cube.002”) at 0x0000020C0323F948>
active when quit = <bpy_struct, Object(“Cube.003”) at 0x0000020C00DDA0F8>
active when enter: <bpy_struct, Object(“Cube.003”) at 0x0000020C00DDA0F8>
active when quit = <bpy_struct, Object(“Cube.004”) at 0x0000020C4DC5C2F8>

What i wanted was exactly what you got

strangely this didn’t work for me.
At enter i had always the “Cube” as active ( though the cube.001 ) was selected and active in the 3D view.

However i changed parts of the program structure/sequence and now it works properly ( even if i still don’t know why it works :confused: )
My python and blender API knowledge are far too weak for me to understand all this…

Thanks and happy blending !

I don’t think the first element of the list will always be the result of an object creation operator. It should be the active (context.active) by default, but you might need to call context.view_layer.update() the ensure that. Also, don’t use Pascal case for variables, that’s awful :stuck_out_tongue:

Despite its prevalence, excessively calling operators in operators is a bad practice unless you can’t replicate the operators behavior manually. Operators are primarily to be called from the interface. If you need to use operators though, and they’re not behaving as you think they should, you can try calling the view layer update function to update the scene data and that should fix the context.

Personally, if I need the objects resulting from an operator, I do something like:

init_objects = bpy.data.objects[:] # The [:] is making a copy of the list
operator_call
new_objects = [obj for obj in bpy.data.objects if obj not in init_objects]
1 Like

Thanks for the Info @init_pixel

I did do tries with some update ( finally i didn’t really know what i asked update for as there are lots of them ) but it didn’t help with my problem.

For the variable names i use very often the underscore but here i used the std C, C++ or C# one for diferenciation what comes from me of what was previously written ( i do some changes in the ivy addon :stuck_out_tongue: ). —> IVY addon small changes - #33 by pitibonom
As i’m a dumb noob in python and blender api, this helps me a lot.

Btw, if you have some few time having a peek at the shitty code i made, i’d really be pleased you can help enhance and clean it with your feedback :wink:

happy blending !