0
$\begingroup$

I wrote a short script which performs multiple operations on a selected objects, and some of those operations depend on the objects position. I have 100+ identical objects in different positions and I would like to apply this set of operations on each object. If I select all of the objects, is there a way to then iterate through each of the objects in this selection?

Apologies if this is a bit vague, I am fairly new to coding so I hope I am using the correct terminology. I was thinking perhaps all the selected objects could be put into an array and each one could be selected by incrementing the array index after performing all the operations.

Edit: For example

def execute(self, context):
    for object in bpy.context.selected_objects:
        bpy.context.scene.objects.active = object #was missing this part
        bpy.ops.object.convert(target='MESH')
        bpy.ops.object.editmode_toggle()
        bpy.ops.mesh.select_all(action='TOGGLE')
        bpy.ops.mesh.edge_face_add()
        bpy.ops.object.editmode_toggle()
        bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')
        bpy.ops.object.editmode_toggle()
        bpy.ops.view3d.snap_cursor_to_selected()
        bpy.context.space_data.pivot_point = 'CURSOR'
        bpy.ops.object.editmode_toggle()

    return {'FINISHED'}

This seems to only apply to the last object selected even though multiple objects are highlighted.

Update: I was missing "bpy.context.scene.objects.active = object". Adding that at the start of the loops fixes the problem.

$\endgroup$
1
  • $\begingroup$ If you provided more info about what set of operations you need to perform, it would be possible to answer this question more specifically. $\endgroup$ Jun 28, 2019 at 0:23

1 Answer 1

2
$\begingroup$
import bpy

for object in bpy.context.selected_objects:
    object.location.z += 0.1 # Do whatever you want to the object
$\endgroup$
2
  • $\begingroup$ Updated with more information, apologies for the lack of detail in the original post. Thanks for the help! $\endgroup$
    – BlendNoob
    Jun 28, 2019 at 1:13
  • $\begingroup$ Figured it out. Updated post with the missing part. $\endgroup$
    – BlendNoob
    Jun 28, 2019 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.