How do I iterate through objects in a Scene?

I wrote a tiny little script to help me through a repetative task:

import bpy
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.looptools_flatten(influence=100, plane='normal', restriction='none')
bpy.ops.object.editmode_toggle()

Works great when I manually click on each plane I need flattened, but as there are 123 planes which need to be flattened, I’d really like to figure out how to do this with a for loop.

I won’t write out all my failed code as It might prossibly span pages, but here’s the most recent code attempt which doesn’t work:

import bpy

for obj in bpy.data.objects:
    obj.select = True
    bpy.ops.object.editmode_toggle()
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.mesh.looptools_flatten(influence=100, plane='normal', restriction='none')
    bpy.ops.object.editmode_toggle()
    obj.select = False

Can someone tell me why this doesn’t work and what the correct way to go about doing this is? Thanks.

I think you it is what I realized.
All bpy.ops.* … without referencing an object work on a really active object
And you do it like this:

bpy.context.scene.objects.active = “HERE your object”
>>> bpy.ops.mesh.select_all(action=‘SELECT’)
Traceback (most recent call last):
File “<blender_console>”, line 1, in <module>
File “C:\Users\Peter\25blender\blenderlatest\2.59\scripts\modules\bpy\ops.py”, line 179, in call
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.mesh.select_all.poll() failed, context is incorrect

>>> bpy.ops.object.editmode_toggle()
{‘FINISHED’}

>>> bpy.ops.mesh.select_all(action=‘SELECT’)
{‘FINISHED’}
>>> bpy.ops.mesh.looptools_flatten(influence=100, plane=‘normal’, restriction=‘none’)
{‘FINISHED’}

Try this.
I added in some test code. hope this helps.

    # If there ARE objects selected then act on all objects
    if bpy.context.selected_objects != []:
        for obj in bpy.context.selected_objects:
            print(obj.name, obj, obj.type)
            if obj.type == 'MESH': 
                print("&gt;&gt;&gt;&gt;", obj.name)


    # If there are NO objects selected then act on all objects
    if bpy.context.selected_objects == []:
        print('selected:')
        for obj in bpy.context.scene.objects: 
            print(obj.name, obj, obj.type)
            if obj.type == 'MESH': 
                print("&gt;&gt;&gt;&gt;", obj.name)

Yes, active Object! Tha’t exactly what I was missing. Much appreciated, PKHG.

And thanks for the conditional logic, Gary_P.

Here’s the working code:

import bpy

if bpy.context.selected_objects != []:
    for obj in bpy.context.selected_objects:
        if obj.type == 'MESH':
            bpy.context.scene.objects.active = obj
            bpy.ops.object.editmode_toggle()
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.mesh.looptools_flatten(influence=100, plane='normal', restriction='none')
            bpy.ops.object.editmode_toggle()
            


Attachments

buckyballSmalll.blend (442 KB)

Thank you very much for this script. In order to make it run i had to change line 5. from this:

bpy.context.scene.objects.active = obj

to this:

bpy.context.scene.objects.active = ob

Thanks again! Now I can use collada imports without them having problems with textures and normals.

This:
if len(bpy.context.selected_objects) > 0:

is nicer than
if bpy.context.selected_objects != []: