I am starting with the animation nodes and I am trying to replace as many nodes as possible with Python code; it is especially nice for the math. However, now I am stuck with the looping. Is it possible to replace the Loop Input and its generators with Python code plus other nodes? thanks
You just have to know how to access input variables and how to output variables in the script. Otherwise it's just common python operations. Mainly you will have to use
Let's see how to emulate a loop with a python script, for example to output a list of the location of each object in a given collection.
Here in my collection I have 3 objects.
And here the simple setup to output their locations using a loop node :
Now, to do the same with a script node. Add your script node, add a text block with the + icon. Add a new input of type "Collection", I renamed it "col". Add a new output of type "Vector list", I renamed it "positions".
In a text editor, find your new script textblock and open it. Write a few lines to do the job.
positions =  for obj in col.objects: positions.append(obj.location)
Or with list comprehension
positions = [obj.location for obj in col.objects]
col hasn't been defined in this text block : AN gives it context since you told the script node that a variable named
col will be available, as a collection type.
Similarly, you have to define the variable
positions and at the end of the execution of the script, AN will automatically gather its value.
positionsare case sensitive. They have to match the variable names you wrote in the script node.
Add an invoke node set to your script, execute the AN tree and voilà :