Skip to content
BIMpedia
Chapter 2 · Lesson 1 of 3Topic: Scripting and APIs

Your first script

Pick a task you do often by hand, write its steps in plain words, then build it in Dynamo or Python, and test on a copy. The steps are the hard part; the code is the easy part.

3 min read3 quiz questions
Skip to the quiz
  • Watch out
  • What it costs
  • Experienced readers
  • Remember
  • In BIMpedia
Adadata and automation lead
Even if you do this every day, watch for this: Edge cases and re-runs. First scripts work on the test model and fail on the real one: ties, missing values, and running twice.

Most BIM people who automate didn't start as programmers. They started with a task they were tired of doing by hand. The trick is to choose the right task, describe it precisely, and only then build it.

The automation ladder
  1. 1Written processThe steps, the inputs, the right result
  2. 2Checklist or templateSame result every time, still by hand
  3. 3Visual scriptDynamo or Grasshopper for one person’s task
  4. 4Team toolPython or pyRevit, tested, documented
  5. 5Scheduled serviceRuns on its own through APIs, feeds reports

Choose the task

Try it: is this worth automating?
Pays back after
9 weeks
Hours saved a year (46 working weeks)64

Tasks done often, by many people, with clear rules pay back fast. Rare or fuzzy tasks rarely do; for those, a checklist is the better automation.

Good first tasks are frequent (done every week or every issue), rule-based (no judgement) and easy to check (you can see if it worked): numbering rooms, renaming views, filling a parameter from another, listing elements that break a rule.

Write the steps in plain words

Task: number rooms on each level from left to right, as 03-001, 03-002…

  1. For each level, get all rooms on that level.
  2. Sort the rooms by the X coordinate of their location point; if two have the same X, sort by Y.
  3. For each room in order, build the number: the level code, a dash, a three-digit sequence starting at 001.
  4. Write the number into the room's Number parameter.
  5. Skip rooms with no level, and list them at the end.

Step 2's tie-breaker and step 5's skip are the edge cases. Most first scripts fail on exactly these.

Basics

In Dynamo, each step is a node (get elements, sort, build text, set parameter), connected left to right. In Python (for example with pyRevit), the same steps are about 20 lines of text. Both call the same Revit API underneath.

The same steps in Python

from collections import defaultdict
rooms = get_rooms()                     # all placed rooms
by_level = defaultdict(list)
skipped = []
for room in rooms:
    if room.level is None:
        skipped.append(room)            # step 5
    else:
        by_level[room.level.code].append(room)
for level, items in by_level.items():
    items.sort(key=lambda r: (r.x, r.y))      # step 2, with tie-breaker
    for i, room in enumerate(items, start=1):
        room.number = f"{level}-{i:03d}"      # steps 3 and 4
print("Rooms with no level:", [r.id for r in skipped])

get_rooms() and the room.x style properties stand for calls to the authoring tool's API; the shape of the logic is the point.

Adadata and automation lead
Your turn. Give it about 15 min and work it out on your own before you open the model answer: that is where it sticks.
Exercise: Specify a script 15 min

Your team fills the Department parameter of every room by hand from a spreadsheet: room number in one column, department in the other. It takes an hour per model update.

Write the plain-language steps of a script that does it, including at least three edge cases and what the script should do about each.

Key takeaways

  • Good first tasks are frequent, rule-based and easy to check: numbering, renaming, filling parameters, simple checks.
  • Write the steps in plain language first, including the edge cases.
  • Visual (Dynamo, Grasshopper) is a fine start; move to Python when the task becomes a team tool.
  • Test on a copy, check a sample, then run it for real, and keep a readme.

Your notes

Related tool

Define how rooms, sheets or assets are numbered and preview every new number.

Open Numbering Rule Builder →

Check yourself

3 questions · 2 right to complete
  1. 1. Which is the best first task to automate?
  2. 2. Before writing any code, what should you do?
  3. 3. A script numbers rooms by their X coordinate. Two rooms have the same X. What should the steps say?
0 of 3 answered