0
$\begingroup$

I have 2 objects in my scene. I only want to do the following simple thing and for some reason every solution I encounter online fails. I want to iterate over all the objects in the scene. for each, I want to set it active and scale it up by 2. Tried something like the following:

for obj in bpy.context.scene.objects:
    # Here I need some line that makes obj the active object!
    bpy.ops.transform.resize(...)

another thing that I fail to do is add a cube and then a sphere and then go back the cube and add a boolean modifier that specifies the sphere (as I cannot change the active object, it tries to assign a boolean modifier with itself and fails). thanks.

I use blender 3.4 if it matters.

$\endgroup$
1
  • 1
    $\begingroup$ There's plenty of information in the Blender docs about using python. Here for instance. $\endgroup$
    – John Eason
    Dec 10, 2022 at 22:54

1 Answer 1

2
$\begingroup$

For operating on the objects using ".ops.", we can either add objects to the selection set using method object's select_set method.

Another way without disturbing the current context and selection is to override the context temporarily using temp_override

import bpy

for obj in bpy.context.scene.objects:
    # Here I need some line that makes obj the active object!
    #bpy.ops.transform.resize(...)

     with bpy.context.temp_override(selected_objects=[obj]):
        print('Operating on obj',obj)
        bpy.ops.transform.resize(value=(2, 2, 2))

For the second part of the question,

import bpy

bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, 0), rotation=(0, 0, 0), scale=(1, 1, 1))
cube=bpy.data.objects[-1]

bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=(0.5, 0, 0), rotation=(0, 0, 0), scale=(1, 1, 1))
sphere=bpy.data.objects[-1]

with bpy.context.temp_override(selected_objects=[cube], object=cube, active_object=cube):
    bpy.ops.object.modifier_add(type='BOOLEAN')
    bpy.context.object.modifiers["Boolean"].object = sphere
$\endgroup$
1
  • $\begingroup$ @GalSuchetzky, did this help? $\endgroup$
    – R M K
    Dec 13, 2022 at 22:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .