How to get the url of an image?

Hi,

For every object I want to get the image of the first texture of the first material.

Is it only possible to get the image through the url, like this?

from bge import logic, texture

url = logic.expandPath('//Images\untitled.png')
img = texture.ImageFFmpeg(url)
img_data = img.image
img_array = texture.imageToArray(img, 'RGB')
img_size = img.size
...

Or is there maybe another way?

Edit: changed title and question


from bge import logic , texture

#the usual
cont = logic.getCurrentController()
own = cont.owner

#get the first member in the objects mesh list
mesh = own.meshes[0]

#get the name of the first texture in the first material in the mesh
mesh_name = mesh.getTextureName(0)

#using the texture module, get the texture using the name from above
texture.materialID(own,mesh_name)

Relevent api pages.
http://www.blender.org/documentation/blender_python_api_2_68_2/bge.types.KX_GameObject.html#bge.types.KX_GameObjecthttp://www.blender.org/documentation/blender_python_api_2_68_2/bge.types.KX_MeshProxy.html#bge.types.KX_MeshProxy

http://www.blender.org/documentation/blender_python_api_2_68_2/bge.texture.html

Wow, that’s such a relief. Thanks, GGentzel. I searched the API but must have skipped it somehow, it’s the first time I notice getTextureName().

However, I don’t understand your approach. You can’t get the texture with:

texture.materialID(own,mesh_name)

With this you only get the material id.

For now I’m getting the image path through the texture name. But I still wonder if there’s a way to get the image in a more direct way. I still have to predefine the path of the folder in which images are stored and that’s unfortunate. I don’t want to be dependent on such things, because I may want to store images in separate folders. Blender ‘knows’ the url, so I was hoping there was something in the API for this.

The following function stores the data and size of an image so it could be edited later on (for splats, footsteps, cracks, …).

def create_texture(own):
    url = logic.expandPath('//Images/Textures/Color/')
    tex_name = own.meshes[0].getTextureName(0)
    img_path = url + tex_name[2:]
    img_source = texture.ImageFFmpeg(img_path)
    img_buffer = texture.imageToArray(img_source, 'RGB')
    img_size = list(img_source.size)
    tex = texture.Texture(own, 0, 0)
    tex.source = texture.ImageBuff()
    tex.source.load(img_buffer, img_size[0], img_size[1])
    tex.refresh(False)
    own['tex'] = [tex, img_size]

As you can see, this only works assuming that the texture name and image name are identical. This may not be always the case. Anyone knows of a better solution, also without having to predefine a path? Or even get the url of a packed image?

Yep. I forgot a line.


from bge import logic , texture

#the usual
cont = logic.getCurrentController()
own = cont.owner

#get the first member in the objects mesh list
mesh = own.meshes[0]

#get the name of the first texture in the first material in the mesh
mesh_name = mesh.getTextureName(0)

#using the texture module, get the material holding the texture using the name from above
matID = texture.materialID(own,mesh_name)

#using the texture module, the texture
wanted_texture = texture.Texture(own, matID)

I double checked this one. It works.

Thanks for your reply, GGentzel. My purpose is to store the data of the image as an array of bytes. I can get this with texture.ImageFFmpeg(img_path), but I need the image path for this. That’s why I hoped to get the image another way, unless I could get the image path through the texture itself. However, I think what you’re showing is how to create a new (empty) texture object, isn’t it? Or am I mistaken and could I retrieve the image data and size from its source, somehow?

I think I’m at a loss here.

Hello, anyone out there who could confirm that it isn’t possible to get an image source (or its path) directly?