Preview

This section describes how to prepare a channel to support previews. In the GUI a preview is generated when you click the preview button in the graph canvas. You can also test preview locally by including the "--preview" flag on the ana command that runs the channel.

To determine if a given run of a channel is in preview mode, you should check the context variable 'preview'. This will be set to True if the channel should generate a preview image. Typically this is done by taking the rendered image and generating a copy called "preview.png". The preview image should be stored in the root of the output directory.

Channels should be optimized so that preview times are minimal. This includes such things as reducing the number of render samples and decreasing the resolution of the image.

Here is an example of a blender channel that generates a preview. This code is in the render node that generates the image. It lowers the sample count to 10, cuts the resolution of the preview image in half, and also doesn't write annotation or metadata if preview is set. Note the node has one input which is a pointer to the AnaScene object.

from anatools.lib.node import Node
import anatools.lib.context as ctx
from PIL import Image, ImageFile

class Render(Node):
     def exec(self):
        ana_scene = self.inputs["Scene"][0]
        # cut the resolution in half
        if ctx.preview:
            bpy.context.scene.cycles.samples = 10
            bpy.context.scene.render.resolution_x = int(bpy.context.scene.render.resolution_x / 2)
            bpy.context.scene.render.resolution_y = int(bpy.context.scene.render.resolution_y / 2)
        # render the image
        bpy.ops.render.render(write_still=True, scene=ana_scene.blender_scene.name)
        # generate preview image
        if ctx.preview:
            filename = f'{ctx.interp_num:010}-{ana_scene.blender_scene.frame_current}-{ana_scene.sensor_name}.png'
            image_file = os.path.join(ctx.output, "images", filename)
            image = Image.open(image_file)
            image.save( os.path.join(ctx.output,"preview.png") )
        else:
            ana_scene.write_ana_annotations()
            ana_scene.write_ana_metadata()
        return {}

Last updated