Skip to content
AlphaToolBox3D is in early alpha: features are incomplete and things can break. Use it at your own risk and keep copies of your files. Report a problem
ToolBox3D

Learn · Scripting · Lesson 3 of 3

Batch-convert models with tbpy

Turn a folder of OBJ files into GLB files without opening a window. The script uses the same import and export code as the editor's File menu, so the results match what you would get by hand.

  • 15 minutes
  • Intermediate
  • Windows app
Illustration

1. What tbpy is

tbpy is ToolBox3D's headless Python, like Maya's mayapy. It starts a ToolBox3D session without a window and runs your script against it with the same import toolbox3d as tb API as the Script Editor. It needs Python 3.12 or newer: set TOOLBOX3D_PYTHON to a python.exe, or have the py launcher or Python on your PATH.

tbpy -c "import toolbox3d as tb; print(tb.app.is_headless)"
True

2. Convert one file

Save this as convert.py:

import sys
import toolbox3d as tb

src, dst = sys.argv[1], sys.argv[2]
roots = tb.io.import_file(src)
meshes = tb.scene.objects(type="mesh")
print("imported", len(roots), "root(s),", sum(o.mesh.vertex_count for o in meshes), "vertices")
for path in tb.io.export(dst):
    print("wrote", path)

Run it with the input and output paths:

tbpy convert.py crate.obj crate.glb

tb.io.import_file adds the file's objects to the scene, like the Model editor's Import Model. tb.io.export picks the format from the extension and returns every file it wrote — formats with companion files, such as OBJ with its .mtl, write them next to the main file.

3. Convert a folder

A tbpy run is one session, so start each file from an empty scene with tb.io.open, which replaces the document like File › Open:

import pathlib
import sys
import toolbox3d as tb

folder = pathlib.Path(sys.argv[1])
out = folder / "glb"
out.mkdir(exist_ok=True)
for src in sorted(folder.glob("*.obj")):
    mtl = src.with_suffix(".mtl")
    warnings = tb.io.open(str(src), companions=[str(mtl)] if mtl.exists() else [])
    for w in warnings:
        print(f"{src.name}: {w['message']}")
    for path in tb.io.export(str(out / (src.stem + ".glb")), embed_textures=True):
        print("wrote", path)
tbpy convert_folder.py D:\assets\props

4. Check and fail loudly

For pipelines, the exit status matters. An uncaught exception ends tbpy with status 1, a usage error with 2, and 3 means Python or the session could not start. Use sys.exit for your own checks:

if not meshes:
    print(f"{src}: no meshes imported", file=sys.stderr)
    sys.exit(4)

Calls that need a window, such as tb.ui.prompt, raise tb.HostCapability under tbpy, so a batch script never waits for an answer nobody can give.

Next steps

Every export option — pose, axis, clips, textures — is listed under tb.io.export in the API reference.