Animation Nodes distances question

Hi,
See the attached image - I have a basic setup so that the distance between 2 objects affects the emission material strength. I’m just trying to figure out how to make it the inverse. I would like it to be brighter when the sphere is closer (at the moment it is brighter when the sphere is further away). I’ve been playing around with some maths nodes and falloff nodes, I can’t figure it out.


Thanks in advance!

G’Day Mate! :smiley:

I would do it like this:


There are three inputs to control the setup:

  1. Brightness Factor - this controls how bright the light will be, so a factor of 0.5 and distance of 10 means the emission strength is 5

  2. Maximum - the greatest distance that the system works over, beyond this the emission is not changed.

  3. Minimum - the minimum emission strength, so when the object is at, or more than, the Maximum distance away, it’s strength is always this value.

Blend file: light-dist.blend (606 KB)

Just move one of the objects. :smiley:

I have set the node tree to run 'Always", during an animation set this to run on “Frame Changed”.

Cheers, Clock.

EDIT:

Or you could do it with an Expression Node:


Amazing, I just built your first solution and learnt a few things! The clamp node will be a handy one to know.
Thank you very much sir!

You’re welcome!

Cheers, Clock.

Actually one quick follow-up. Do you know a way to make it so the emission is either 1 or 0 and nothing in between? Like past a certain distance, it just clicks over? Instead of a smooth gradient.
Cheers

Sure, just use this setup: :yes:


I have kept the Brightness Factor input and the formula in the Expression Node is:

(x <= y) * z

Where X is the distance between the objects, Y is the cut-off distance and Z is the Brightness Factor. An expression like this:

(x <= y)

Returns True, or 1 if x is less than or equal to y and returns False, or 0 if x is greater than y. You can use these types of expression in Drivers as well. be aware that you cannot use = on its own - use == for “equals” and != for “not equals”

If you want the emission to be on outside the distance, just change the formula to:

(x > y) * z

Expression nodes are very useful and versatile in a node tree, for example you can put:

obj.matrix_world.decompose()

And it will return a list containing the True World Location, Rotation and Scale of an input object, even if it is parented to another object - you can use almost any line of Python code in fact.


Cheers, Clock. :smiley:

EDIT:

I forgot to say… don’t forget to set the output type for the Expression node thus:


Just click the little gear icon and select Float,

Thank you so much sir! I will have a go at this now!

OK I have one more question which I hope is just a tweak to the expression.
What if I want to make the brightness go between say 0 and 25 but I want to slide between this large range when the distance (of the two objects) changes only by a small amount? So do I need to use the Map node or something?

So I want to be able to slide an Empty object a small distance and it will turn the emission on from 0 to 25.
Thanks :smiley:

I asked too soon! I think I have it.



OK now here is the next question: (haha this is never-ending!)
Imagine I have a series of boxes and I want to slide an empty down the line and they will light up and down in sequence.
So I have my nodes in place, now I am going to just Shift+D and duplicate the nodes (then push Shift+R a few times) and change the object and the material each time so everything is hooked up.
OK but the little I know about programming leads me to think I should be looking to avoid this manual work and instead implement a FOR loop or something. Is there any potential for doing this? :smiley:

OK, so this you do with a Loop Sub-programme - feed a list of objects as the iterator and whatever parameters you want to effect the objects - all will be processed as if they were in a for… loop.

All is explained here, have a go and feel free to ask any questions.

Here is an example to show the structure:


Alternatively, if you are good with Python, you could use a Script node and a for… loop.

Cheers, Clock. :slight_smile:

If I was doing it I would use two of my own nodes - you can mimic these using Python scripts, and/or Python in Expression scripts, so the node tree is simply this:


And the project:


As I move the Empty-Sphere, any cube, within 3.4 units, gets switched to the brighter material. Without my Material Nodes, it’s more complex, but quite doable. :slight_smile:

Go to this page on my website, to look at how to use my material nodes, other pages explain how to install my add-on nodes.

Cheers, Clock. :smiley:

Thank you very much sir!
I’m going to have a good read of the Subprogram and Loop documentation and also check out your website!
AN is very fun, I can imagine it has huge potential.

Carl

Yep, massive potential, I really like everything about it, I just made some more nodes to do things that are quite complex so I don’t have to remember how…

Cheers, Clock.

PS. type bpy.data.materials into an Expression node and you get a list of all the materials in your project.

Type [item for item in bpy.data.materials if item.name.startswith(‘green’)] and you get a list of all the materials that start with the letters “green”:


EDIT:

Feed an Expression node with an Object(obj) and a Material(mat) (Get List Element from my previous Expression node) type this in obj.material_slots[0].material = mat and it will set the material slot 0 to be the material.