Changing textures/mats in the run-time

Hallo again, feels like I have posted here 1 million times lately!

I am looking for some help with coding one of my hud elements. I have a little button, and when my player hits a key, I want the image to appear as if it’s highlighted, kind of like shortcuts do on other computer games. I figure the best way to do this is have a second image texture that is the highlight be displayed for just a second when they press the button.

I know I could have a second model of the button and just replace mesh with this new texture on it, but that seems inefficient when thinking of multiple buttons and other objects with changing textures like player clothings.

Can anybody explain to me the way to python code it so that I can change/add/remove textures and/or materials from an object in the run-time? Maybe some kind of quick sample code to help me understand how to do it?

I would ideally like to do it internally, but if it’s only possible externally, then so be it.

Look up uv animation and sprite sheet animations :smiley:

@animation, yes I was thinking of using AnimAll to animate the UV frames. I have never used the add-on for that before, but was oing to give it a shot if there was no nice and clean python root of switching them.

@Raco, I have never created a library in python/blender before. Essentially I assume it is just like a hast table with each picture entry in it’s own slot?

It all depends on how you’d like to structure data. I’ll explain later.

I’m sorry but something came up and I don’t have time this week. Hope someone else could be of assistance.

I’d simply change the object color via an animation. That said, I do have functiona for both completely changing the image, and for scrolling the image only in certain cases. I’ll go digging through my hard drive in a couple hours.

@Raco, okay thanks anyways. I am sure Mr. Google can help me to understand libraries at least a little.

@sdfg any sample code is great! Hopefully I can use it to change the image successfully, or better yet create a texture whenever my collision sensor is tripped (in the case of bullets or something hitting a wall and leaving a mark).

Woops, that was more than a few hours.
Code to completely change the image:

def setUVImage(obj, img):    try:
        del logic.texture
        '''Do Nothing'''
    mesh = obj.meshes[0]
    tex = mesh.getTextureName(0)
    url = bge.logic.expandPath(img)
    new_source = bge.texture.ImageFFmpeg(url)
    ID = bge.texture.materialID(obj, tex)
    object_texture = bge.texture.Texture(obj, ID)
    bge.logic.texture = object_texture
    bge.logic.texture.source = new_source

I wrote this a long time ago, and creating the variable in bge.logic looks like it may be unnecessary. Perhaps try changing that?

It appears that the code that I thought shifted the image to based on mouse hover actually just does a replaceMesh on it. If you look at a UV scroll script, and scroll it one way on sensor.positive and the other way on sensor.negative, it should do what you want.