1
$\begingroup$

Following the few examples I found on the web, I managed to whip up this code:

import bge

contr = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
obj = contr.owner

texture = None
if "render_to_texture" in obj:
    texture = obj["render_to_texture"]
else:
    camera = scene.objects['Camera']

    # create texture
    matID = bge.texture.materialID(obj, 'MAcam_render_material')
    texture = bge.texture.Texture(obj, matID)

    # create source
    texture.source = bge.texture.ImageRender(scene, camera)
    texture.source.capsize = [640, 480]
    texture.source.background = [255, 255, 255, 255]

    obj["render_to_texture"] = texture

print(texture)                                         # prints <VideoTexture.Texture object at ...>
print(bge.texture.imageToArray(texture.source, 'RGB')) # prints 'None'

# force a refresh
pixels = bytearray(640 * 480 * 4)
texture.source.refresh(pixels, "RGBA")

# print(pixels)                        # just prints an array of zeroes

if(texture.source.valid):              # always 'False'
    print(texture.source.image)
else:
    print("No image available")

And placed this script to be run every few ticks on a plane object with the material 'cam_render_material' on it. But no matter what, I get empty arrays and None as a result

I've been pounding my head against the desk for the past three hours and heading nowhere. If someone has an hints, they'd be greatly appreciated.

I've added the blender file in question here, if someone wants to check it out

$\endgroup$
  • $\begingroup$ Make sure you aren't using the scene's active camera. You can only use a non-active camera with ImageRender. Also make sure the viewport is in Textured mode. $\endgroup$ – sdfgeoff Jan 12 '18 at 7:16
  • $\begingroup$ @sdfgeoff Yes, I'm aware of that. There are two cameras in the scene: Camera and Camera.001. The latter is the active one. Thanks anyways <3 $\endgroup$ – Francisco Jan 12 '18 at 7:19
0
$\begingroup$

All I needed to do in your blend file was put the viewport in textured mode, then it started working. Other things you may like to consider:

  • It takes one frame for things to start happening, and because you're printing so many values, you have to wait a while.
  • the .valid attribute doesn't seem to work for ImageRender (it does for ImageFFMPEG though).
  • 480*480*4 is nearly a million values. So if it looks like you're always getting 0's or 0xff's, you may not be. Try with a smaller image so you can see more of the data values
  • Consider unwrapping the plane and assigning it a shadeless texture so you can preview what is being displayed (you'll have to run texture.resresh(True) for it to update the texture instead of the buffer).
  • Consider switching to GLSL mode
$\endgroup$
  • $\begingroup$ You sir, are a gentleman and a scholar <3 $\endgroup$ – Francisco Jan 12 '18 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.