Reading the monitor texture from Python

This guide shows how to read the texture generated by a monitor from Python using Pillow.

Monitor Setup

  1. 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.
  2. Create a Texture.
    • A texture with the desired resolution can be created in an external application then imported.
  3. 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

This example shows how to save the image in Python using Pillow. Because the default Python interpreter provided by Vortex doesn't include Pillow, make sure to follow these steps to use your own Python environment.

The following example is in Python 3. To use with Python 2, simply rename the Vortex module to VxSim and the bytes(...) function to 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")