Python PRT file writer.


#1

I recently needed to generate some PRT files using python.
I thought I would share the bare bones needed to write a valid PRT file with python in case it helps someone else out in the future.

Obviously this isn’t very useful as it stands because it just creates 8 hardcoded particles in a cube shape, but it should be trivial to incorporate in your own program.

[code]import sys, struct, zlib

out = file(‘c:/test/data.prt’, ‘wb’)

out.write(struct.pack(“B”, 0xc0)) # Magic numbers
out.write(struct.pack(“B”, 0x50))
out.write(struct.pack(“B”, 0x52))
out.write(struct.pack(“B”, 0x54))
out.write(struct.pack(“B”, 0x0d))
out.write(struct.pack(“B”, 0x0a))
out.write(struct.pack(“B”, 0x1a))
out.write(struct.pack(“B”, 0x0a))

out.write(struct.pack("<I", 56)) # Header Length
out.write(struct.pack("@32s", “Extensible Particle Format”)) #Format Name
out.write(struct.pack("<I", 1)) # Version Number
out.write(struct.pack("<Q", 8)) # Particle Count
out.write(struct.pack("<I", 4)) # Reserved 4 bytes
out.write(struct.pack("<I", 1)) # Number of Channels
out.write(struct.pack("<I", 44)) # Channel definition entry length

Write Channel Definition

out.write(struct.pack("@32s", “Position”)) #Name of Channel
out.write(struct.pack("<I", 4)) # Data type
out.write(struct.pack("<I", 3)) # number of values per particle for this channel
out.write(struct.pack("<I", 0)) # Data offset of this channel

data = [(-9.74339,-10.2985,0.0),(9.74339,-10.2985,0.0),(-9.74339,10.2985,0.0),(9.74339,10.2985,0.0),(-9.74339,-10.2985,11.5985),(9.74339,-10.2985,11.5985),(-9.74339,10.2985,11.5985),(9.74339,10.2985,11.5985)]

z = zlib.compressobj()
for p in data:
for v in p:
x = (struct.pack("<f", v))
compressedData = z.compress(x)
out.write(compressedData)

compressedData = z.flush()
out.write(compressedData)
out.close()

[/code]