[SOLVED] Dynamic Texture

(Daedalus_MDW) #1

well, im throwing in the towel on this one.

on one of my computers it crashes right away in 2.74 and the other it works fine. in 2.78c it doesnt do anything on either.

both computers are pretty much the same software and blender wise.

if someone could tell me why its not working that would be great.

EDIT: apparently its working just fine on 2.74, still not sure about other versions.

PS: there is also globalDict support, just delete the “#” in front of the logic.loadGlobalDict() and logic.saveGlobalDict() in the Main.py in “Py”.


DynTexture.zip (107 KB)

(sdfgeoff) #2

Works fine for me.

However, looking at the code: Don’t create singletons. Don’t store things inside bge.logic or bge.texture if you can help it. In fact, I’d say never do it. (different to what I would have said a year ago though).
I’d also advise looking at pep8, just to make your python code consistant with everyone elses. only constants should be capitalised.

Perhaps this function could be of use to you:

def replace_image(obj, path, mat_id, tex_id):
    '''Replaces the image associated with a mesh'''
    # The texture has to be stored in a place associated with other game data
    # so we store it in a game property. This name inlucdes the mat ID's and
    # tex ID's so that a single object with a complex setup does not have the
    # textures overwrite each other.
    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
        tex = obj[prop_name]

    # Load the image from the path
    raw = bge.texture.ImageFFmpeg(path)

    # Check to see that it loaded
    if raw.status != bge.texture.SOURCE_PLAYING:
        # Error in loading image
        raise ValueError("Unable to load image at {}".format(path))

    # Assign the new image to the texture and update the texture.
    tex.source = raw
    return tex

It will load the image from disk each time, so you may want to create a cache around the bge.texture.ImageFFmpeg call. Something like this may work:

import functools

@functools.lru_cache(max_size=128)  # If you have more than 128 images at the same time, increase this number
def load_image(path):
    return bge.texture.ImageFFmpeg(path)

(Daedalus_MDW) #3

funny, i tried it again today and works just fine on both computers now… :spin:

this was just supposed to be a little experiment, not too worried about all that religious code stuff. yeah, i know about pep stuff, but i dont like it. plain and simple as that. i follow my own standard, which i think is perfectly appropriate for a hobbyist like myself.

now i agree if i had any intention on making a job of it or contributing to a project then pep8 would be best.

(sdfgeoff) #4

i follow my own standard, which i think is perfectly appropriate for a hobbyist like myself.

If you’re going to follow a standard, why not use an ‘official’ one? The only cost is the week or two it takes for your fingers to learn when to hit the shift key…
(I started off with my own style as well, and also found PEP8 to look ‘ugly’, but changed when I started working with large codebases and wanted to use consistency checking tools)