Extruding multiple planes in for loop

My goal is to extrude a great number of selected planes through a for loop. However, my current code keeps on extruding the active object:

import bpy

selected_objects = bpy.context.selected_objects


length = len(selected_objects)


bpy.ops.object.mode_set(mode='EDIT')
for x in range(0, length):
    o = selected_objects[0]
    o.location = (0, 0, x * 0.1)
    bpy.ops.mesh.extrude_region_move()
    bpy.ops.transform.translate(value=(0, 0, 1))
bpy.ops.object.mode_set(mode='OBJECT')

How can I extrude each plane, just like how the location is defined to each plane?

can you show before and after pictures to illustrate the desired outcome?

Hi CoDEmanX!

Thanks for having a look! My aim is to extrude an x number of planes (selected_objects) along the z-axis through a for loop. The planes will be imported and variable in number.



Still can’t seem to figure it out. I tried to make a function out of it, but to no avail.

Hi CodeManX, thank you for replying!

Here are two pictures, the before and after. The second picture is the desired outcome. The amount of Planes are 500+ that must undergo this procedure. It would be great if this can be controlled through a for loop.



However, I’ve been at it for some time now, but it doesn’t want to work the way I wish to. I broke everything down into functions, look at the order of executing the extrusion…anyways, thanks for your time.

well the extrude operator you use is actually a macro, which needs to be called differently.

Here’s my approach:

import bpy

bpy.ops.mesh.primitive_plane_add()
ob = bpy.context.object
scene = bpy.context.scene

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.extrude_region_move(MESH_OT_extrude_region={}, TRANSFORM_OT_translate={"value":(0, 0, 0.1)})
bpy.ops.object.mode_set(mode='OBJECT')

obs = []
for i in range(1, 21):
    ob_new = ob.copy()
    obs.append(ob_new)
    ob.location.z = i * 0.5
    
for ob in obs:
    scene.objects.link(ob)
    
scene.update()

Ah, so macro operations don’t run in for loops. The pictures I uploaded were made quickly are not the actual work, sorry if I was unclear. The planes are actually images planes and are already imported on the scene. So a generic plane add and copy is not really working in this situation. I tried to convert to mesh to curve and do a curve extrude (o.extrude). This kinda of worked, but the object loses the images. I was also looking into Bmesh and extrude with Vector, but that hasn’t paid of yet. Any idea how to tackle this? Thanks for any help.