Tags: Snippets (2)

Logging In Python Dynamo Node

19 July 2017 - 08:11 pm
Let's talk logging in dynamo.

Earlier, I realized earlier on that debugging is difficult if you aren't logging your output. Unlike ghPython in grasshopper, python in dynamo doesn't currently natively have a console to "print" to. So to handle this I have a log and clearLog() function I always tend to use. This writes to a text file path that you will see in the code.

My Typical Log for dynamo

# <------------ LOG DYNAMO STUFF -------------------->#
import time,datetime

#<----- EDIT BELOW --->

LOGFILE = "C:\Path\To\A\log.txt" #path to save text
LOG_ACTIVE = True #Setting to False will disable the log

# <---- END EDIT ---->

def log(textArr,is_raw=False):
activeScript = ""
#---- prefix to identify time and script running ---#
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
precursor ="[" timestamp "] --> "
#---- end prefix --->
filename = LOGFILE
if not isinstance(textArr, (list, tuple)):
textArr = [textArr]
target = open(filename, 'a ')
for i in textArr:
if (not is_raw):

def clearLog():
filename = LOGFILE
target = open(filename, 'w ')

# <------------ LOG DYNAMO STUFF -------------------->#

Log Example

Now you can call these two functions like so. Each log call, will print a single line of text

#clear the log. usually I run this at the beginning of my script.
#simple single values
log("hello world")
#multiple concatenated values via a list or tuple (will automatically give text version)
foo = 5
log(["hello the value of foo is: ",foo, "!!!"])

I then usually open it in a browser to easily refresh the value of the log.txt file. Boom, your own external console.

Disabling the Log

After debugging your scripts, you'll obviously want to disable logging features. To Disable the log, change the LOG_ACTIVE value to False. This will not write to any file when the log functions are called, without you having to delete or comment them out.

LOG_ACTIVE = False #Setting to False will disable the log

Revit Python Shell vs. Dynamo Python Node

13 February 2017 - 10:18 pm
Let's start off with something more or less simple, 2 code snippets. I find that a lot of the times these days I write a lot of python code, usually because of its versatility--it can be used on windows, mac, in rhino, in revit python shell, or dynamo, etc.You can even use it on the web, etc.. BUT lets talk in terms of revit, There are two ways you can use python with the Revit API.

Two ways of using python with revit

1. Revit Python Shell - The revit python shell, is essentially a downloadable add-in available here. This is more like conventional scripting, and if your used to rhinoscript/python script or any kind of traditional sequential scripting, this is synonymous to having a script written with your favorite text editor and then running it. Essentially you can write scripts, and then run them via buttons/ shortcuts etc.

2. Dynamo Python node - Alternatively, you can run python in a dynamo definition, so if your familiar with grasshopper or visual programming, or if your script is more in need of this type of interface, this is a good bet. plus its native, so no add-in is necessary. This pretty much functions the same way ghPython for grasshopper works. It is a node with customizable inputs and outputs, double clicking into the node, allows you to add your code.

Base Code

The building coder and proving ground provide a great resource for these two ways of using python. But I found it useful to save this snippet of code to know how to convert between all in the same place. I sometimes start out creating a dynamo graph, and then realize its more useful as a revit python shell script (I.E. in the ribbon as a button or used as a shortcut). OR vice versa. It turns out its not so different in how you set up your base code, which for most api calls, the below set of variables, dependency libraries, etc will be useful. I have converted many scripts to and from dynamo and rps, its super easy!

Typical Revit Python Shell Script

You can see here a typical RPS script base. Often you will not have to import too many libraries as you will in dynamo. API and APIUI will be enough. Also note the global var __revit__ and __window__. These are not set in the dynamo interface. __revit__ is your access into various revit globals. __window__ is actually a console (text terminal) that appears when you run a script. if you print something, for instance, it should show up here. Generally I close this in my scripts, as it is not how I like to debug, and it is generally not useful unless you are doing so.

#Typical python revit shell

#import routine
import clr
from Autodesk.Revit.DB import *

#important vars, revit python shell version
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
view = doc.ActiveView

#revit python shell has a console, access it like so

Typical Dynamo Node

Dynamo nodes for python generally take a lot more imports of nodes, especially if your'e affecting the document i.e. transactions.

#Typical dynamo node

#import routine
import clr
# Import RevitNodes
import Revit

# Import RevitAPI
import Autodesk
from Autodesk.Revit.DB import *

# Import Revit elements
from Revit.Elements import *

# Import DocumentManager
import RevitServices
from RevitServices.Persistence import DocumentManager

# important vars, dynamo version
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
app = uiapp.Application
view = uidoc.ActiveView

Chris Malcolm

Chris Malcolm is an aspiring architect, web developer, and designer originally from Florida. A MIT M.Arch Graduate, he currently is based in San Francisco, employed by Skidmore Owings and Merrill. He enjoys creating tools both professionally and for fun. As for tooling, architecturally speaking, Rhino and Grasshopper is his first love, with revit,dynamo,passion as close seconds. Outside of architecture tooling, he loves all things javascript, html, python, and regular expressions.

More projects and info at my main site: