Hey everyone,

Today I am going to show you, how you can change settings globally. You want to make all materials in your blend file shadeless? Or add a modifier to all objects? Instead of doing that manually (which can take a lot of time) you can write a few lines of code, and you’re done! Curious? Here is the answer!

Don’t worry, you don’t need any special knowledge, though some basic understanding of Python can be useful here.

First of all, open up the Text Editor space inside Blender (and a Python Console, which is useful sometimes). Add a new text and start scripting!

1: Change a setting for all materials

You want to change a material setting, but not only for a single material but for all in your file:

# import the Blender module
import bpy

#Iterate over all members of the material struct
for item in bpy.data.materials:
    #Enable "use_shadeless"
    item.use_shadeless = True

Type the lines above into the Text Editor and press the button “Run script”. You don’t have to type the comments (lines starting with # are comments inside the code) It will make all your materials shadeless! Easy isn’t it?

You can do the same with all other settings, just replace the last line.

Check the “Python” text inside the tool tip to get the name of a property. Some examples:

#Set the material Emit value to 1
 item.emit = 1
#Change diffuse color to red
 item.diffuse_color = (1,0,0)
#Change diffuse shader to toon
 item.diffuse_shader = 'TOON'

You can also make more than one change at the same time, just add a new line for each setting:

#Enable transparency and change method to Raytrace
 item.use_transparency = True
 item.transparency_method = 'RAYTRACE'

2: Add a Subdivision Surface Modifier to all Mesh objects

import bpy

#Iterate over all members inside the Object struct
for ob in bpy.data.objects:
     #Check if object is a Mesh
     if ob.type == 'MESH':
         #Add a SUBSURF Modifier with the name "My SubDiv".
         ob.modifiers.new('My SubDiv', 'SUBSURF')

3: More examples

import bpy

#Iterate over all members inside the Object struct
for ob in bpy.data.objects:
     #Display the object's name
     ob.show_name = True

     #Move all objects by 1 Blender Unit in Z direction
     ob.location[2] += 1

4: More information

If you have any question, feel free to leave a comment and I will try to answer it.
You can get more information about the Blender scripting API here.

I hope you find this article useful!

copied from dingto.org