[TouchDesigner] Tips for for statements using python

Nice to meet you

My name is ibuibu69 and I make videos and music as a hobby! I used to do things like VJ and instrumental bands using max and processing. .. https://www.youtube.com/watch?v=NswI72i6lCg

... The band has disbanded and I want to do something new with Touch Designer these days. I live in Tokyo, so please make friends with me. ..

Now, let's post the first Advent calendar.

Main subject

When trying to do difficult things with TouchDesigner, control by python becomes indispensable.

However, I think there are relatively few TouchDesigner users (including myself) who are familiar with python. .. .. maybe. ..

Therefore, here are some tips for batch operations of operators using for statements, which you will often use.

Operate many operators

For example, "Substitute 1 for value 0 of constant 1, 2 ... 9" is as follows.

for i in range(1,10):
    op( 'constant' + str(i) ).par.value0 = 1

cfsd3-u422f.gif

This is easy, isn't it?

Commentary: Since the inside of op () is a string, i must be a string with str ().

Assign to many parameters

For example, "Substitute 1 for values 1, 2, ... 9 of constant1" is as follows.

for i in range(1,10):
    script = 'op(\'constant1\').par.value' + str(i) + ' = 1'
    exec(script)
print(a)

7cqii-7k8am.gif

Use the exec function. (I didn't know this and struggled.)

Tips: To make the value item number an iterator, first write the entire statement you want to execute in string type and embed the iterator. Then execute it with the exec () function. Don't forget to escape the quotes! !!

Get the values of many parameters

For example, "Display the total value of 1,2, ... 10 of constant1" is as follows.

a = 0
for i in range(10):
    a += eval('op(\'constant1\').par.value' + str(i) )
print(a)

It's similar to the exec function above, but this time it uses eval, a function that evaluates values.

You may use the exec function as shown below.

a = 0
for i in range(10):
    script = 'a += op(\'constant1\').par.value' + str(i)
    exec(script)

Summary

In this way, when you want to turn TouchDesigner's built-in function with a for statement, using exec or eval works well.

It was an introduction! !!

I occasionally upload videos and music on twitter. I would be grateful if you could follow me.

https://twitter.com/ibuibu69

Recommended Posts

[TouchDesigner] Tips for for statements using python
Tips for using python + caffe with TSUBAME
[Python + Selenium] Tips for scraping
~ Tips for beginners to Python ③ ~
[Introduction to Python] How to write repetitive statements using for statements
python tips
python tips
Tips for calling Python from C
[Python] Reasons for overriding using super ()
[Python] Multiplication table using for statement
[VS Code] ~ Tips when using python ~
Python Tips
Python tips
Notes for using OpenCV on Windows10 Python 3.8.3.
Python development environment for macOS using venv 2016
[50 counts] Key transmission using Python for Windows
Tips for dealing with binaries in Python
Summary of various for statements in Python
[python, multiprocessing] Behavior for exceptions when using multiprocessing
Precautions when using for statements in pandas
[Python] Organizing how to use for statements
Notes for using python (pydev) in eclipse
Tips for making small tools in python
2016-10-30 else for Python3> for:
Python Conda Tips
python [for myself]
Python debugging tips
Python click tips
Start using Python
Reading and creating a mark sheet using Python OpenCV (Tips for reading well)
Unexpectedly (?) Python tips
Scraping using Python
vprof --I tried using the profiler for Python
Python basics (variables, types, operators, if statements, for statements)
Tips for using Realsense SR300 on MacBook in 2020
~ Tips for Python beginners from Pythonista with love ① ~
Get note information using Evernote SDK for Python 3
Refined search for Pokemon race values using Python
Tips for using ElasticSearch in a good way
Let's make a module for Python using SWIG
~ Tips for Python beginners from Pythonista with love ② ~
Let's analyze Covid-19 (Corona) data using Python [For beginners]
About Python for loops
Initial settings for using Python3.8 and pip on CentOS8
Python and numpy tips
Extendable skeletons for Vim using Python, Click and Jinja2
Fibonacci sequence using Python
Directory structure for test-driven development using pytest in python
Data analysis using Python 0
Python netCDF4 read speed and nesting of for statements
[Heroku] Memo for deploying Python apps using Heroku on Windows [Python]
[Python] Output battles and combinations (nesting for statements and if statements)
About Python, for ~ (range)
Tips for speeding up python code correctly with numba
Data cleaning using Python
python textbook for beginners
Explore Alibaba Cloud Function Compute for DevOps using Python 3.0
Python Tips (my memo)
Refactoring tools for Python
Using Python #external packages
WiringPi-SPI communication using Python