I’m trying to use Blender as a visulisation tool for another program. I have a text file which has the positions of multiple objects over many frames. The format is basically this :
Object 1 x,y,z frame 1
Object 2 x,y,z frame 1
Object 3 x,y,z frame 1
Object 1 x,y,z frame 2
Object 2 x,y,z frame 2
Object 3 x,y,z frame 2
And so on. (In fact what I have is a list of 250 random postiions, just for proof-of-concept that the script will work). So I am trying to do the following :
- Use the first 25 lines of the file to generate 25 spheres at the correct positions and set IPO keys. This bit works !
- Advance the frame (OK !) and use the next 25 lines to update the object positions.
- Repeat 2 until done.
I’m completely stuck on stage 2. Not knowing quite how to have Blender select the appropriate object to update (perhaps an array of object names generated along with the meshes ? Haven’t figured this out yet either !), I tried to have it just update one sphere, so it should be getting its positions from lines 1,26,27,28… and so on. What it actually comes back with the positions from lines 1,50,75,100… which is obviously not right. I think there are probably simple ways to do this, I just can’t figure out what. The current script is below.
Hope someone can figure out what I’ve done wrong… I can attach the text file if need be.
import Blender as B
from Blender import *
def create_meshes( line ):
# chop up line into name and coordinates
x, y, z, r, XR, YR, ZR = line.split()
scn = B.Scene.GetCurrent()
subD=2
diameter = float(r)*2
me=Mesh.Primitives.Icosphere(subD,1.0)
object=scn.objects.new(me,'Halo')
object.setLocation(float(x), float(y), float(z))
object.insertIpoKey(B.Object.LOC)
object.setSize(diameter, diameter, diameter)
object.insertIpoKey(B.Object.SIZE)
object.Layer=1
diameter=float(r)
me=Mesh.Primitives.Plane(1.0)
ob=scn.objects.new(me,'Disc')
ob.setLocation(float(x), float(y), float(z))
ob.setSize(diameter, diameter, diameter)
ob.Layer=2
ob.RotX=(float(XR))
ob.RotY=(float(YR))
ob.RotZ=(float(ZR))
ob.insertIpoKey(B.Object.LOC)
ob.insertIpoKey(B.Object.SIZE)
def move_meshes( line ):
x, y, z, r, XR, YR, ZR = line.split()
scn = B.Scene.GetCurrent()
#for n in range (1,nh+1): (This will be necessary to do all objects)
ob=B.Object.Get('Halo') # Somehow have to read in appropriate objects
ob.setLocation(float(x), float(y), float(z))
ob.insertIpoKey(B.Object.LOC)
ob.setSize(float(r), float(r), float(r))
ob.insertIpoKey(B.Object.SIZE)
#
# main
#
infile = open('250Halos2.txt', 'r')
nh = 25 #No. halos
nf = 10 #No. frames to read in
mf = 10 #Blender frame increment per frame increment of text file
frame=B.Get('curframe')
if frame == 1: # Must be on frame 1 for initial conditions
try:
for i in range (1,nh+1): # nh+1 due to silly Python convention
line = infile.readline()
create_meshes( line ) # Use first nh lines to create meshes
except Exception, e:
print 'Oops!', e
# This part reads in the next nh lines nf times(not nf+1 since have already
# done once)
for n in range(1,nf):
B.Set("curframe",frame+mf)
frame=frame+mf
try:
for i in range (1,nh+1): # Starts from next line after the last read.
line = infile.readline()
move_meshes( line ) # Use appropriate lines to move meshes
except Exception, e:
print 'Oops!', e
B.Redraw(-1)
# Pseudocode :
# Frame 1. Create nh spheres and planes of correct size and set IPOs.
# Halos are on layer 1 and discs are on layer 2. This all works.
# Next get all spheres and planes and update positions based on next nh
# lines in text file. For now just deals with first sphere.
# Since readline should remember previous line, for loop should work as
# is.
# Problems : Find what lines are actually being used for coords.
# 1, 50, 75, 100, 125, 150, 175...