Changing Object Diffuse Colour Based On Distance Between Objects

Changing Object Diffuse Colour Based On Distance Between Objects

hazmondo
Advocate Advocate
709 Views
6 Replies
Message 1 of 7

Changing Object Diffuse Colour Based On Distance Between Objects

hazmondo
Advocate
Advocate
I followed this tutorial on CG-Academy: http://www.cg-academy.net/pages/free_tutorials/tut_scripted_color_to_length/tut_scripted_color.php to try to get an object's diffuse colour to change when the distance between two points (in this example) is made shorter or larger. The problem is that it doesn't work, even when I download the final file at the end and loaded it, it doesn't work. Does anyone know I can achieve the same effect in the more up to date versions of Max?

Thanks,
-Harry
LinkedIn: linkedin.com/pub/harry-houghton/51/20a/707
Website: hhoughton07.wix.com/hazmondo
Vimeo: vimeo.com/user9745086
YouTube: youtube.com/user/Hazmondo/videos
Twitter: twitter.com/Haz_Houghton
Facebook: facebook.com/harry.houghton.5
0 Likes
710 Views
6 Replies
Replies (6)
Message 2 of 7

Steve_Curley
Mentor
Mentor
First thing is that "DependsOn" is deprecated (shouldn't be used in new scripts) and has been since Max 8.

Create 2 variables, select each one, click Assign Node and select the appropriate object.
Change the provided script to use those variables instead of the $object names and it should work.

Note. Because of the hard-coded values in the rag.hue= line you will find that the distance over which the script works is quite limited - you may need to play with those valuse to suit your scene.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 7

Anonymous
Not applicable
I followed this tutorial on CG-Academy: http://www.cg-academy.net/pages/free_tutorials/tut_scripted_color_to_length/tut_scripted_color.php to try to get an object's diffuse colour to change when the distance between two points (in this example) is made shorter or larger.

it's very old tutorial. it was not a good solution seven years ago but now it's an example how shouldn't do a scripted controller.
do you want to see the right way?
0 Likes
Message 4 of 7

Steve_Curley
Mentor
Mentor
Feel free Denis - the general situation of "alter based on a distance" comes up often enough that a properly written generic solution would be quite useful.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 5 of 7

Anonymous
Not applicable
seven years ago the script controller probably didn't have some features which it has today.
so we can see several problems in the tutorial:

#1 it's a bad practice now to use a PathName values to define max objects used in the expression.
because any object renaming or deleting will cause the script failure.

#2 it's not a good idea to add any unnecessary nodes to the scene.
in the tutorial it's a test sphere.

#3 it's not a good practice to add a script to one controller to change the another.
in the tutorial the script applied to the scale controller to change the material's difffuse.

here is how the same idea has to look today:

fn setupDistanceColorController mat source target maxdistance:100 =
(
c = mat.diffuse.controller = point3_script()
c.AddConstant "maxdistance" maxdistance
c.AddObject "source" (NodeTransformMonitor node:source)
c.AddObject "target" (NodeTransformMonitor node:target)

str = "if isvalidnode source and isvalidnode target then\n"
str += "(\n"
str += " c = amin 1.0 ((distance source target)/maxdistance)\n"
str += " \n"
str += ")\n"
str += "else "

c.SetExpression str
c
)
/*
(
delete objects
mat = standard name:"Distance Meter Material"
source = box name:"source" isselected:on material:mat
target = box name:"target" wirecolor:green
setupDistanceColorController mat source target
)
*/

execute the function and run the commented part of the script to make a test scene.
move source or target nodes to see the color change.

the maxdistance parameter is used to map distance value (0+) to color (0-1)

PS. i forgot to say about another problem in the tutorial:
#4 in animate mode ON the script adds key to the diffuse controller every transform change.
which is definitely wrong.
0 Likes
Message 6 of 7

Anonymous
Not applicable
Wouldn't a distance-based Falloff map driving a Gradient Ramp in mapped mode eliminate the need for a script at all?
0 Likes
Message 7 of 7

hazmondo
Advocate
Advocate
Thanks for the replies, I was able to get it working 🙂
LinkedIn: linkedin.com/pub/harry-houghton/51/20a/707
Website: hhoughton07.wix.com/hazmondo
Vimeo: vimeo.com/user9745086
YouTube: youtube.com/user/Hazmondo/videos
Twitter: twitter.com/Haz_Houghton
Facebook: facebook.com/harry.houghton.5
0 Likes