Writing particle point positions to a CSV file

One of the nice things about Python is the convenience of modules like CSV.

A point cloud is an X3DObject, so you could just get the point positions the same way you would for a mesh. But the recommended way to do it is with ICEAttribute.GetDataArrayChunk or, for small point clouds, ICEAttribute.DataArray.

import csv
from siutils import si		# Application
from siutils import sidict	# Dictionary
from siutils import log		# LogMessage

# Get a CSV writer object
# http://docs.python.org/release/2.5.2/lib/module-csv.html
csvWriter = csv.writer(open('cloud1.csv', 'wb'), delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)

# PointCloud is an X3DObject, so you can simply get points at current frame
# For large point clouds, use the ICEAttribute object instead
points = sidict.GetObject( "pointcloud" ).ActivePrimitive.Geometry.Points;
for p in points:
	#print "(%s, %s, %s)" % ( p.Position.X, p.Position.Y, p.Position.Z )
	csvWriter.writerow([p.Position.X, p.Position.Y, p.Position.Z])

# Use the ICEAttribute.DataArray for PointPositions
# For large point clouds, use ICEAttribute.GetDataArrayChunk
points = sidict.GetObject( "pointcloud" ).ActivePrimitive.Geometry.ICEAttributes("PointPosition").DataArray
for p in points:
	log( "%s, %s, %s" % (p.X, p.Y, p.Z) )
	csvWriter.writerow([p.X, p.Y, p.Z])

Update: The above script will write the CSV file in %XSI_BINDIR%. Changing the output location is an exercise left to the reader 😉 as is using GetDataArrayChunck (but for that you can copy the example in the docs).