Converting Scientific Data to Blender Voxel or Point Cache

I’m looking for help in writing a blender readable voxel or point cache file. I’ve looked at the generated .bphys files, and I’ve read this website .

I can deal with converting the raw data (from an excel file) into the blender readable format, but I really need to see an example of the formatting or syntax of a voxel or point cache file.

I assume you can write these files in a text editor and open them using the “external” cache option. Is there more to it? :confused:

Thanks.

Moved from “General Forums > Blender and CG Discussions” to “Coding > Python Support”

I found this script for writing a blender voxel file but it doesn’t work. Any ideas?

#! /usr/bin/env python

Writes a Blender Voxel accepting this format:

----------------------------

X Y Z

data data data data data

----------------------------

Where X Y Z are the dimentions (separated by spaces)

data is a float between 0.0 and 1.0

Data is read by Blender in this manner:

Z * resY * resX + Y * resX + X

So write your data line like this for a 3 x 3 voxel box:

(0, 0, 0) (1, 0, 0) (2, 0, 0)

(0, 1, 0) (1, 1, 0) (2, 1, 0)

(0, 2, 0) (1, 2, 0) (2, 2, 0)

(0, 0, 1) (1, 0, 1) (2, 0, 1)

(0, 1, 1) (1, 1, 1) (2, 1, 1)

(0, 2, 1) (1, 2, 1) (2, 2, 1)

(0, 0, 2) (1, 0, 2) (2, 0, 2)

(0, 1, 2) (1, 1, 2) (2, 1, 2)

(0, 2, 2) (1, 2, 2) (2, 2, 2)

Remember to write these all on 1 line in a constant row

import sys, os
from struct import *

def main():
if len(sys.argv) < 2:
print(“Usage: " + sys.argv[0] + " <file>”);
return

SOURCE = sys.argv[1]

if not os.path.exists(SOURCE):
	print("Can't find source file " + SOURCE)
	return

source = open(SOURCE, "r")

destination = open('./voxels.bvox', "wb")

lines = source.readlines()

X, Y, Z = lines[0].split(' ')

destination.write(pack('i', int(X)))
destination.write(pack('i', int(Y)))
destination.write(pack('i', int(Z)))
destination.write(pack('i', int(50))) # 1 frame

for value in lines[1].split(" "):
	if value != '

':
destination.write(pack(‘f’, float(value)))

destination.close()
source.close()

print("Success.")

main()

In Python indentation is very important. Could you copy/paste the script within

 tags to keep indentation and formatting correct?

Note: The instructions say open command prompt, navigate to directory containing this python and a .txt containing the data to be converted, type: >bvox.py data.txt

I press enter nothing happens. Now I’m going through Blender Cookie’s python basics tutorials.

import sys, os
from struct import *

def main():
if len(sys.argv) < 2:
print(“Usage: " + sys.argv[0] + " <file>”);
return

SOURCE = sys.argv[1]

if not os.path.exists(SOURCE):
	print("Can't find source file " + SOURCE)
	return

source = open(SOURCE, "r")

destination = open('./voxels.bvox', "wb")

lines = source.readlines()

X, Y, Z = lines[0].split(' ')

destination.write(pack('i', int(X)))
destination.write(pack('i', int(Y)))
destination.write(pack('i', int(Z)))
destination.write(pack('i', int(50))) # 1 frame

for value in lines[1].split(" "):
	if value != '

':
destination.write(pack(‘f’, float(value)))

destination.close()
source.close()

print("Success.")

main()

#! /usr/bin/env python

# Writes a Blender Voxel accepting this format:
# ----------------------------
# X Y Z
# data data data data data
# ----------------------------
# Where X Y Z are the dimentions (separated by spaces)
# data is a float between 0.0 and 1.0
#
# Data is read by Blender in this manner:
# Z * resY * resX + Y * resX + X
#
# So write your data line like this for a 3 x 3 voxel box:
# (0, 0, 0) (1, 0, 0) (2, 0, 0)
# (0, 1, 0) (1, 1, 0) (2, 1, 0)
# (0, 2, 0) (1, 2, 0) (2, 2, 0)
#
# (0, 0, 1) (1, 0, 1) (2, 0, 1)
# (0, 1, 1) (1, 1, 1) (2, 1, 1)
# (0, 2, 1) (1, 2, 1) (2, 2, 1)
#
# (0, 0, 2) (1, 0, 2) (2, 0, 2)
# (0, 1, 2) (1, 1, 2) (2, 1, 2)
# (0, 2, 2) (1, 2, 2) (2, 2, 2)
#
# Remember to write these all on 1 line in a constant row

import sys, os
from struct import *

def main():
	if len(sys.argv) &lt; 2:
		print("Usage: " + sys.argv[0] + " &lt;file&gt;");
		return
	
	SOURCE = sys.argv[1]
	
	if not os.path.exists(SOURCE):
		print("Can't find source file " + SOURCE)
		return

	source = open(SOURCE, "r")
	
	destination = open('./voxels.bvox', "wb")
	
	lines = source.readlines()
	
	X, Y, Z = lines[0].split(' ')

	destination.write(pack('i', int(X)))
	destination.write(pack('i', int(Y)))
	destination.write(pack('i', int(Z)))
	destination.write(pack('i', int(50))) # 1 frame
	
	for value in lines[1].split(" "):
		if value != '
':
			destination.write(pack('f', float(value)))
	
	destination.close()
	source.close()
	
	print("Success.")

main()

I’m basically starting from the bottom with intro to python tutorials. Anyway, using Eclipse with PyDev installed I get the following error when running this script:

“Unused in wild import: Struct, calcsize, error, pack_into, unpack, unpack_from - location: Line 29.”

So, it doesn’t like "from struct import * "

That’s all I got.