Changing (setting) materials in-game

Greetings Blender Artists.
I am pretty new to blender game engine, so my apologies if this post seems noob-ish :wink:
I have a script that will randomly generate a solar system, and all works well; however, I cannot find a way to create a random material for the planets and such. I have this function for actually making the material:


def makeMaterial(name,diffuse,specular,alpha):
    mat=bpy.data.materials.new(name)
    mat.diffuse_color=diffuse
    mat.diffuse_shader='LAMBERT'
    mat.diffuse_intensity=1.0
    mat.specular_color=specular
    mat.specular_shader='COOKTORR'
    mat.specular_intensity=0.5
    mat.alpha=alpha
    mat.ambient=1
    return mat

and then I go and make my material,


mat=makeMaterial('name',[uniform(0,1),uniform(0,1),uniform(0,1)],[uniform(0,1),uniform(0,1),uniform(0,1)],1)

which (i think) makes a good material with random colors and such. Unfortunately, I have no clue as to how to actually assign the material to the object I have. Any help is greatly appreciated, and thanks in advance.

-Collective2

Well, you can’t create materials / geometry in-game if you plan on distributing the game as a standalone executable. What you can do is change the material color using Python (obj.color = [red, green, blue, alpha]).

I see… Thank you Joeman16. Is that it then, just color? I guess I can just look on the api reference for tips on how to add textures and stuff (if you can add textures at all, I don’t know!), so again, thank you for your help.
-Collective2

You can use the videotexture module to dynamically add / replace a specified texture on a material with one from a directory - that’s useful.