This example uses Metafold API/SDK to create a beam 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 SampleLattice
function enables the sampling of a beam lattice within a defined space of specified sample points.
Steps
- Import all necessary libraries
- Insert project and token information
- Define the Scene parameters
- Set the beam lattice parameters
- Create the 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 beam lattice parameters
In the following example, the lattice_data contains the edges and nodes of the lattice, providing the structural definition of the BCC arrangement. The section_radius
parameter defines the radius of the lattice sections, while the scale
parameter scales the lattice in three dimensions using a uniform factor.
bcc_params= {
"lattice_data" : {
"edges": [
[0, 7],
[4, 7],
[1, 7],
[2, 7],
[5, 7],
[6, 7],
[3, 7],
[7, 8]
],
"nodes": [
[0, 0, 0],
[0, 1, 0],
[1, 0, 0],
[1, 1, 0],
[0, 0, 1],
[0, 1, 1],
[1, 0, 1],
[0.5, 0.5, 0.5],
[1, 1, 1]
]
},
"section_radius" : 0.1,
"scale": [5, 5, 5]
}
6- Create the Metafold graph
We now use the operators defined earlier in the graph section to create our Metafold shape
shape_func= Threshold(
Redistance(
SampleLattice(source, parameters=bcc_params)
),
{
"width": 0.03,
},
)
JSONEvaluator
is used to evaluate the graph
evaluator = JSONEvaluator(source)
shape_func(evaluator)
graph_json=evaluator.json()
7- 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, "bcc.stl")
The STL file exported using this python script