088: Boids with Script SOP

boids1_small.jpg

Note: the most recent version of this file can be found here:

BoidsWithSOPs is another example usage of the Script SOP. Here I am using the numpy library (which is included in the TouchDesigner088 installation) to apply rules derived from code here based on Craig W. Reynolds’ description of boid systems.

Of note here is the Table DAT called controls holding parameters that can be adjusted.
cohesionScale, alignmentScale and targetScale are reversed meaning larger values equal to a lesser effect.

To start things up, all point positions and the values for a Custom Attrib (added in a Point SOP) are read into a numpy arrays:

# add points and velocity into empty numpy array
oldPoints = np.empty([len(points),3])
oldVelocity = np.empty([len(points),3])

count = 0
for i in points:
	oldPoints[count]=[i.x,i.y,i.z]
	oldVelocity[count]=[i.Z[0],i.Z[1],i.Z[2]]
	count += 1

After looping through all points and determining their new velocity the result is added back on to the point position and velocity is stored as an attribute. In order to also rotate them correctly, the velocity is applied as the Normal to the point:

# update point location and save velocity into Z
p = myPoints[j]
p.P += rules
p.Z = rules
p.N = rules

Note: This component requires TouchDesigner088
BoidsWithSOPs.tox (5.37 KB)

Same idea with a little bit more of user control :wink:

is it normal that the script SOP takes ~14 ms for this rather small amount of points?

Hey! So i was trying 088 scripting and found this interesting, i love boids!

The script doesn’t work for me unless you rename ‘points’ array to something else and will directly grab sop content by EG

points = scriptOP.copy(op('point1')) 

instead of

scriptOP.copy(scriptOP.inputs[0])

2 Derivative - my python skills are kinda fresh, two days at all =), but it looks like there were changes at some point in scriptSOP method calling fields for similar reason (is this to avoid variable vs method names override or something else)?

Because I see /geometry_examples/script1_script in old PythonExamples.toe calling scriptop.appendPoly with points=true instead of addPoints=true and that doesn’t work in my build (11760) unless fixed.

Cool thanks, this is awesome!
https://www.youtube.com/watch?v=nCx_6PPSTzI#t=39

Yes, there were some recent changes to clean up syntax. There is a new Python example file for you to refer to found here:
/Samples/Python/PythonExamples.toe

Hey,

an updated version has been added to the initial post.

cheers
Markus

I had some fun with this. No optimisation. The oscillator CHOP is the big fat CPU-sucker in this case. :slight_smile:

rod.
BoidsWithSOPsRodModAudio.4.toe (5.81 MB)