# (Animation nodes) Is there a way to instance objects inside a subprogram?

I'm working on a recursive fractal where a hand splits into 5 new hands, much like this:

but my node setup looks like this:

each of the 2 huge groups on the bottom is for just 1 level of recursion. Since the addon seemingly doesn't allow objects to be instanced inside subprograms, you can see at the top I have to output each new hand, instance it outside the group and pass it to the next level. That means, if I wanted to add another level, I would have to output all 25 individual hands, and exponentially more each level. Please, someone tell me there's an easier way to do this. Here's the .blend file if you want to take a closer look:

www.mediafire.com/?vso1h3wk68jjmpt (just copy-paste it, my rep isn't high enough for >2 links in 1 post)

## 2 Answers

You can use a script as workaround, so that AN cannot see that you create object instances in a subprogram:

Using scripts is the same way as all subprograms in AN. For script:

• Add a text
• Write the script, identifying the variables that will be inputs and output
• Set these parameters adequately in the script node

But, a big warning: be careful about the frame changes as it could generate a lot of objects.

If this is happening, you can use some script to apply in Python console, like:

for m in [m for m in bpy.data.meshes if m.users == 0]: bpy.data.meshes.remove(m)


Short Answer: No (or not yet). Currently, this is a known limit.

However, you can try slicing a long list into lists by using tricks in the example setup below:

Actually I tried to make a similar setup for making fractals:

(compatible with AN v1.6)

Advantage here is that the iteration is unlimited (depending on your PC performance), while no extra nodes needed.

To use, simply tweak those nodes at the top, which are out of any frames.

• Where do the vectors that determine the transformation of the branches come from? – Jackson Roberts May 1 '17 at 12:09
• There is an object called "Branch Tips (should better be called Branch Landing Points maybe?)" in the 2nd scene layer. I just separated the five faces at each finger tip apart, then used the face center location as branch loc, and convert face normal vector to branch rotation. Kind of a quick and dirty way though. – Leon Cheung May 1 '17 at 12:15