How to randomly generate character mesh color?

Hi BA community! I’m very new to python scripting and BGE, so I apologize in advance if I’ve made some very basic errors.

Here’s what I’m trying to do: tell an empty (“empty”) to add a rigged character (“figure”) into the scene, and generate a random color for the character’s mesh (“mesh”). This works fine when I try to add a basic cube mesh, but when I try to add the character, the color stays white and doesn’t get randomly chosen. I figure it’s likely because “mesh” is a child of “figure” so its color attribute isn’t getting called, but I’m not certain how to do that.

I’ve also ensured that the Object Color option is checked for “mesh”, and tried calling on “diffuse_color” instead of “color” attribute, but that doesn’t seem to work either. I’ve attached the relevant script below. Any help would be appreciated!

import bge
import random
sce = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner
r = random.random()
g = random.random()
b = random.random()
for y in range(1):
    #obj = sce.addObject("Cube","empty") 
    #obj.color = [r,g,b,1]  
    obj = sce.addObject("figure","empty")
    for child in obj.children:
        if "mesh" in child:  
            bpy.data.objects["mesh"].active_material.color = [r,g,b,1]  

bpy.data.objects[“mesh”].active_material.color = [r,g,b,1]

don’t use bpy module in the game engine. your script should have output errors. for one you haven’t imported ‘bpy’ so it would at least say ‘bpy’ is not defined.

if the child were just an object, you could change that line to simply:

child.color = [r,g,b,1]

3 Likes

also ensure object color is checked in material properties*

That was it! Thank you so much @sodsm_live for your help, I appreciate it :slight_smile:

2 Likes