THE POWER OF ITERATION
Before I started to learn python I heard that it can be useful for many aspects of compositing to speed up the workflow and to create all sort of cool/usefl things. So I watched my first tutorials about strings, variables, lists and functions. It was a lot of theory and seemed like the theory part never ends. I stopped learning python then because it was exhausting, waiting for the "interesting part".
I started and stopped at least three times weeks apart until I managed to push myself through the "boring part" and start to build something.
Because of that I would like to try to help a bit those compositors who wants to learn python but would like to start at somewhere "interesting"! For me For Loops was the first part with the "gotcha" moment when I started to see the real benefits of learning python. For Loops are very common and very efficient ways of dealing with boring tasks.
Obviously, this won't mean that the theory of booleans, classes and all other aspects can be skipped but it might be easier once the benefit of learning is in sight.
Let's assume that this is your first time using the script editor and give you a brief tour!
There are more buttons to use at the top of the Script Editor and if you hoover your mouse over will know what each one does but to be honest I never use else than the Ctrl+Enter shortcut to run the code.
Also, this is a Preferences setup for the Script Editor that I like to work with so that:
1 I can see how things I do manually looks in python in the output window
2 I like to keep my Script Editor history ( it's stored in the menu.py as ScriptEditorHistory.xml )
Iteration AKA "for" loop
With For Loops here we iterate through a sequence and apply certain statements on it's elements. To put it simply, we apply the same settings on multiple elements. So let's say that you don't want to set up the same colorspace value for 50 Read nodes. Luckily, you don't need to! It's much easier to write 4 lines of codes that will do it for you!
Okay, let's run some code!
Copy paste the code below to your input window in a fresh Nuke.
TIP: It is good practice if you select the code that you would like to run so when you run the code only that part from the input window gets interprited!
With running this code you just managed to change multiple Blur nodes size value at once!
Let's break down a bit what is happening here before moving on.
for & in - These are the fixed elements of this snippet to be a For Loop.
node - That is an arbitrary variable. We usually use a word that makes sense in the context and as we are iterating through nodes I used the word "node" but if you change the two appearing "node" words to "banana" it will work just as well! It is a way of calling each element of the list when we applying statements on it.
Hope you will find it useful!