Setup Your Own Python Template

With Dynamo 2.0 we have the ability to specify a default template (.py extension) to use when opening the python window for the first time. This has been a long-desired request as this expedites the usage of Python within Dynamo. Having the ability to use a template allows us to have default imports ready to go when we want to develop a custom Python script.

The location for this template is in the APPDATA location for your Dynamo install.

This is typically as follows ( %appdata%\Dynamo\Dynamo Core\{version}\ ).

Setting Up The Template

In order to utilize this functionality we need to add the following line in our DynamoSettings.xml file. (Edit in notepad)

Where we see <PythonTemplateFilePath />, we can simply replace this with the following:

<string>C:\Users\CURRENTUSER\AppData\Roaming\Dynamo\Dynamo Core\2.0\</string>

Note: replace CURRENTUSER with your username

Next we need to build a template with the functionality that we want to use built-in. In our case lets embed the Revit related imports and some of the other typical items when working with Revit.

You can start a blank notepad document and paste the following code inside:

import clr

from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

from Autodesk.Revit.UI import *

from System.Collections.Generic import List

import Revit

import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

#Preparing input from dynamo to revit
element = UnwrapElement(IN[0])

#Do some action in a Transaction


OUT = element

Once that is done, save this file as in the APPDATA location.

Python Script Behavior After

A_f_ter the python template is defined, Dynamo will look for this each time a Python node is placed. If it is not found it will look like the default Python window.

If the Python template is found (like our Revit one for example) you will see all of the default items you built in.

Additional information regarding this great addition (by Radu Gidei) can be found here.

Last updated