Particle Connect with Distance and Density Control
This is a simple MEL script that draws a line between two particles if their distance is less than a user defined value. I added a density control to reduce the amount of duplicate connections by skipping particle distance checks. This increases execution speed but reduces accuracy.
I started to sketch out the logic. The pseudo code on the right hand side is what I ended up going with.
The logic is a simple nested for loop that checks the distance of the current particle to all other particles in the system. This happens for every particle in the system on any given frame. If the distance is less that the user defined threshold, a linear two point NURBS curve is drawn between the two particles. This happens on every frame. A visibility key is set to only display the current frame’s calculated connections.
Here is main loop:
for ($i = 0; $i < $totalParticles; $i++)
for($j = 0; $j < $totalParticles; $j++)
if(fmod($j, $connectionDensity) == 0) //this check increases execution speed but reduces accuracy
//Get particle positions
vector $pos1 = `particle -attribute position -id $i -q $myParticle`;
vector $pos2 = `particle -attribute position -id $j -q $myParticle`;
vector $result = $pos1 - $pos2;
float $distance = mag($result);
if($distance < $distanceThreshold)
$line = `curve -d 1 -p ($pos1.x) ($pos1.y) ($pos1.z) -p ($pos2.x) ($pos2.y) ($pos2.z) -k 0 -k 1`;
//Group lines for organization
parent $line $linesGrp;
I made a GUI to help the user dial in the desired distance and density variables. The current frame connections can be drawn and deleted. Every frame within the range slider can be drawn and deleted.
Finally I added curve render attributes for rendering with Arnold
The idea for this script came to me while researching fluid solvers written in Processing. Some great examples like this can be found at BlueThen.
Here are two ideas for optimizations to reduce file size and execution time.
Currently this script creates duplicate connections resulting with lines on top of lines. Adding a check to prevent duplicate connections will lighten the scene by reducing the amount of lines drawn. It may or may not reduce execution time.
Spatial partitioning will reduce the total amount of distance calculations needed for accurate results. Instead of checking every particle in the system this optimization will only check particles within a defined partition. A 3d fluid container could define a set of spatial partitions. Then each particle will only calculate the distances from itself to the other particles within its partition.