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.context.space_data.pivot_point = 'CURSOR'

    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.

  • $\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$ – Martynas Žiemys Jun 28 '19 at 0:23
import bpy

for object in bpy.context.selected_objects:
    object.location.z += 0.1 # Do whatever you want to the object
|improve this answer|||||
  • $\begingroup$ Updated with more information, apologies for the lack of detail in the original post. Thanks for the help! $\endgroup$ – BlendNoob Jun 28 '19 at 1:13
  • $\begingroup$ Figured it out. Updated post with the missing part. $\endgroup$ – BlendNoob Jun 28 '19 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.