Read Text, eval and assign an integer

Hey folks, I’m doing something a bit out of my depth, but I figure this is an easy one for somebody out there. I’ve got a message coming in from mqtt, and it’s displaying fine. I can also isolate just the cell in the mqttclient node by linking it into the text node.

The text node has a “DAT” parameter, and I give it the “mqttclient1” name and tell it to use Row “1”, and it displays the text message I’m sending over. The next thing I’d like to do though is eval this text and then assign it an integer based on what text is coming in.

Basically something like this:

1 op('text1').par.text == 'Game1' else 0

I’m not getting it to eval correctly though, ideas? Keep in mind I’m a beginner. I dabble in scripting, but I’m mainly a graphics person.

It looks like it’s not seeing anything in the ‘text’ parameter. I need to somehow read it directly from the mqttclient1 node? The mqttclient only have 1 column and 1 row. When I read it, it’s column 0, and Row 1 if that helps at all.

Hi @DreamAlchemist,

just to make sure - you are using a Text TOP?

You could switch the Text parameter into expression mode and type an expression that references the MQTT DAT’s cell you are interested in:

1 if op('mqttclient1')[1,0] == 'Game1' else 0

More practically you would want to parse the message right when it arrives in the MQTT Client DAT’s onMessage callback.

Here for example you get a bunch of variables you can use that represent the message:

# dat - the OP which is cooking
# topic - topic name of the incoming message
# payload - payload of the incoming message
# qos - qos flag for of the incoming message
# retained - retained flag of the incoming message
# dup - dup flag of the incoming message

What is always useful is to open up the Textport (Dialogs>Textport) and then write a debug statement into the callback like this:

def onMessage(dat, topic, payload, qos, retained, dup):
	debug('dat:', dat, 'topic:', topic, 'payload:', payload, 'qos:', qos, 'retained:', retained, 'dup:', dup)
	return

When a message is received, you should now get a printout in the Textport with all the values of the variables printed out.

From here you will see if the value you want to check is in your payload or if it’s the topic. If it’s the payload, a script to place the value in the Text TOP could look like:

def onMessage(dat, topic, payload, qos, retained, dup):
	textVal = 0
	if payload == 'Game1':
		textVal = 1
	op('text1').par.text = textVal
	return

Oftentimes it might make sense to prepare a Table DAT for the different types of messages you might receive, store the values in there and then retrieve them in the displaying operators (like your Text TOP) by referencing that Table DAT.
We can look at that more when you get this running though.

For a good introduction to scripting in TouchDesigner, have a look at our Curriculum at

learn.derivative.ca

there are great short videos that convey the basic concepts of python in TouchDesigner.

cheers
Markus

The 1 if […] else 0 line is btw not needed. You can conert a Boolean to an integr just fine. So instead of
1 if op('mqttclient1')[1,0] == 'Game1' else 0
you can write
int(op('mqttclient1')[1,0] == 'Game1')
This is also in regard to stuff like "True if […] else False which I sometimes see. Not needed at all :slight_smile:

Do your question, a dictionary and the get function might be better.

lookupDict = {
    "Game1" : 1,
    "BonusGames" : 2
}
def onMessage(dat, topic, payload, qos, retained, dup):
	op('text1').par.text = lookupDict.get( payload, 0 )
	return
1 Like

Thanks, this did solve my issue. My payload was appearing as b’Game1’, not sure where the b came in, but as soon as I added that, it saved me banging my head more. Now I’m adding more logic to what I’m doing with this information. This is getting kinda cool. I truly feel like “I have the power!” as He-Man would say.

1 Like

Thanks, I’ll have to look into doing this sort of thing!

Hi @DreamAlchemist,

the b'Game1' indicates that this is a binary value. You should be able to convert this to a “regular” string with:

payloadDecode = payload.decode('utf-8')
debug(payloadDecode) # will print the variable payloadDecode

cheers
Markus

Thankee, pretty happy with this now!