HOM Particle Cache Example: Writing Particle Data to a Custom Format

Overview

This example illustrates how to write out a particle simulation output to a custom file format.

Location

Supporting files for this example are in $HFS/mozilla/documents/hom/cookbook/particle_cache, also found in the cookbook/particle_cache directory of cookbook_files.tar.gz.

Getting Started

Load , start up a Python shell, and run hou.session.writeFireworks() to save the particles in a POP network to a file named fireworks.txt.

particle_cache’s hou.session module contains the following code:

def writeParticlesAsGeo(popnet, file_name):
    popnet.geometry().saveToFile(file_name)

def writeParticlesInCustomFormat(popnet, file_name):
    """Write the particle data in the geometry geometry to a custom file
       format."""
    geo = popnet.geometry()
    f = file(file_name, "w")

    # Write out the number of particle attributes, followed by the particle
    # attribute information.
    f.write("%d attributes:\n" % len(geo.pointAttribs()))
    for attrib in geo.pointAttribs():
        f.write("%s %s %d\n" %
            (attrib.name(), attrib.dataType(), attrib.size()))
    f.write("\n")

    # Write the number of particles, followed by the particle attribute values.
    f.write("%d points:\n" % len(geo.points()))
    for point in geo.points():
        for attrib in geo.pointAttribs():
            f.write("%s = %s\n" % (attrib.name(), point.attribValue(attrib)))
        f.write("\n")

    f.close()

def writeFireworks():
    writeParticlesInCustomFormat(
        hou.node("/obj/particle_object1/popnet1"), "fireworks.txt")

Sample output:

8
P attribData.Float 4
v attribData.Float 3
accel attribData.Float 3
life attribData.Float 2
pstate attribData.Int 1
id attribData.Int 1
parent attribData.Int 1
Cd attribData.Float 3
2
Float 3

2 points:
P = (0.0, 0.0, 0.0, 1.0)
v = (0.44430351257324219, 1.0430929660797119, 0.008625030517578125)
accel = (0.0, -0.36000001430511475, 0.0)
life = (0.0, 3.3668022155761719)
pstate = 0
id = 1
parent = 0
Cd = (1.0, 1.0, 1.0)

P = (0.0, 0.0, 0.0, 1.0)
v = (0.018614673987030983, 0.05000000074505806, -0.12590165436267853)
accel = (-0.026060543954372406, -0.43000000715255737, 0.17626231908798218)
life = (0.0, 1.3654334545135498)
pstate = 0
id = 2
parent = 1
Cd = (1.0, 0.90333300828933716, 0.0)