Ubernoob question - doing things with all selected objects

Sorry for asking such simple things here - but I couldnt find it anywhere. I just need a simple script that changes all the selected objects - ie add a specific material or set to smooth or set object pass index. This would help me to automate some work … Much thanks!

This code prints the list of objects in the current scene.


import bpy
obl = bpy.data.objects
for i in obl: print(i.type)

This code prints the object types and their assigned name


import bpy
obl = bpy.data.objects
for i in obl: print(i.type, i.name)

When we cycle through the list of objects in the scene, we can decide
to only print the objects that are of type ‘MESH’.


import bpy
obl = bpy.data.objects
for i in obl:
    if i.type == 'MESH':
        print(i.name)

If you are only interested in the selected objects


import bpy
for i in bpy.context.selected_objects: print(i)

if you want to smooth all selected objects, it’s much easier


import bpy
bpy.ops.object.shade_smooth()

if you hover with the mouse over the button ‘smooth’ in the tool shelf of 3dview, the tooltip will show you the python command.

Setting a ‘specific material’ is a little bit more involved, and so is setting ‘object pass index’. You might have criteria by which you determine what material or pass index to assign to an object.

Materials, assuming you have your materials defined already and you are using a basic material setup. Reusing a bit of code from earlier. This prints the names and materials of the selected objects.


import bpy
for i in bpy.context.selected_objects:
    print(i.name, i.active_material.name)

to set the selected objects to a material, where ‘MAT1’ is replaced with the name of your material.


import bpy
for i in bpy.context.selected_objects:
    i.active_material = bpy.data.materials['MAT1']

Now, i’ve never used a pass index for renders so i had to dig around a little for that.


import bpy
for i in bpy.context.selected_objects:
    print('object name:', i.name, 'pass index:', i.pass_index)

The above code doesn’t give a very nicely formatted printout, but that’s beyond the scope of the problem here, anyway. Here’s a way to set all selected objects to some arbitrary pass_index:


import bpy
for i in bpy.context.selected_objects:
    i.pass_index = 3

that should get you started :slight_smile:

Thank You! Thank You! That helps me a lot! Awesome list :smiley:

One can only hope that this shows up in Blender Python notes (old link) http://www.blender.org/documentation/blender_python_api_2_56_4/ “how to” - oh, wait, that section/forum/thread/topic doesn’t exist. :slight_smile:

http://wiki.blender.org/index.php/Dev:2.5/Py/API/Intro