This example uses Metafold API/SDK to create a surface lattice and evaluate the shape metrics and export it as an STL file.
The Graph
The Metafold backend create shapes by consuming directed acyclic graphs where each node represents a series of mathematical computations or the initialization of source values. We can build graphs very quickly using the SDK. See here for details on current operations.
Every graph begins with the GenerateSamplePoints
node, which establishes a grid or set of points for sampling the volume. The final two operators in all graphs are redistance
and threshold
. TheRedistance
function transforms the grid of samples into signed distances from the zero level set, providing important distance data for further processing. Meanwhile, the Threshold
operation clamps sample values within a specified range centered around zero and normalizes them to the signed range [-1, 1], outputting the values as bytes. The SampleSurfaceLattice
function enables the sampling of a surface lattice shape within a defined space of specified sample points.
Steps
- Import all necessary libraries
- Insert project and token information
- Define the Scene parameters
- Set the surface lattice parameters and create a Metafold graph
- Evaluate metrics and export the part as STL
1- Import all necessary libraries
Start by importing the required libraries
import pandas as pd
import numpy as np
from pprint import pprint
from metafold import MetafoldClient
from metafold.func_types import JSONEvaluator
from metafold.func import *
2- Insert project and token information
In the following code snippet, we create a MetafoldClient
instance and assign it to the variable client
. This step is crucial for interacting with the Metafold API. The access_token
parameter should contain your authentication token, which grants you access to the Metafold service. Similarly, the project_id
parameter specifies the specific project or workspace you want to work with. Make sure to replace access_token
and project_id
with your own credentials and identifiers for this to work properly.
project_id = 5701
access_token = "..."
client=MetafoldClient(access_token,project_id)
3- Define the Scene parameters
To create a grid of sample points, GenerateSamplePoints
is used. The generated grid includes sample points at the boundary minimum and maximum as defined by the offset and size. The grid is also commonly referred to as a patch.
source = GenerateSamplePoints({
"offset": [ -15, -15, 0],
"size": [30, 30, 30],
"resolution": [128, 128, 128]
}
4- Set the Surface lattice parameters and create Metafold graph
shape_func= Threshold(
Redistance(
SampleSurfaceLattice(
PointSource,
{
"lattice_type": "Gyroid",
"scale": [10, 10 , 10],
"threshold": 0.0,
},
),
),
{
"width": 0.03,
},
)
The JSONEvaluator
is used to evaluate the graph
evaluator = JSONEvaluator(source)
shape_func(evaluator)
graph_json=evaluator.json()
5-Evaluate metrics and export
The job used to evaluate the relative_density
of the beam lattice can be given below
job= client.jobs.run("evaluate_metrics", {"graph": graph_json, "point_source": 0})
pprint((job.meta["relative_density"]))
An STL file can be exported using the following code
export_job = client.jobs.run("export_triangle_mesh", {
"graph": graph_json,
"point_source": 0,
})
export_asset = export_job.assets[0].id
client.assets.download_file(export_asset, "Gyroid.stl")
The STL file exported from python script