Close
Community Post

My Learning Journey 06 - Python slowly coils....

I continue learning touchdesigner via the "Absolute Beginner TouchDesigner | From Zero to Hero with Examples and Assignments" Youtube video by the talented and comprehensive Acrylicode. This new section covers DATnodes, which are based on tables, text, and scripting.

  • Text -- The first node my instructor showed me was the text DAT node, which is used for note taking and python code. At this point I was shown how to acces the textport python interpreter via the viewer window. We wrote a little basic hello world printing script. I had a hard time running the code, and I learned that I must SELECT the text node and then ctrl + r or right click and select "run code" to run the python code; you can't just run it at any time. Next we started referring to TOP operator nodes and their channels. This is a great straightforward way to manipulate channels without having to rely on CHOP operators! When referring to an operator, use op('operatorname').par.parametername = value. Very important not to use quotation marks but colons when naming the operator. I ran into an error the first time because I named my operator incorrectly, but couldn't read where the issue was coming from--I'm sure there's a way to report errors somewhere, but it was not explained to me, so I'll have to figure it out later..
  • Button COMP to Null CHOP to chopExecute DAT -- here my instructor introduced a very interesting and likely useful combination of three almost entirely new nodes: the Button comp, which when in viewer active mode, can be clicked, and a Null CHOP, which can translate the button's clicks to a 0/1 value. From there, we connect the Null CHOP to a chop execute DAT node; meaning the code will run anytime a certain CHOP parameter is met. I had to set the specific triggers for the DAT node in its panel, and then write the code in the code viewer to specifically what it should do upon execution. The code was as follows:
#including the python random library
import random
 
#everytime the null chop is activated, this piece of code runs
#this is a default function of the CHOPexecute DAT node --
def onOffToOn(channel, sampleIndex, val, prev):
	#a little debug test 
	print("triggered")
	#choose a random number between 0 and 10k
	randomseed = random.randint(0,10000)
	#put this number in the seed channel of the noise node
	op('noise1').par.seed = randomseed
 
	return
 

now we experimented with a second random function, random.random, which picks a float between 0 and 1 I believe. We used this code to manipulate the phase channel in a ramp TOP  node. I'll have to remember to use the correct data type (float, integers, etc) with the corresponding channel!

import random
 
#everytime the null chop is activated, this piece of code runs
def onOffToOn(channel, sampleIndex, val, prev):
	print("triggered")
	#choose a random number between 0 and 10k
	randomseed = random.random()
	#put this number in the seed channel of the noise node
	op('ramp1').par.phase = randomseed
 
	return
 
  • Using tables to influence RGBA -- the table DAT is pretty cool. It allows me to change multiple values at once; our instructor demonstrated an interesting way for us to use tables, which is to change RBGA  values of colors. so I created a table and manually set its rows and columns in the 'Fill' tab to exactly match the table of a ramp node I had made. I set the pos values of the table to just 0 and 1 (only needed two points), and the alpha values to 1, then addressed the table using a chop execute DAT like so:
import random
 
#everytime the null chop is activated, this piece of code runs
def onOffToOn(channel, sampleIndex, val, prev):
	r1 = random.random()
	r2 = random.random()
	g1 = random.random()
	g2 = random.random()
	b1 = random.random()
	b2 = random.random()
 
	op('randomcolors')[1,1].val = r1
	op('randomcolors')[2,1].val = r2
	op('randomcolors')[1,2].val = g1
	op('randomcolors')[2,2].val = g2
	op('randomcolors')[1,3].val = b1
	op('randomcolors')[2,3].val = b2
 
	return
 

hooking up a button and null to the chop execute, I was now able to change the value of the table with a press of the button! once I had connected the table to the ramp, its colors began to change! It was really fun! I'm so excited to manipulate colors in the future with this method.

  • Operator Reference Paths -- the instructor took a moment here to differentiate between using code in the textport code editor and within a nodes when referencing operators. Within the text port, if I want to refer to an operator, I must use its entire address, as it is a global editor. so to refer to the "ramp1" node, instead of calling it op('ramp1), I would have to refer to it as op('project1/DATtutorial/ramp1') -- the ramp node is in the DATtutorial base node in the project named project1. Good to know!
  • Folder DAT -- The final thing touched on was using the folder DAT node to be able to access resource files saved outside of the project. As I didn't have any files to play around with on hand, I simply watched the video. I will probably come back to this section if I ever need to use outside files with out importing them explicity.

This was a great section! Even though I'll never be a python fan (C family or death) I love to program, and I was happy to see that most of this was graspable, and considerably more efficient for channel manipulation in some cases. I look forward to experimenting more with code. The next section covers COMP operators.

 

 

Comments