Hi,
I’m trying to bake a background to a texture, and am not using the VideoTexture module, I’m just taking a screenshot and save the image as the texture’s name. How do I update the textures?
I’m pretty sure VideoTexture is the only way to do this. Is there a reason you do not want to use it?
Edit: I should be more clear. VideoTexture is the only “correct” way to do this. I can think of at least one complicated workaround. It would be very preferable if you would use VideoTexture, but if there is some special reason you absolute cannot, I can recommend a kludge that should work (never tested it).
The non-bge way:
The BGE way:
Try adding a transparent texture layer (I suppose it will have to be re-made transparent after you end the game), save it to a file, and overwrite it with the “Baked” version - play with the types of texture layering to see if you can get the desired “combined” result.
Still, you will need some kind of “reload texture” option (or a simple “re-open texture” command, which should update it in a similar way to the one in the screenshot, except that it may work in BGE), which I don’t know if it exists - a possible alternative would be to restart scene/game or remove/re-add object.
For this to work, the texture would need to be an external file that could be modified and the scene would need to be completely re-instanced by the BGE (freed from memory and reloaded). The most seamless way to do this would be to use libLoad() and two separate “library” files to load an updated mesh from. This is the “complicated” method I referred to above.
I still don’t know if it will work, but I almost have a demo put together, so I will know soon. (I needed to do some tests with libLoad() for another project anyway. This was a great opportunity.)
I’m pretty sure VideoTexture is the only way to do this. Is there a reason you do not want to use it?
Well, maybe I’m not very familiar with it! Does it reload all textures in realtime?
I’ll have a quick look at your tuts…
I’ve attached a demo to this post. Open the main.blend file and run to see it work.
(Press “S” to load in the first mesh. Then press “T” to swap the external texture files. Once you have done that, press “S” to swap the meshes again.)
An explanation of how it works:
The image “texture.png” is a placeholder. Both “swap1.blend” and “swap2.blend” use that image to texture their mesh.
Pressing “T” causes “texture.png” to be overwritten with either “texture_1.png” or “texture_2.png” (they alternate based on a boolean)
Pressing “S” causes either “swap1.blend” or “swap2.blend” to be loaded into memory with LibLoad(). When the library file is initialized, it loads in the current “texture.png”. The mesh is switched using replaceMesh(), and then the opposite library is freed with LibFree().
Due to naming collisions, it is very important that the two meshes have different names for their materials. When you free a library, it erases the associated material datablocks, so if the mesh material datablocks have the same name, you will cause a segfault.
I imagine you are trying to use this functionality to take snapshots for save states. Hopefully you find my efforts of some assistance.
Attachments
texture_swap.zip (200 KB)
I don’t have any tutorials on VideoTexture, and I’ve had very poor success getting it to work on Linux, so I was wondering if your reason for not using it was related to something like that.
Anyway, if you don’t want to use VideoTexture my demo file should help. If you’d like to give VideoTexture a try, check the API Reference here:
http://www.blender.org/documentation/blender_python_api_2_62_release/bge.texture.html
Hey blendenzo, why not try running a windows blender executable with Wine to see if there are any changes…?
Before I download, I’ll explain what I’m trying to do:
I have 2 scenes. the background scene might have a large amount of objects, but they are spread in a large distance but need to be visible. So I generate the objects, take 6 screenshots of 6 sides of a cube and place the thextures on the sides of a skybox cube. So everytime the player on the main scene moves 10 units, the new background should be baked again.
Here’s the script:
import bge, random
from mathutils import *
from bge import render
from bge import texture
#print(dir(bge.render))
def ori():
sce= bge.logic.getSceneList()
p0=sce[0].objects[‘Player_ proxy’]
p1=sce[1].objects[‘Player’]
p0.position= p1.position; p0.orientation=p1.orientationdef main(cont):
own= cont.owner
sce= bge.logic.getSceneList()
p0=sce[0].objects[‘Player_ proxy’]
p1=sce[1].objects[‘Player’]
p0.position= p1.position; p0.orientation=p1.orientationif not "init" in own: own["init"]=1 sce[0].objects['Empt']["render"]=False pos=(1,0,0) posi=(int(p1.position[0]/10),int(p1.position[1]/10),int(p1.position[2]/10)) if pos!=posi: print(pos,posi,"baking...") gen_map() posi=(int(p1.position[0]/10),int(p1.position[1]/10),int(p1.position[2]/10)) posa= Vector(posi).copy() pos=(posa[0],posa[1],posa[2]) if pos!=posi: print(pos,posi,"baking...") gen_map()def gen_map():
rand=random.Random()
rand.seed(5)
scen= bge.logic.getSceneList()
sce= scen[0]plane= sce.objects['Plane'] plane.replaceMesh('o',0,0) s=sce.objects['Empty'] for a in range(1000): x=rand.uniform(-100,100) y=rand.uniform(-100,100) z=rand.uniform(-100,100) m=sce.addObject('Monkey', s); m.position= (x,y,z); m.setParent(s,0); m.color=(x,y,z,1) sce.objects['Empt']["render"]=Truedef renderScene():
scen= bge.logic.getSceneList()
sce= scen[0]if sce.objects['Empt']["render"]==True: print("ready") front= sce.cameras['Cam_front'];#print("f") back= sce.cameras['Cam_back'] top= sce.cameras['Cam_top'] bottom= sce.cameras['Cam_bottom'] right= sce.cameras['Cam_right'] left= sce.cameras['Cam_left'];#print("L") if sce.objects['Empt']["prop"]==1: sce.active_camera= back render.makeScreenshot("//back.png") IDback=texture.materialID(sce.objects['Cube.001'], "IMback.png") top_tex= texture.Texture(sce.objects['Cube.001'],3) pat= bge.logic.expandPath('//back.png') nsource= texture.ImageFFmpeg(pat) bge.logic.texture=top_tex; bge.logic.texture.source= nsource bge.logic.texture.refresh(False) if sce.objects['Empt']["prop"]==2: sce.active_camera= top render.makeScreenshot("//top.png") IDtop=texture.materialID(sce.objects['Cube.001'], "IMtop.png") top_tex= texture.Texture(sce.objects['Cube.001'],0) pat= bge.logic.expandPath('//top.png') nsource= texture.ImageFFmpeg(pat) bge.logic.texture=top_tex; bge.logic.texture.source= nsource bge.logic.texture.refresh(False) if sce.objects['Empt']["prop"]==3: sce.active_camera= bottom render.makeScreenshot("//bottom.png") IDtop=texture.materialID(sce.objects['Cube.001'], "IMbottom.png") top_tex= texture.Texture(sce.objects['Cube.001'],0) pat= bge.logic.expandPath('//bottom.png') nsource= texture.ImageFFmpeg(pat) bge.logic.texture=top_tex; bge.logic.texture.source= nsource bge.logic.texture.refresh(False) if sce.objects['Empt']["prop"]==4: sce.active_camera= left render.makeScreenshot("//Left.jpeg") if sce.objects['Empt']["prop"]==5: sce.active_camera= right render.makeScreenshot("//right.jpeg") IDtop=texture.materialID(sce.objects['Cube.001'], "IMright.png") top_tex= texture.Texture(sce.objects['Cube.001'],0) pat= bge.logic.expandPath('//right.png') nsource= texture.ImageFFmpeg(pat) bge.logic.texture=top_tex; bge.logic.texture.source= nsource bge.logic.texture.refresh(False) if sce.objects['Empt']["prop"]==6: sce.active_camera= front render.makeScreenshot("//front.png") IDtop=texture.materialID(sce.objects['Cube.001'], "IMfront.png") top_tex= texture.Texture(sce.objects['Cube.001'],1) pat= bge.logic.expandPath('//front.png') nsource= texture.ImageFFmpeg(pat) bge.logic.texture=top_tex; bge.logic.texture.source= nsource bge.logic.texture.refresh(False) if sce.objects['Empt']["prop"]==7: sce.active_camera= front sce.objects['Empt']["prop"]+=1 if sce.objects['Empt']["prop"]==7: sce.active_camera= front for a in sce.objects["Empty"].children: a.endObject() #sce.objects['Plane'].replaceMesh('1') sce.objects['Empt']["render"]=False #mat1=sce.objectsInactive['Cube.001'].materialID[0] print("clearing scene")
