Python Vs. TouchDesigner

Hi Forum.

So I picked up a copy of the Generative Design book, and I’ve been trying to recreate the Intelligent Agent script (P.2.2.2) as a pure TD OPs network. It’s not so easy. Yes, I downloaded the .toe file that recreates it with Python, but my thought was that TD is meant to be a visual programming language, so it should be able to replicate simple programs like this one using the built-in OPs, right?

I am wondering if there is something fundamentally different about programming with nodes that makes it impossible (or just a bad idea) to make complex behavior using OPs? Like the way the script steps through and executes each line of code, or how parts of the script loop back and change itself.

So, I guess what I’m doing is trying to maximize my general TouchDesigner skill before I start using Python to solve everything. But maybe that’s not necessary…

Thanks for any direction you guys can give me!

Ian

I dont know the example you’re referring to, mind explaining what it does more?

Generally our procedural networks (by themselves) are optimized for one way data flow, but scripts and higher order languages have a natural advantage in implementing loops, recursion and feedback.
You can setup feedback using Feedback CHOPs, or Execute DATs that push data back to the start of a network on specific events, etc, if you want to stay away from scripting solutions.
Replicator Components, and Copy SOP stamp() functions probably are our closest analog to loops and iterations.
Hope that helps,
Rob.

Hi Elburz, this is the script I am referring to:

It is a simple line that stretches out and changes direction when it hits the border or hits a previously made line.

[code]# me is this DAT.

scriptOP is the OP which is cooking.

import random
def cook(scriptOP):

xOld = scriptOP.par.value0x
yOld = scriptOP.par.value0y
drawMode = scriptOP.par.value1x
angle = scriptOP.par.value1y
direction = scriptOP.par.value1x
stepSize = scriptOP.par.value1z

scriptOP.clear()

scriptOP.numSamples = 1

x = scriptOP.appendChan('x')
y = scriptOP.appendChan('y')

for i in range(stepSize):
	#draw dot at current pos:
	newx = xOld + math.cos(math.radians(angle))
	newy = yOld + math.sin(math.radians(angle))

	scriptOP.par.value0x = newx
	scriptOP.par.value0y = newy
	
	border = False
	if newy >= 200:
		direction = 2
		border = True
	elif newx >= 200:
		direction = 3
		border = True		
	elif newy <= -200:
		direction = 0
		border = True		
	elif newx <= -200:
		direction = 1
		border = True
	
	#crossing path?
	px = ((newx+200)/400)*1080
	py = ((newy+200)/400)*1080
	
	color = op('feedback1').sample(x=px,y=py)
	
	if border or color[0] > 0.1:

		scriptOP.par.value1x = direction
		
		op('script1').par.value1y = getRandomAngle(int(direction))

		startX = scriptOP.par.value0z
		startY = scriptOP.par.value0w
		
		distance = math.sqrt(math.pow(startX-newx,2)+math.pow(startY-newy,2))
		if distance > 2:
			op('line1').par.pax = startX
			op('line1').par.pay = startY
			op('line1').par.pbx = newx
			op('line1').par.pby = newy
		
		scriptOP.par.value0z = newx
		scriptOP.par.value0w = newy		

	x[0] = newx
	y[0] = newy
	xOld = newx
	yOld = newy

return

def getRandomAngle(dir):
# north, east, south, west
directions = [90,0,-90,180]
a = (random.randrange(-7,7) + 0.5) * 90 / 7
a = a + directions[dir]
return a[/code]

It is part of this package of examples: derivative.ca/wiki088/index. … ive_Design

Thanks, Rob, that definitely helps me understand the sandbox I am playing in a little bit more.

Here is a followup question to all you python experts: Do you find that, knowing python, you tend to use it for most things in your TD projects? Or do you still find it useful to use OPs networks to build?

I use Python for just about everything, but I also use OPs for tons of things. I think you should definitely learn Python, it’s an extremely readable programming language supported by quite a number of programs as their scripting language. You’ll quickly realize what tool is the best tool for the job at hand.

Thanks for your input! I guess my priority should be to go through these exercises and learn how the python scripts are working, rather than to worry about how to accomplish things in the non-programmer’s world that I am more comfortable with.

Ya, the CodeAcademy courses are pretty good if you’re just starting Python, as well as being easy to jump in and out of quick sessions, although they’re based on Python 2.7 instead of Python 3, so you’ll run into little things that you’ll have to change to make what you learn work in 3, but not that much.

There is a simpler Python example project in the 088 shared component section of the forum that might be easier getting you started off using Python to talk between OPs in simple steps, then you can build from there.

If you can do everything easily in Python already, why not? Consider TouchDesigner your modular real-time IDE that can also be used to make other graphical and 3d things really quickly that you wouldn’t want to do in Python alone. In my mind the goal is the get to the end goal as quickly and efficiently as possible, whatever the means!

I am having trouble with the passing of information from chops into a simple bit of python and out into chops again. or maybe a table.

I have put together some complex python code in the past. The challenge for me rite now being new to touch designer is how to move information from one for to another.

some tasks i would love to see an example file cover is simple things like…

Listen to 2 chops when value A is less than B return -1, equal return 0 and return 1 when A is grater than B. Then export this information to a chop.

write to some tables via python.

Another question is with python, using python dictionarys to store data, having the ability to listen to chops and write to chops… is the need for DAT operators greatly reduced?

I am stuck at this beginning stage where i don’t know quite how to relate to touch designer not understanding how to flow in and out of the processing languages and not getting the momentum to make the art i want. Coming from 9 years of houdini it is hard to be lost in something that is so close to what you know but not quite.

I find the documentation confusing and non intuitive to learn, but i am open to blaming to not being in the rite head space for TD.

Here you go. 2 examples, one file.

At first I tried to solve this with the chop execute dat. However I wasn’t able to get both channels coming into the valueChange() at once. So i mangled the expression chop to do the bidding. Like houdini, many ways to skin a cat.

This also demonstrates a handy way of putting your logic into functions that are easier to write than one liners.
pythonexample.toe (3.99 KB)

thanks that is just the little ingredient i needed to overcome this little road block and let loose.