Scripting – Change settings the fast way

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

The Future of Overrides
This Summer’s Sculpt Mode Refactor
Geometry Nodes Workshop: October 2024
New Brush Thumbnails

14 comments
  1. Why I can’t use (.name or .diffuse_color) in a loop like 3th example?

    for o in bpy.data.objects:
    material= o.active_material.diffuse_color
    color_name = o.active_material.name
    – – Console – –
    >>> AttributeError: ‘NoneType’ object has no attribute ‘diffuse_color’
    >>> AttributeError: ‘NoneType’ object has no attribute ‘name’

    how is the correctly way of use this attribute?

    • if this is “active” you need to be in context not data…

  2. oh, nevermind my question – I already did it ;)

  3. how to code that every selected object will have added bevel just to its bottom part regardless of objects’ actual rotation (where optically for example left side could become bottom side but I want to have it on it original bottom side…or idealy add bevel on every edge except the studa), is something like this even possible, please? what I want to achieve is seam on bricks…I am loading LEGO sets to Blender but need to achieve this effect of real bricks which has visible tiny seam at their connection points…

  4. I came to your “Scripting – Change settings the fast way | Blender Code” page and noticed you could have a lot more traffic. I have found that the key to running a website is making sure the visitors you are getting are interested in your subject matter. There is a company that you can get traffic from and they let you try it for free. I managed to get over 300 targetted visitors to day to my website. Check it out here: http://voxseo.com/traffic/

  5. @JONATHAN

    Here is one example if you want to iteratively change Modifier Subdiv value for currently selected mesh.

    But this is a bit hardcoded to modifier named ‘Subsurf’.

    import bpy

    selected_objects = bpy.context.selected_objects

    for object in selected_objects:
    object.modifiers[‘Subsurf’].levels = 3

  6. great Thomas Just what I was looking for, but I’m stuck with changing ior,
    item.MaterialRaytraceTransparency.ior = 1.33
    doesn’t work – have I made a silly mistake

  7. hi i know this is an old post, but is there a way to edit the modifiers through python script? I cant seem to find much on it. And example would be loop through all objects and change subsurf subdivisions to 2.

  8. can u provide a sample script for changing the render settings, like render dimensions, file format, start and end frames, etc.,

  9. Thanks a lot for the link WASA !

  10. ARTORP:
    for ob in bpy.context.selected_objects:
    …..
    :)

  11. I’ve been wanting to learn python in blender for years. Where do I start? Is there a blender specific object or command reference showing how blender is connected to python?

  12. For those that want to learn a bit more about python 3.x (the version used by Blender 2.5.x) here is a very helpful guide.

    http://openbookproject.net/thinkcs/python/english3e/index.html

  13. Awesome, I see some basic python scripting can be very useful for efficient workflow. How would I go about determining which objects I have selected? I can imagine that would be useful for most scripts, since then I could limit the affection. :)

In order to prevent spam, comments are closed 7 days after the post is published.
Feel free to continue the conversation on the forums.