Creating Primitives

Creating Primitives

This example uses Metafold API/SDK to create a primitive shape and evaluate the shape metrics and export 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 SampleBox function enables the sampling of a primitive shape within a defined space of specified sample points.

image

Steps

  1. Import all necessary libraries
  2. Insert project and token information
  3. Define the Scene parameters
  4. Set the primitive shape and size
  5. Create the Metafold graph
  6. 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, FuncType, Vec3f, Mat4f
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.) More information about Metafold’s implicit representations can be found here.

source = GenerateSamplePoints({
						"offset": [ -1, -1, -1],
						"size": [2, 2, 2],
						"resolution": [256, 256, 256]
					}

4- Set the primitive shape and size

The next step is setting up parameters for a box shape using a dictionary named box_params. The rotation of the box is defined with an array box_rot as [0, 0.0, 0.0], while the position is represented by the array box_pos as [0.0, 0.0, 0.0]. The dimensions of the box are specified as [2, 2, 2].

To create the transformation matrix for the box, we utilize the xform function with the rotation and position values, and then flatten the matrix before incorporating it into the box_params dictionary under the "xform" key.

This setup in the box_params dictionary encapsulates all the necessary information for defining a box shape within a 3D space, including its size, placement, and orientation through a transformation matrix.

For the shape type, box can be replaced by Cylinder, BoxFrame, Ellipsoid or other shapes supported by Metafold, you can find a detailed description here.

box_rot = np.array([0, 0.0, 0.0])
box_pos = np.array([0.0, 0.0, 0.0])
box_params = {
    "shape_type": "Box",
    "size": [2, 2, 2],
    "xform": xform(box_rot, box_pos).flatten()
}

5- Create the Metafold graph

We now use the operators defined earlier in the graph section to create our Metafold shape

shape_func= Threshold(
                Redistance(
                    SampleBox(source, parameters=box_params)
                )
            )    

JSONEvaluator is used to evaluate the graph

evaluator = JSONEvaluator(source) 
shape_func(evaluator)
graph_json=evaluator.json()

6- Evaluate metrics and export

Use the evaluate_metrics job to evaluate the approximate interior volume and surface area of the implicit surface defined by the given Metafold shape definition.

job= client.jobs.run("evaluate_metrics", {"graph": graph_json, "point_source": 0})

use pprint to print the desired information from evaluate_metrics, in this specific example “relative_density” was retrieved.

pprint((job.meta["relative_density"]))

An STL file can be exported using the export_job

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, "box.stl")

The STL file exported from python script

image