Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

May 26, 2011

Blender 2.5 Python 3.2 Intro 1


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']

setting a pass index for all selected objects

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 :)