I've a game project done with BGE and i've done a menu where the user can choose the character and the characters have only different material and texture. I'm not able to change material and texture with python, i just know how to replace the mesh with logic bricks but i want to keep the mesh and the armature of the character. Any advices? Thanks for the help
1 Answer
UPBGE has a Replace Material API, but regular BGE does not. However, you can replace the material with a custom GLSL shader.
Changing textures is not doable, but you can replace the images used in a material. A simple python function that does this is:
def show_picture(obj, path, mat_id=0, tex_id=0):
'''Loads a picture into a texture'''
prop_name = 'SHOW_PICTURE{}:{}'.format(mat_id, tex_id)
if prop_name not in obj:
tex = bge.texture.Texture(obj, mat_id, tex_id)
obj[prop_name] = tex
else:
tex = obj[prop_name]
raw = bge.texture.ImageFFmpeg(path)
if raw.status == 0:
# Error in loading image
bge.log.error("Unable to load image at {}".format(path))
raise ValueError("Unable to load image at {}".format(path))
tex.source = raw
tex.refresh(True)
Where tex_id is the texture slot of the image.