Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created July 28, 2012 19:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/3194438 to your computer and use it in GitHub Desktop.
Save zeffii/3194438 to your computer and use it in GitHub Desktop.
csv_based_animation.py
import bpy
import time
from mathutils import Vector
# purposely coded verbosely in places. relax :)
'''
about the data being processed here:
we have 11 elements per line. Per particle snapshot they inform us about:
Element[0] = track num
Element[1] = timestamp = num * 0.35ms (from the start)
Element[2,3,4] = Position Px,Py,Pz
Element[5,6,7] = Velocity in Vx, Vy, Vz
Element[8,9,10] = Acceleration in Ax,Ay,Az
'''
# setup reading location, and global variables
data_directory = '/home/yourname/Downloads/Physics/' # linux
# data_directory = 'C:/Users/yourname\Downloads/' # windows
datafile = 'test_data_trkflag_timestamp_1.txt'
lpt_name = "LPT_REP" # name of object for scene
# optional, use these to look at a subset of the data
skip_value = 6 # default 1, is full set
use_tokens = True # True = set a cutoff point, False = entire dataset
start_token = 00000 # start importing from this line, 0 for start
break_token = 80000 # don't import beyond this line or until dataset ends.
def printTimeStamp():
# divider + timestamp
print("\n" + "="*19 + time.ctime() + "="*19)
'''
def printVectorList(vec_list):
for item in vec_list:
print(item)
'''
def CreateMesh(num_param, data_set):
# debug prints, for flow control
debug_string = "num_param = " + str(num_param)
debug_string += " & data_set length = " + str(len(data_set))
print("Reaching CreateMesh with data: " + debug_string)
# make new mesh, add vertices using coordinates
Verts = []
for data_segment in data_set:
xfloat = float(data_segment[2])
yfloat = float(data_segment[3])
zfloat = float(data_segment[4])
unique_vertex = Vector((xfloat, yfloat, zfloat))
Verts.append(unique_vertex)
test_mesh = bpy.data.meshes.new("LPT_DATA")
test_mesh.from_pydata(Verts, [], [])
test_mesh.update()
new_object = bpy.data.objects.new(lpt_name, test_mesh)
new_object.data = test_mesh
scene = bpy.context.scene
scene.objects.link(new_object)
new_object.select = True
# looks like i could refactor CreateMesh and CreateWireBounds
def CreateWireBounds(box_coordinates):
print("making wire bounding_box object")
# printVectorList(box_coordinates)
# hardcode some edges
Edges = [ [0,1],[1,2],[2,3],[3,0],
[4,5],[5,6],[6,7],[7,4],
[0,4],[1,5],[2,6],[3,7]]
b_mesh = bpy.data.meshes.new("WIRE_BOX_DATA")
b_mesh.from_pydata(box_coordinates, Edges, [])
b_mesh.update()
box_object = bpy.data.objects.new("WIRE_BOX", b_mesh)
box_object.data = b_mesh
scene = bpy.context.scene
scene.objects.link(box_object)
box_object.select = True
def ConstructBoundingBox():
bpy.context.scene.update() # necessary sometimes
box_Vector = bpy.data.objects[lpt_name].dimensions
print(box_Vector)
# make bounding box 3dGrid
box_coordinates = bpy.data.objects[lpt_name].bound_box
CreateWireBounds(box_coordinates)
return
def MakeHistogram(pnum, ldata):
print("Entering MakeHistogram function:")
print("--Total data points stored: ",len(ldata))
# print number of unique tracks
# find longest track
# find shortest track
# print histogram of tracks & lengths
return
def InitFunction():
def isBeyondToken(line_num):
if use_tokens == False: return True
if line_num <= break_token: return True
else: return False
num_lines = 0
line_checking_list = [] # list to check for consistency
line_data_list = [] # list to append the various data values onto
dataset = open(data_directory + datafile)
# should be rewritten, it's not very pretty
for line in dataset:
if not isBeyondToken(num_lines): break # stop importing if beyond
if num_lines % skip_value == 0:
if (num_lines >= start_token) or use_tokens == False:
items = line.split(",")
line_checking_list.append(len(items))
line_data_list.append(items)
num_lines += 1
dataset.close() # to be polite.
# detect anomalies first, before getting hopes up.
set_check = set(line_checking_list)
if len(set_check) == 1: # means no variation
printTimeStamp()
print("Number of lines in original dataset: ",num_lines)
print("Skipping every",skip_value, "lines")
num_parameters = list(set_check)[0] # gives parameters per line
MakeHistogram(num_parameters, line_data_list)
CreateMesh(num_parameters, line_data_list)
ConstructBoundingBox() # manually for grid drawing
else:
print("There exists variance in the data, won't proceed")
print("At least one line contains unexpected data")
return
return
InitFunction()
@pinanunes
Copy link

Fantastic! (I know is 3 years old but still...Fantastic!)
I'm rather new in blender, and and my line of work is far from this field but this is inspirational for something I've wanted to do for some time. I would like to animate movements along roads (In my case animal transport movements from farm to farm). Would be possible in blender make the particles follow a path with a given id imported from SVG (the roads) starting with a given time at a given velocity between coordinates (batch starting time, road speed limits) imported from a CSV? Or do you have any experience in animating GIS data that you could give some tips on how to approach that? The end result wouldn't have to be so impressive as this (https://vimeo.com/110348926) but that is the general idea.
Keep up the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment