Reading the monitor texture from Python
This guide shows how to read the texture generated by a monitor from Python using Pillow.
Monitor Setup
- Create a Camera (e.g. Perspective Camera).
- Change its name. The default name "Perspective" is also used by the Editor camera.
- Make sure that something is visible from the camera so that the Monitor can be tested.
- Create a Texture.
- A texture with the desired resolution can be created in an external application then imported.
- Create a Monitor extension and set its properties as described here:
- Camera Name = <The name of the camera>
- Texture Target = <The Texture that has been created>
- Enable Readback = True
The Readback Texture Format field can then be specified to ease integration via script. The example below supposes a RGB texture format.
Python Example
The following example is in Python 3. To use with Python 2, simply rename the Vortex module as VxSim and the bytes(...) function as buffer(...).
import Vortex import PIL.Image import os {...} def readMonitorTexture(): # Here, inputTextureExtension is an input extension field to which the monitor target texture is connected. texture = Vortex.Texture.dynamicCast(inputTextureExtension) image = texture.getImage() data = bytes(image.getImageBytes()) width = image.getWidth() height = image.getHeight() pilImage = PIL.Image.frombytes("RGB", (image.getWidth(), image.getHeight()), data) pilImage = pilImage.transpose(PIL.Image.FLIP_TOP_BOTTOM) # Image is flipped vertically. # The image is ready to be used pilImage.save("image.png")