Change textures in game

Is there a way for a mesh to change it’s texture in game? Such as when “b” is pressed, a plane can change it’s texture from “fps background.jpg” to “pretending to work.png”

Python OK

I just switch meshes.

Without blender materials, you have an option to make an animated sprite, but you do not have very much control over it.

There is a video script running around that you might want to look at.

Yes, there is a script to do that, but it takes the Blender module (which is included in Blender, but not in the blenderplayer, so it will not be in your runtime… you will need to get a copy of Blender.py somewhere if you want your game to run independent of Blender)

This is the script (apparently written by alien_xmp, but I found it in a demo by martinsh):

import Blender

gobj = GameLogic.getCurrentController().getOwner()

# Assumes mesh name == object name... very bad
bme = Blender.NMesh.GetRaw(gobj.name[2:])

# Get Image handle for first face
ima = bme.faces[0].image

ima.reload()

This script will reload the texture. The texture needs to be external for this to work (it cannot be packed in the file). Also, the object name and the mesh name must be identical for this script to work as it is written.

In order to switch textures using this method, you must have at least three files per texture. You need the original texture, the texture to switch to, and the texture you load from. The texture you load from should initially be a copy of the original texture. When you want to switch textures, first delete the texture you load from, then replace it with a copy of the texture to switch to (all using standard Python file commands). Now run the reload script. (This entire process will take 1/60 of a second on a capable computer.)

When you want to swap back to the original, just do the same thing, only using the original this time. Obviously, you could use this method to switch between more than one texture if need be.

You can do it using Video Texture plug-in:

import blendVideoTex
from PIL import Image
obj = GameLogic.getCurentController().getOwner()
GameLogic.texImage = blendVideoTex.TexImage(obj, 0)
img = Image.open(’myPicture.jpg’)
GameLogic.texImage.setTexture(img.tostring(), img.size[0], img.size[1], 1)

You’ll need PIL too, to load image in any format.
For deeper information look on Video Texture plug-in pageor on its discussion thread