Overview

The Asset type in the generative.core package is used to store any digital asset, which are easily created from files, using FileAsset or web assets, using Asset.

Assets are mainly used to store outputs from Generative Functions, and let complex data outputs like geometry, images and videos to be visualised in the app.

Supported file formats

While any digital file can be stored as an Asset, only certain formats are viewable in the app.

The app tries to detect automatically the correct way of viewing assets, and should be able to for all the supported formats below. Occasionally you’ll need to tell the app how to view assets, but either way you don’t need to think about it when defining your function.

If you want to view a file format that isn’t listed here, please contact us.

Geometry

The following geometry formats can be viewed in the app as 3D models:

  • glb and glTF, which can be output by most CAD platforms

Images and Videos

In general, Assets of any image or video format that web browsers support viewing can be visualised in the app.

Formats that are (almost) universally supported are:

  • Images: gif, jpg, png
  • Videos: MP4, WebM

Further information on what formats browsers support for images and videos.

File Assets

FileAsset is a subtype of Asset used to handle files accessible locally from the system where your Generative Function is running.

A FileAsset is a type of Asset. Any assets returned by Generative Functions should have typing Asset rather than FileAsset

Creating a File Asset from an existing file

To create an Asset from an existing file, simply pass the path to the file into FileAsset. For example, to create an asset out of the file at my-existing-file.png:

from generative.core import FileAsset

existing = FileAsset("my-existing-file.png")

Paths can be relative to the project folder (where your Function Sever runs), which is normally the top level folder of the project, or absolute.

Creating and writing to a new File Asset

If you’re creating a file within your Generative Function, you can generate an empty FileAsset instance first, which will create a file in a temporary location, then write to it. This avoids having to manually deal with the paths. For example the following code could be added to the cantilever example to export a 3D model of the cantilever bounding box, which would then be viewable in the app:

from generative.core import FileAsset, Asset
import trimesh

def cantilever_3d_model(geom: CantileverGeometricProperties) -> Asset:
    # Only the outer bounding-box of cantilever modelled for simplicity
    cantilever_mesh = trimesh.creation.box(extents=(geom.width_m, geom.length_m, geom.depth_m))
    asset = FileAsset()
    cantilever_mesh.export(str(asset.path), file_type="glb")
    return asset

The correct type annotation for assets is always Asset. FileAsset is used to create assets inside functions but it can’t be used as a type annotation (because they are only valid locally).

When an Asset created using FileAsset is returned from a Generative Function that has been run from the app, the file is saved securely to cloud.

Tools that rely on global state, like Matplotlib’s pyplot, can cause memory leaks and crashes when handling used in Generative Functions. Make sure an alternative is used.

For example for Matplotlib, initialise each figure with Figure() to ensure each Generative Function evaluation gets an isolated, reliable plot without interfering with others.