← API Guide / Script API and JSON

Script API and JSON

Script.add(), native parameters, export, import and trusted Python loading.

Build an ordered native command sequence

Script.add(command, **params) appends one command and returns the same Script, so calls can be chained. The command ID and keyword names are passed through unchanged. They are SolidCraft's names rather than aliases invented by SCPC.

from scpc import Script

script = Script()
script.add("sketch.create", plane="XY", name="Base")
script.add("sketch.finish")
assert len(script) == 2

Inspect and save commands

script.commands returns a detached copy of the ordered command entries. to_dict() returns the full native document; to_json() serialises it to readable UTF-8 JSON; save(path) creates the destination directory if needed and writes the file.

commands = script.commands
document = script.to_dict()   # {"commands": [...]}
json_text = script.to_json()
script.save("output/commands.json")

Import JSON without running Python

SCPC can reopen the native JSON format directly, which is useful for comparing generated scripts or replaying a fixed command sequence. Import is structural, not CAD validation.

restored = Script.load("output/commands.json")
restored = Script.from_json(json_text)
restored = Script.from_dict(document)

The input must have exactly a top-level commands array; every entry must contain a nonempty command string and a params object. Malformed JSON, extra structural fields, unsupported Python values and non-finite numbers are rejected. The original order and parameter values are preserved.

Loading a Python model

load_script("model.py") executes a trusted Python file and finds its single nonempty Script instance. Loading .json with Script.load() does not execute Python. Never evaluate an unknown Python model as untrusted input.

from scpc import load_script

plan = load_script("examples/plate/model.py")
print(len(plan), plan.commands[0]["command"])

What this API does not do

SCPC does not implement geometry, named edge selectors, automatic coordinate transforms or separate Fillet and Hole Python classes. It only serialises native command requests. The native engine decides whether an operation is valid.