Community Post

Python Tips'N'Tricks #1

Python Tips'N'Tricks #2

Python Tips'N'Tricks #3

Python Tips'N'Tricks #4


At first i hated Python with all my guts. Im not even a programmer, but I had my fare share with languages like C, Java and the like. Python is so different. But the snake grew on me over time and I discovered a lot of neat little tricks to make my life easier. I will share some of them with you in the following post.



Sometimes you want, or have to, write some statements into one line of code. Maybe, because you are working in a parameter.

IF statement

if you want to set a value depending on a condition, you can write the following:
variable = value if condition else othervalue

This can be used to to  set a parameter depending on another one.


If you need to fill a list with values, you do not have to write a complete loop over several  lines. You can  write the loop into the list itself.

variable = [x for x in loopable]
par.val = [row[0].val for row in op('table1').rows()]     #this will set the parametervalue to a list of all the values in the first col of table1


One of the most asked questions about python is, how do I acces as parameter if i have the name as a string.

By using the attr functions. The names say it all.


has attribute -> hasattr

boolean  = hasattr(object, "attribute name")

hasparameter = hastattr( op('operator').par, 'Level' )


setattribute -> setattr

setattr(object, "attribute name", vaulue)

setattr( op('operator').par, 'level', 100)


get attribute -> getattr

attribute = getattr(object, "attribute name")

parameter = getattr( op('operator').par, 'Level')

You can also pass an additional argument to getattr as a default value, so when the attribute does not exists, it instead returns a default value.

parameter = getattr( op("operator").par, "Level", op("default_operator").par.Level )


You can also use this to get other attributes from operators, or even other classes like functions, properties etc. This is extremly powerfull when you work with compositing code, so instead of hardcoding something, you can easily create a table of function names.


But, be aware, this functions take quite a lot of time. So use them rarely. Best is to call them once in save the results into:
You can also use the set/getattr methods to retrieve functions.


Dicts are truly amazing and make programming with python so much easier. A dictionarie is a collection of values, accessable by a key.

myDict = { key : value }
myDict[key] return value

But the truly amazing thing is, that key and value are not bound to be strings or anything. They can be (nearly) what ever you want.You can define funtions to be called at a certain key, or define a parameter object as the key (As long as the are immutable. So no list or dicts). The following dictionary will use a debug or print statement, depending on the parameter you pass into the dictionary:
myDict = { op('operator').par.Level      : debug,
            op('operator').par.Intensity : print, }


When calling a function, you can pass the arguments as a dictionary using **
def myFunction(a, b, c = ''):
    debug(a, b, c)
argumentdict = { "a" : 1,
                 "b" : 2,
                 "c" : 'A text',




I work more and more with webapplications, and JSON (JavaScript Object Notation) is a staple of the game. Its easy to read, edit and understand. (I know there are people defending XML. Poor souls.) The JSON notation looks something like the following:
myJsonString = '{ "Name" : "Hibert" }'

Looks familiar? It basicly is the same as dicts in python. But not all the way. JSON is String only, so when working with json in touch, you will need to parse the string first. Luckily, Python supports JSON without problems, just import the json module
import json
myJsonDict = json.loads(myJsonString)

And whats even better, you can turn basicly every python dict into a json string. You can, for example, store some configuration of the project into a dict and export is as a JSON File and only use that JSON file to bringt your config to the system.
import json
config_dict = { 'Level' : op.settings.par.Level.eval(),
                'Brightness' : op.settings.par.Brightness.eval(), }
config_json = json.dumps(config_dict, indent = 4
) #indent increases the readability alot
op('text1').text = congig_json

giving us a file looking like that:
"Level": 0.0,   
     "Brightness": 0.0



The tdu functions are another nice tool given to us by derivative. The tdu function are a set of function hardcoded into the engine and running extremly fast. By using them you can remove clutter from your project. Lets have a look. Here we have a RAMP Lfo driving two parameter with different range:

This takes up some space, and scales badly. Imagine how this looks like with 10 circles. Now let's delete the two mathChops and one of the nulls and lets use the tdu.remap function. It is defines the following:
tdu.remap( value, from_min, from_max, to_min, to_max)

So now we can put the following expression in  our scale parameter:
tdu.remap( op('null2')['chan1'], 0, 1, 0.3, 0.4)

Note: In  this case, one way is not specificly better then the other. It can help for bigger system to populate parameter dynamicly without the need to take care of additional nodes and connections.







Thats all I have for you today, but more is to come. I'm pretty sure.