This template repository is for a Speckle Automate function written in Python using the specklepy SDK to interact with Speckle data.
This template contains the full scaffolding required to publish a function to the Automate environment. It also has some sane defaults for development environment setups.
- Use this template repository to create a new repository in your own / organization's profile.
- Register the function
This implementation reads elements from the Program Block family in the triggering model and creates a pie chart grouped by type using area totals.
chartTitle: Optional chart title.
All detected Program Block types are included automatically in the chart with their:
- Type share based on total
Area - Colors from each type's
RGB_COLORtype parameter (Type Parameters -> Text) - Fallback colors when an
RGB_COLORvalue is missing - Robust area lookup fallback (
AREA,Area,Gross Area,Program Area) across parameter tree
Version 0.2.x Features:
- Program Block family filtering
- Pie chart distribution by area (not element count)
- RGB_COLOR driven pie/legend colors per type
- Pie chart and legend show each type's area share as percentage
The function also stores program_block_area_summary.csv containing:
- Program Block Type name
- Total AREA per Type
- Percentage of total AREA
- RGB color used in the chart
Chart legend formatting:
- Legend entries show only Program Block Type names
- Legend is placed below the pie chart in horizontal layout
If no valid area values are found, the function stores program_block_area_diagnostics.txt
with parameter paths checked and exclusion counts.
The function stores revit_colors_pie_chart.png as a file result in the Automation run.
To add new Python package dependencies to the project, edit the pyproject.toml file:
For packages your function needs to run (like pandas, requests, etc.):
dependencies = [
"specklepy==3.0.0",
"pandas==2.1.0", # Add production dependencies here
]For development tools (like testing or formatting tools):
[project.optional-dependencies]
dev = [
"black==23.12.1",
"pytest-mock==3.11.1", # Add development dependencies here
# ... other dev tools
]How to decide which section?
- If your
main.py(or other function logic) imports it →dependencies - If it's just a tool to help you code →
[project.optional-dependencies].dev
Example:
# In your main.py
import pandas as pd # ← This goes in dependencies
import specklepy # ← This goes in dependencies
# You won't import these in main.py:
# pytest, black, mypy ← These go in [project.optional-dependencies].devDescribe how the launch.json should be edited.
Create a new repo from this template, and use the create new code.
- Create a new Speckle Automation.
- Select your Speckle Project and Speckle Model.
- Select the deployed Speckle Function.
- Enter the function input:
chartTitle. - Click
Create Automation.
- Register your Function with Speckle Automate and select the Python template.
- A new repository will be created in your GitHub account.
- Make changes to your Function in
main.py. See below for the Developer Requirements and instructions on how to test. - To create a new version of your Function, create a new GitHub release in your repository.
- Install the following:
- Run the following to set up your development environment:
python -m venv .venv # On Windows .venv\Scripts\activate # On macOS/Linux source .venv/bin/activate pip install --upgrade pip pip install .[dev]
What this installs:
- All the packages your function needs to run (
dependencies) - Plus development tools like testing and code formatting (
[project.optional-dependencies].dev)
Why separate sections?
dependencies: Only what gets deployed with your function (lightweight)devdependencies: Extra tools to help you write better code locally
The code can be tested locally by running pytest.
This template uses the modern PEP 621 standard in pyproject.toml, which works with all modern Python dependency managers:
poetry install # Automatically reads pyproject.tomluv sync # Automatically reads pyproject.tomlpip-compile pyproject.toml # Generate requirements.txt from pyproject.toml
pip install -r requirements.txtpdm install # Automatically reads pyproject.tomlAdvantage: All tools read the same pyproject.toml file, so there's no need to keep multiple files in sync!
Running and testing your code on your machine is a great way to develop your Function; the following instructions are a bit more in-depth and only required if you are having issues with your Function in GitHub Actions or on Speckle Automate.
The GitHub Action packages your code into the format required by Speckle Automate. This is done by building a Docker Image, which Speckle Automate runs. You can attempt to build the Docker Image locally to test the building process.
To build the Docker Container Image, you must have Docker installed.
Once you have Docker running on your local machine:
-
Open a terminal
-
Navigate to the directory in which you cloned this repository
-
Run the following command:
docker build -f ./Dockerfile -t speckle_automate_python_example .
Once the GitHub Action has built the image, it is sent to Speckle Automate. When Speckle Automate runs your Function as part of an Automation, it will run the Docker Container Image. You can test that your Docker Container Image runs correctly locally.
-
To then run the Docker Container Image, run the following command:
docker run --rm speckle_automate_python_example \ python -u main.py run \ '{"projectId": "1234", "modelId": "1234", "branchName": "myBranch", "versionId": "1234", "speckleServerUrl": "https://speckle.xyz", "automationId": "1234", "automationRevisionId": "1234", "automationRunId": "1234", "functionId": "1234", "functionName": "my function", "functionLogo": "base64EncodedPng"}' \ '{}' \ yourSpeckleServerAuthenticationToken
Let's explain this in more detail:
docker run—-rm speckle_automate_python_example tells Docker to run the Docker Container Image we built earlier. speckle_automate_python_example is the name of the Docker Container Image. The --rm flag tells Docker to remove the container after it has finished running, freeing up space on your machine.
The line python -u main.py run is the command run inside the Docker Container Image. The rest of the command is the arguments passed to the command. The arguments are:
'{"projectId": "1234", "modelId": "1234", "branchName": "myBranch", "versionId": "1234", "speckleServerUrl": "https://speckle.xyz", "automationId": "1234", "automationRevisionId": "1234", "automationRunId": "1234", "functionId": "1234", "functionName": "my function", "functionLogo": "base64EncodedPng"}'- the metadata that describes the automation and the function.{}- the input parameters for the function the Automation creator can set. Here, they are blank, but you can add your parameters to test your function.yourSpeckleServerAuthenticationToken—the authentication token for the Speckle Server that the Automation can connect to. This is required to interact with the Speckle Server, for example, to get data from the Model.
- Learn more about SpecklePy and interacting with Speckle from Python.