Wednesday, March 12, 2008

Blender Materials and Python


I spent some time tonight working with Blender on an illustration of DNA. At first, I thought it would be straightforward... I soon realized there were many challenges. The biggest was setting up the nucleotides. They have to be four different colors, representing the pair A and T, as well as C and G. The pairs need to go together. Since there were so many of them, I thought this was a good time to do a little Python. After poking around in the APIs, I found where I could set the Object's material. I ran my script and nothing seemed to happen. As it turns out, Blender can associate a material with an object or a mesh. By default it associates with a mesh and it seems that the mesh material takes precedence over the object material. A small change to the code and I got it working. Here's the program:

import Blender
from Blender import Material, Mesh

objects = Blender.Object.GetSelected()
mat = Material.Get("T_Nucleotide")

for curObj in objects:
n = curObj.getData()
n.materials = [mat]
n.update()
curObj.layers = [5]

The code starts out with the import statements to use the Blender, Material, and Mesh modules. Next it gets all the selected objects (this way I could click on the ones I want to change). Then it sets a variable to the material I want to use. I loop through the list of objects getting the mesh data, then setting its materials. Note that the materials property is actually a list of materials, so I add the brackets around the material variable. I call update() so the screen is updated. Lastly, all the ones that were changed I move to layer 5. The layers property is list, this is a neat feature of Blender... unlike most graphics programs where an object can only reside on one layer at a time, in Blender you can set the object to appear on multiple layers. Turns out to be very useful.

4 comments:

  1. Hello There!
    I just wanted to write a short message of thanks for the helpful Python/Blender scripts on your site.
    I have been using Blender for a little while now, and have just started looking into Python scripting, after working with Max/MSP, scripting with Javascript.
    I'm studying Sound and Music Tech in Plymouth, UK, and am doing some work with Cellular Automata in Evolutionary Music - With the help of your Materials and Object repetition scripts, I've managed to write a script that makes 1-D Cellular Automata!
    So far this will be useful for video footage to accompany my C.A. Music, and if I can learn how, maybe integrate my Blender work into Max/MSP??
    Thanks again! It was REALLY helpful to have a simple and well documented script to learn from!
    If you'd like a copy of the C.A. script, post me a note and I'll email it. All the best with the DNA work.......Graham

    ReplyDelete
  2. Graham,

    Thank you for the kind comments! I am so glad to hear my blog/scripts were helpful. I would love to see your script on Cellular Automata!

    Regards,
    John

    ReplyDelete
  3. Just got started with Blender and a bit of Python. I haven't used any before.

    My quick thoughts are:
    Blender - forget any shortcut,UI you might be tempted to relate to Blender and you'll be fine :)
    Python - this one's different as well, but it's less code to do more stuff.

    I stumbled upon this post while looking at how to set a material. I made a Plane, loaded an Image, assigned it to a Texture, set the texture to a new Material.
    Just by looking at the API I thought I could do

    p.setMaterials([planeMat])

    where p is a plane and planeMat is the described material. That didn't work. I didn't get any error, but no material was created for the object.
    What am I missing ?

    your method with the getData worked straight away :)

    ReplyDelete
  4. Did you try
    p.materials = [planeMat]

    ReplyDelete