Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Thursday, March 27, 2008

DNA script


After writing the script I posted in the previous blog, it occurred to me that I should be able to create all the nucleotides with a script, not just color them. So I poked around in the Blender Python APIs and came up with the following:
import random, math
import Blender
from Blender import Object, Scene, Material

scn = Scene.GetCurrent()
colors1 = [Material.Get("G_Nucleotide"), Material.Get("C_Nucleotide")]
colors2 = [Material.Get("A_Nucleotide"), Material.Get("T_Nucleotide")]
groups = [colors1, colors2]
for i in range(0, 90):
rot = i*.125*math.pi
o = scn.objects.new(Object.Get("nucleotide").getData())
o.colbits = 1
o.modifiers = Object.Get("nucleotide").modifiers
o2 = scn.objects.new(Object.Get("nucleotide").getData())
o2.colbits = 1
o2.modifiers = Object.Get("nucleotide").modifiers
groupNum = random.choice([0,1])
firstColorNum = random.choice([0,1])
if firstColorNum == 0:
secondColorNum = 1
else:
secondColorNum = 0
firstColor = groups[groupNum][firstColorNum]
secondColor = groups[groupNum][secondColorNum]
o.LocZ = i*.2
o.RotZ = rot
o.setMaterials([firstColor])
o2.LocZ = i*.2
o2.RotZ = rot + math.pi
o2.setMaterials([secondColor])
scn.update()

Python is one of the more unique languages I've worked with. After my initial shock at being forced to indent, I've now found I like it. I'd do it anyway, but now it is sure to be done and done consistently. It makes for reading my code and others' code much nicer. As I get used to syntax and familiar with the Python and Blender APIs, I find I am looking for more excuses to use it. :)

For one of my future courses, I'd like to teach how to program... using Python as the language and Blender as the environment. This would be for folks who want to learn to program, but haven't a clue were to start. It's nice to be able to solve some of the more tedious tasks (like creating and coloring nucleotides) programmatically.

Another course I'd like to teach is one on the Blender Game Engine. With the logic bricks, you can do a lot of programming logic without getting into the syntax issues you get in a language. A cool thing in Blender is you can extend the logic bricks by calling Python scripts.

About the image: The image above has over 1200 objects, but only one mesh. Each object reference the same mesh (making changes easy). I applied the Material color to the object (not the mesh, which is the default). By using a script, I was able to apply the colors randomly but following the rules that the A color had to go with T (or T with A) and the G color had to go with C (or C with G). Since all the nucleotides of the same color share the same Material, color and texture changes are also easy.

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.

Tuesday, January 15, 2008

Sandy and Blender

Click on the above image to see a Flash animation. I modeled figure above in Blender, exported to ActionScript 3 using Dennis Ippel's Python script, and then rendered it in Flash using Sandy. It was coded using FlashDevelop. All done with open source!

Tuesday, December 25, 2007

How I spent my Christmas vacation


It was only a four day weekend this year, but I still managed to slip in some productive fun. Over the weekend I:
  • Wrote my first productive Python script from scratch
  • Created a game in Blender using Blender's game engine
  • Painted in ArtRage (the picture above, you didn't think I was in that did you?)
  • Spent time in Alice
  • Opened a Twitter account
I also did a lot of last minute shopping, visited family, and shoveled snow (lots of snow!). And of course, thanking the Lord for sending his son, Jesus.

Sunday, December 23, 2007

Python and Blender

I was just working on a Blender project where I have 36 objects that I should have set the rigid body property for each. It took several minutes to do manually, was very tedious, and made me realize it was time to learn how to script Blender.

Blender is scriptable through the Python language. I've never scripted Blender and I've never used Python, so it took me a bit... Figuring out the appropriate Blender APIs and then learning the Python syntax (it is very different from what I'm used to like AS3, C# and Java). Here's my tiny, first script:
import Blender

objects = Blender.Object.GetSelected()

for curObj in objects:
curObj.rbFlags = curObj.rbFlags | Blender.Object.RBFlags.RIGIDBODY

Taking it apart, the first line imports the Blender module for the Blender APIs. The second line gets all of the selected objects in a scene using the Object module's GetSelected() method. The third and fourth lines iterate through the list of objects. Line 4 uses the bitwise OR operator to set the RIGIDBODY flag for the current object.

It took me a while to figure this little script out, longer than it took to do the operation manually. But the next time I need to set a property of a large number of objects -- any property, bitwise or otherwise -- it'll take seconds to type instead of minutes in the interface.