Skip to content

Structure scripts and applications

Let the application choose the project. Let the functions doing the work receive the API clients they need. The same wall-counting function can then run from a standalone script, the Tapir palette, a desktop application, or a batch runner.

This is separation of concerns: project management and model operations have different jobs. Passing a client into a function is dependency injection. You do not need a framework to use either idea.

Give each layer one job

Layer Receives or creates Owns
Application entry point Creates MultiConn Application setup and passing the manager to the shell.
Application shell Receives MultiConn Discovery, project selection and switching, and launching scripts.
Script entry point Receives ConnHeader Checking metadata, showing project information, and passing clients to operations.
Reusable operation Receives UnifiedApi or other needed clients Reading or changing the Archicad model for one task.
---
config:
  themeCSS: |
    .label, .nodeLabel, .cluster-label {color: var(--md-default-fg-color, #333) !important; font-family: var(--md-text-font-family, sans-serif);}
    .cluster-label span, .cluster-label p {color: var(--md-default-fg-color, #333) !important;}
    .flowchart-link {stroke: var(--md-default-fg-color, #333) !important;}
    marker path {fill: var(--md-default-fg-color, #333) !important; stroke: var(--md-default-fg-color, #333) !important;}
    .edgeLabel, .edgeLabel p {background-color: var(--md-default-bg-color, #fff) !important; color: var(--md-default-fg-color, #333) !important;}
    .script .nodeLabel, .script .nodeLabel p {color: #fff !important;}
    .manual .nodeLabel, .manual .nodeLabel p {color: #000 !important;}
---
flowchart TD
    classDef manual fill:#8ecae6,stroke:#023047,stroke-width:2px,color:#000;
    classDef script fill:#023047,stroke:#8ecae6,stroke-width:2px,color:#fff;
    classDef empty fill:transparent,stroke:#8ecae6,stroke-width:2px;

    Start>Start application]:::empty
    Entry{{Entry point: create MultiConn}}:::script
    User[Choose project and task]:::manual
    subgraph App [Application]
        Shell{{Shell: discover and switch projects}}:::script
        Script{{Script entry point: check metadata}}:::script
        Operation{{Reusable operation: work with API clients}}:::script
        Shell -->|ConnHeader| Script
        Script -->|UnifiedApi or other clients| Operation
    end
    Archicad{{Target Archicad instance with Tapir}}:::script
    Start --> Entry
    Entry -->|MultiConn| Shell
    User --> Shell
    Operation -->|API commands| Archicad
    style App fill:transparent,stroke:#8ecae6,stroke-width:3px,stroke-dasharray:8 5

Read the arrows as "passes this object to the next layer." The shell holds the connection manager. The script receives the selected header. The operation receives clients, so it does not need to know about the shell or its UI.

Put each responsibility in a separate file

The inline example uses this layout:

application/
  main.py             # Creates MultiConn and the shell
  shell.py            # Owns project selection and starts scripts
  wall_script.py      # Receives ConnHeader and checks project metadata
  wall_operations.py  # Receives UnifiedApi and counts walls

The snippets below are the contents of the four files. Save them together in your Python project, follow the quick-start setup, and run uv run main.py from that folder. The example is read-only and uses normal script mode; it has no GUI framework.

wall_operations.py: do the work

from multiconn_archicad import UnifiedApi
from multiconn_archicad.models.official.types import ElementType


def count_walls(api: UnifiedApi) -> int:
    walls = api.official.element_listing.get_elements_by_type(ElementType.WALL)
    return len(walls)

The function does not choose a project or read UI state. It only needs a client. A standalone script can call count_walls(UnifiedApi()). A palette script can do the same, using the target port supplied by the palette.

wall_script.py: use project context

from wall_operations import count_walls

from multiconn_archicad import ConnHeader, is_session_ready


def run_script(header: ConnHeader) -> int:
    if not is_session_ready(header):
        raise RuntimeError("Wait for a ready Archicad project.")
    print(f"{header.archicad_id.projectName} | Archicad {header.product_info.version}")
    return count_walls(header.unified)

This is the script entry point used by the shell. It can inspect the project name and versions, then pass the needed clients to operations. This operation uses an official command, so the example checks session readiness without imposing a Tapir version. For a Tapir operation, check installation and only impose a minimum version when that task actually requires one.

If an operation also needs raw commands or the official wrapper, pass header.core or header.standard alongside header.unified. Its inputs should show which clients it uses.

shell.py: choose the project and run the script

from wall_script import run_script

from multiconn_archicad import MultiConn, Port


class ApplicationShell:
    def __init__(self, conn: MultiConn):
        self.conn = conn

    def select_project(self, port: Port) -> None:
        if port not in self.conn.open_port_headers:
            raise RuntimeError(f"No discovered instance at port {port}.")
        self.conn.primary = port

    def run_selected(self) -> int:
        header = self.conn.primary
        if header is None:
            raise RuntimeError("Open an Archicad project first.")
        return run_script(header)

The shell keeps the manager supplied at startup. A GUI project selector would call select_project(); a Run button would call run_selected() through its background-work mechanism. Opening or switching projects belongs in this layer.

main.py: assemble the application

from shell import ApplicationShell

from multiconn_archicad import MultiConn


def main() -> None:
    conn = MultiConn()
    shell = ApplicationShell(conn)
    print(f"Walls found: {shell.run_selected()}")


if __name__ == "__main__":
    main()

Example output, depending on the project:

Office building | Archicad 29
Walls found: 42

The last two lines of Python make main() run when this file is started as a script. Other files can import its definitions without starting the application; search for Python main guard for more detail.

Add a GUI or a batch runner

A GUI shell adds views, metadata events, background work, and cache invalidation when the project changes. Create its connection manager when the framework's UI event loop is ready. Do not copy this synchronous startup unchanged into a UI callback.

A headless pipeline replaces the interactive shell with a command-line entry point and processing stages. The GUI and headless guides show property workflows using the same header/client boundaries. A port identifies an instance, not a project that will stay open forever: finish current work before switching and obtain fresh context for the next run.