[Closed] How do I change diffuse colour of a poly using maxscript?
Hi all,
I’m new to the forum and max scripting. I’ve had a read through the manual for max script but even though I have a programming background, finding it difficult to find the information I need.
I need to put a script on various frames which will change the diffuse colour (or swap the texture) of an object. To give you an idea of what i want to achieve, I’m creating a visualisation for a website which will demonstrate traffic building up, I want to start the vis. with green cars and then change the colour to red over time.
Could someone point me in the right direction please?
Thanks,
eb_dev
Do you have a flash (or director) background, or something similar? “I need to put a script on various frames” sounds like you’re thinking in terms of global frames whereas max is more object based. In max, something like this would all be done in one place and animated over time.
Standard materials (all materials?) have a .diffuseColor property that you can animate over time. It’ll look something like this:
with animate on (
for t = 0 to 10 do (
at time t (
$.material.diffuseColor = color t (t*10) (t*200) -- arbitrary numbers for testing
)
)
)
Keep in mind you don’t need to set a value for t at every frame because max will interpolate between them.
Hi focomoso,
Thanks for your speedy reply. Yeah I do have a background in flash, I thought that maybe I could add scripts to key frames like you would add a change in a certain property. No bother though, doing it over time is fine by me.
From looking at the code you have supplied, this code checks whether you can animate, then creates a loop to set ten colour changes, each colour being based on the integer t. Is that right?
I have a few questions if that’s ok
- what does the $ stand for?
- do you reference materials as standalone objects rather than referencing an object (sphere for instance) and then the material that is appied to it? I need to change the colour of individual objects (cars) over time you see.
- does ‘with animate on’ mean when the animation is playing? and is animate a global variable?
- how would you change / swap the material on an object?
Thanks for you help,
eb_dev
I hate to do a rtfm, but some of this is pretty basic stuff you should familiarize yourself with before you start.
From looking at the code you have supplied, this code checks whether you can animate, then creates a loop to set ten colour changes, each colour being based on the integer t. Is that right?
Almost. If you select an object with a standard material and run that code, you’ll see what happens.
The first line isn’t checking whether you can animate, but turning on the “animate” context (see below). Don’t get caught up on the fact that I assigned multiples of t to the color of the material. That’s just so we can show some color changes over time. What’s important is that we’re using t to represent time (in this case a frame number), so we’re stepping through time from frame 0 to 10 and doing something at every frame.
-
$ is a global reference to the current selection. It’s a single node if one object is selected or a collection if more than one object is selected. Most likely you wont use it in your code, but rather get the objects by some other method (say, work on all objects of a certain type or reference the objects by name). We use it here for convenience.
-
In this snippet, we’re referencing the material that’s already applied to a given object, but you don’t have to. You can create a material at any time and assign it to a variable and then assign that to a node’s.material as you like (but you can’t animate this way, see below).
-
animate is global, but it’s not a variable. It’s a state (they call it a context in max). It’s the equivalent of turning on the “auto key” toggle. If you make changes to a node over time and animate is off those changes won’t actually be applied at the specified time because max won’t generate any keys. With animate on, max will set key at the given frame.
-
You assign a material by creating one and assigning it to an object’s .material property, but you can’t animate this way. Every object has one material. What you do is make changes to the material’s properties over time and max will create keys for them. What’s good about this is that you can create and animate a single material and then assign it to as many objects as you want and they’ll all change simultaneously.
Yeah I have looked through the relevant parts of the manual, but I can’t find exactly what I need and I’m also on a tight schedules so looking for shortcuts, thanks for the info though I’m beginning to get my head around it now.
As I want to change my bunch of cars to red over a period of time could I use your code in the following fashion:
with animate on {
for t = 0 to 10 do (
at time t (
$("car"+t).material.diffuseColor = [b][b]color[/b][/b] 255 0 0
)
)
}
I’ve guessed at the car selection bit (yet to find the correct syntax in the manual), what I’m trying to do is select my cars which will be named car<num>.
Thanks,
eb_dev
Almost.
While you can use the t loop to pick objects, the convention is to use t to represent time. And you don’t need to step through every frame to set the color, just the start and end and max will do the intermediate steps. So what you really want is something like this:
for obj in $'car'* do ( -- this creates a collection of all objects starting with the letters 'car'
with animate on (
at time 0 -- set the material to red at frame 0
obj.material.diffuseColor = red
at time 20 -- set the material to green and frame 20
obj.material.diffuseColor = green
)
)
This will put the same color ramp on all the car object’s material.
Ah right great that’s perfect, thanks very much you’ve been very helpful!