Python Script to change material color of Cube after each second

Suppose I want RGB values as follow for
1st second [ 0, 0 , 0 ]
2nd second [ 0. 2 , 0 , 0 ]
3rd second [ 0.4 , 0 , 0 ]
4th second [ 0.6 , 0, 0 ]
5th second [ 0.8 , 0 , 0 ]
6th second [ 1 , 0 , 0 ]

Since Python FOR LOOP doesn’t support floating point numbers, I used while loop as follow,

from bge import logic
import time

cont=logic.getCurrentController()
own=cont.owner

i=0;
while(i<=1):
own.meshes[0].materials[0].diffuseColor=i,0,0
i=i+0.2
time.sleep(1)

This is not working, hence I written same code without using loop

from bge import logic
import time

cont=logic.getCurrentController()
own=cont.owner

own.meshes[0].materials[0].diffuseColor=0,0,0
time.sleep(1)

own.meshes[0].materials[0].diffuseColor=0.2,0,0
time.sleep(1)

own.meshes[0].materials[0].diffuseColor=0.4,0,0
time.sleep(1)

own.meshes[0].materials[0].diffuseColor=0.6,0,0
time.sleep(1)

own.meshes[0].materials[0].diffuseColor=0.8,0,0
time.sleep(1)

own.meshes[0].materials[0].diffuseColor=1,0,0
time.sleep(1)

but still it’s not working.

The BGE waits for all running scripts to complete before it renders each frame. Since your script sleeps, your entire game will freeze for a second waiting for it to finish. The second script is even worse as your game will freeze for 6 seconds and the final rendered frame will only have the last color.

Here’s a script I put together that you can try. It should be executed in script mode with an Always sensor using True Pulse triggering.

import bge
import time

COLORS = (
    (0, 0, 0),
    (.2, 0, 0),
    (.4, 0, 0),
    (.6, 0, 0),
    (.8, 0, 0),
    (1, 0, 0)
)
COLOR_INTERVAL = 1.0

cont = bge.logic.getCurrentController()
obj = cont.owner
now = time.perf_counter()
if 'colorIndex' not in obj:
    obj['colorTime'] = now
    obj['colorIndex'] = 0
   
if now - obj['colorTime'] >= COLOR_INTERVAL:
    obj.meshes[0].materials[0].diffuseColor = COLORS[obj['colorIndex']]
    obj['colorTime'] += COLOR_INTERVAL
    obj['colorIndex'] += 1
    if obj['colorIndex'] == len(COLORS):
        obj['colorIndex'] = 0 
1 Like

Thanks.I’m new to Blender Scripting.

I’ve basic knowledge of python but I’m new to Blender Scripting.

I couldn’t understood the purpose of following code

if ‘colorIndex’ not in obj:
obj[‘colorTime’] = now
obj[‘colorIndex’] = 0

Also tell me what does the perf_counter() function do?

That code handles initializing the colorTime and colorIndex properties on the object being used the first time the script is run on an object.

perf_counter() is my preferred method of getting time intervals between events. It bassically returns the amount of time the process has been running. This post gives a bit more detail.

1 Like

I remember a tutorial where you can use logic blocks to actuate the color change by animate the colors using the cube section instead of the material section. Use cycling properties variable and use the frame proprieties of the action block. https://www.youtube.com/watch?v=rUU5JSJkvDk

1 Like

Thank you very much