Tutorials

Convert an OBJ file to STL

The sample code below reads an OBJ file and passes it to our API telling the API to convert it to STL format.

The sample below assumes you have exported your API token as an environment variable like so.

export KITTYCAD_API_TOKEN="YOUR-API-TOKEN"

Replace YOUR-API-TOKEN with one of your API tokens. It looks like you need to generate an API Token first.

Below is our API-client code:

Python
Go
#!/usr/bin/env python3


from typing import Dict

from kittycad.api.file import create_file_conversion
from kittycad.client import ClientFromEnv
from kittycad.models.base64data import Base64Data
from kittycad.models.error import Error
from kittycad.models.file_conversion import FileConversion
from kittycad.models.file_export_format import FileExportFormat
from kittycad.models.file_import_format import FileImportFormat
from kittycad.types import Unset

# Create a new client with your token parsed from the environment variable:
#   KITTYCAD_API_TOKEN.
client = ClientFromEnv(timeout=500, verify_ssl=True)

# Convert a file from OBJ to STL.
# Read in the contents of the file.
file = open("./ORIGINALVOXEL-3.obj", "rb")
# LITTERBOX-END-NON-EDITABLE-SECTION
content = file.read()
file.close()

result = create_file_conversion.sync(
    client=client,
    body=content,
    src_format=FileImportFormat.OBJ,
    output_format=FileExportFormat.STL,
)

if isinstance(result, Error) or result is None:
    raise Exception("There was a problem")

fc: FileConversion = result

print(f"File conversion id: {fc.id}")
print(f"File conversion status: {fc.status}")

if isinstance(fc.outputs, Unset):
    raise Exception("Expected outputs to be set")

if fc.outputs is None:
    raise Exception("Expected outputs to be set")

outputs: Dict[str, Base64Data] = fc.outputs

if len(outputs) != 1:
    raise Exception("Expected one output file")

# LITTERBOX-START-NON-EDITABLE-SECTION
for _, output in outputs.items():
    output_file_path = "./output.stl"
    print(f"Saving output to {output_file_path}")
    output_file = open(output_file_path, "wb")
    output_file.write(output.get_decoded())
    output_file.close()
Found a typo?