Writing TextureBuffer to disk

Hi all,

My question is simple, I’m trying to create a texture to use within my in-game generated models and I’m trying to experiment with a few methods. I tried generating a texture by create some geometry, parenting it to a hidden window, then writing the texture of that hidden window out to disk, however, nothing gets written out:

       tex=Texture()
       mybuffer = base.win.makeTextureBuffer("My Buffer", 512, 512,tex,to_ram=True)
       mybuffer.setSort(-100)
       mycamera = base.makeCamera(mybuffer)
       myscene = NodePath("My Scene")
       mycamera.reparentTo(myscene)
       self.activePieceModel.reparentTo(myscene)
       file_name=Filename.fromOsSpecific("save_gameDat_001.png")
       mybuffer.saveScreenshot(file_name)

Also, using this method, still nothing is written out:

       p=PNMImage()
       tex=mybuffer.getTexture()
       tex.store(p)       
       p.write("test_001.png")

I did set to_ram to True too as indicated, but still, nothing is written out. Am I missing something?

Also, the texture generated this way will have an alpha-channel present, correct? This is important since I’d like some parts of my generated texture to be transparent.

(I know one can purely use the PNMIMAGE class and I already am, but as I said, I’m trying out different methods to achieve the same thing.)

Thanks in advance.

I think you should call GraphicsEngine.renderFrame() to actually draw something on the texture.

1 Like

Thank you, indeed adding:

base.graphicsEngine.renderFrame()

solved my problem. So the modified code will be:

       tex=Texture()
       mybuffer = base.win.makeTextureBuffer("My Buffer", 512, 512,tex,to_ram=True)
       mybuffer.setSort(-100)
       mycamera = base.makeCamera(mybuffer)
       myscene = NodePath("My Scene")
       mycamera.reparentTo(myscene)
       self.activePieceModel.reparentTo(myscene)
       file_name=Filename.fromOsSpecific("save_gameDat_001.png")
 
       base.graphicsEngine.renderFrame()

       mybuffer.saveScreenshot(file_name)

It should be added before saving the screenshot. Thank you maxxim!

1 Like