OpenVDB  7.2.1
AX Code Examples

Contents

Points Examples

These examples demonstrate how to use AX on VDB points grids.

Point Attributes Example

This example shows a couple of ways to use AX with points attributes. The @ symbol is the identifier for a point attribute. The type of each attribute is specified before the @ symbol and the name is specified afterwards. For example: int@count would imply an integer attribute called count.
In this example there are three point attributes: a float attribute speed, a vector3 float attribute velocity and a vector3 float attribute colour.
This snippet uses the functions length and fit to give points a colour between black and white based on their speed.
// This statement writes to a new float attribute called speed and reads from a
// vector float attribute called velocity. The length function is used here to
// return the length of the point's velocity vector.
float@speed = length(vec3f@velocity);
// Points that move faster than this threshold will all be mapped to the same
// colour (white).
float fast_threshold = 6.f;
// Writing a single value to a vector will mean that all components of the
// vector are assigned the same value.
vec3f@colour = fit(float@speed, 0, fast_threshold, 0, 1);
The velocity attribute in this example is assumed to exist and have a range of values to produce some visual variance in colour. The attributes speed and colour do not need to exist before this snippet is run as the attributes are only written to and will be created on the fly.
Note. In Houdini (AX SOP) changing the name of the attribute from colour to Cd will result in the points being given a colour in the viewport as Cd is a special attribute.

Delete Points

This example demonstrates a simple way of removing points from a VDB points grid. It uses a combination of axrand and axdeletepoint to randomly delete roughly half of the points in the input VDB points grid.
float threshold = 0.5;
// The rand function takes a seed and produces a random value between 0 and 1.
// The integer attribute id from the points is used to seed the rand function.
// If the value from rand is greater than the threshold (0.5) then delete the point.
if (rand(i@id) > threshold) {
deletepoint();
}
The id attribute in this example is assumed to be a unique integer for each point in the VDB points grid such that axrand will receive a different seed value per point producing a good distribution of random values.

Drag Example

This example shows something a bit more complex: modifying a point's velocity with a drag force and then moving the point with the modified velocity.
float dt = 1.0f / (4.0f * 24.0f); // timestep
vec3f gravity = {0.0f, -9.81f, 0.0f}; // gravity
vec3f dV = {2.0f, 0.0f, 0.0f} - v@v; // drag
float lengthV = length(dV);
float Re = lengthV * @rad / 1.225f;
float C = 0.0f;
if (Re > 1000.0f) C = 24.0f / Re;
else C = 0.424f;
// calculate drag force
vec3f drag = 0.5f * 1.2f * C * lengthV * dV * 4.0f * 3.14f * pow(@rad, 2.0f);
// update velocity
v@v += (gravity - drag / ((4.0f / 3.0f) * 3.14f * pow(@rad, 3.0f))) * dt;
// update position
v@P += v@v * dt;


Volume Examples

These examples demonstrate how to use AX on VDB volume grids.

Volume Assignment Example

This example shows a couple of ways to use AX with volumes. The @ symbol is the identifier for a volume. The type of each volume is specified before the @ symbol and the name is specified afterwards. For example: float@density would imply a float volume called density.
Unlike when working on points AX will not create new volumes if they don't already exist when written to in a snippet. This snippet uses axclamp to constrain the density attribute between zero and one.
float@density = clamp(float@density, 0.f, 1.f);
The density attribute in this example is assumed to exist already.