Using Blender/Python script for scientific purposes.

Hi everyone,
I know I am writing on website called “Blender Artists”… But science and art can be close to each other sometimes…

I was wondering if one of you could help me to achieve a very simple script (for you…) in order to help me with some scientific issues.

The script should be able to read a file containing 3D positions (Xi,Yi,Zi) and a scalar value (Ai) for each one of those points.
To start with, I would like to draw a sphere at each one of these positions (Xi,Yi,Zi), and the color of this sphere should be somehow proportional to the scalar value (Ai).

I really think that for some of you this might be super easy… For me it is not… And I think this could help me to start !!!

If I succeed, I definitely think that I could prove you that art and science can go together!
Thanks a lot for your help,
Best regards,
Mathieu

For reading a file, look at the python docs or google. There are tons of examples. But quickly:

If your file looked like:


1.2, 1.3, 1.0, .5
1.1, 1.2, 1.4, .7
1.4, 1.9, 1.6, .2

Then to read it in you’d do something like (untested code, but it shouldn’t be too hard to work out if its not correct):


fo = open("myDataValueFile.txt", "r")
points = []
for line in fo.readlines():
     vals = line.split(',')
     for i, n in enumerate(vals):
          vals[i] = float(n)
     points.append(vals)

Once doing more Blender centric work, use http://www.blender.org/documentation/248PythonDoc/API_intro-module.html as a reference. Two module you’ll probably want to become familiar with are MeshPrimitives and Materials.

MeshPrimitives will allow you to easily create a sphere mesh. You can then easily add that to an object and set the objects to position to where you need it.

Materials is going to allow you to change your color. I would just create a base material, and then copy it for each level you need, adjusting the color factor by the ratio of Ai to the maximum value of Ai. After that, just add the material back to the mesh you’ve created. It’d be something like:


baseMat = Material.Get('MyBaseMaterial')
for p in points:
     me = MeshPrimitives.UVsphere(10, 10, 2)
     nMat = baseMat.__copy__()
     newColorR = baseMat.R * myScaleFactor # myScaleFactor defined based on Ai
     # link nMat back to me
     # create a new object with me, and link it to the scene
     scn.link(me, 'UvSphere')

All of that may have been a little hand waivy, but I suggest looking through those docs as well as looking through some of the example templates in Blender.

Is there anyway to modify this code so it simply links the new mesh (me) primitive to an object that is already in the scene?

For example:
Say you have the above code and it produces an object called “UvSphere”. Let’s assume the default Cube is still in the scene. What kind of code could I use to link the mesh of UvSphere to the object of the default cube? In the end there would be two objects in the scene, one named “Cube” and the other named “UvSphere” but they would both be displaying a sphere in the scene.


Psuedo Code:
myCubeOBJ = Blender.Get('Cube')
myCubeOBJ.Mesh = UvSphere.Mesh

is there a way to use the duplivert to apply and repeat an object to all verticies on a mesh
then with a script modify the color function of the scalar value?

does this looks like voxels ?
can this program be used to help here ?

Thanks

@Atom: Use the link function.


myCubeObj = Object.Get('Cube')
myUVMesh = UvSphere.getData(mesh = True)

# Technically you should check that myCubeObj is a mesh object before linking
myCubeObj.link(myUVMesh)

Try Running this code with a Cube (you’ll have to Tab into edit mode to see the update):


import Blender
import bpy
from Blender import Mesh, Object

scn = bpy.data.scenes.active
me = Mesh.Primitives.UVsphere(10, 10, 2)
scn.objects.new(me, 'UvSphere')

ob = Object.Get('Cube')
ob.link(me)

Blender.Redraw(-1)

@RickyBlender

My guess is yes, but I’m not 100% certain on what you’re asking.

when i talked about duplivert i was thinking inside python directly
not in blender per say

so are there any api call which allow the duplivert to be executet inside python?

any ref on this

mind you might be useful for the guy trying to do it also !

Thanks

http://www.blender.org/documentation/248PythonDoc/Object-module.html#Duplicate

?? :spin:

@forTe: I figured it out. You have to make a call to “makeDisplayList()” if you are completely swapping a mesh with another. This is a little different than linking in a “new” mesh or editing/updating the existing mesh data.


myObject.makeDisplayList()