Dynamo Python Code Error

Below is a code I am working on, but I am getting an error related to Ironpython and somehow on how I define the doc variable. I am rather new to coding with dynamo, so any help would be appreciated!

Here is the error:
IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 6, in
NameError: name ‘revit’ is not defined

Here is the code:

import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import FilteredElementCollector, SectionType, ViewSection, Transaction

# Get the current Revit document
doc = __revit__.ActiveUIDocument.Document

# Get the selected element
selection = __revit__.ActiveUIDocument.Selection
element = doc.GetElement(selection.GetElementIds().FirstElement())

# Define the section type
sectionType = SectionType.Elevation

# Create a transaction for the section view creation
transaction = Transaction(doc, "Create Section View")
transaction.Start()

# Create the section view
sectionView = ViewSection.CreateSection(doc, sectionType, element.Id)

# Commit the transaction
transaction.Commit()

# Set the section view as the active view
__revit__.ActiveUIDocument.ActiveView = sectionView

Here is a good python script template that @john_pierson covered in one of his training sessions. Has helped me greatly. It shows how to load all of the primary references you would need as well as some other nice tidbits. Once you have your code completed and working, then you can weed out the references that you don’t need to load.

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

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

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

TransactionManager.Instance.TransactionTaskDone()

OUT = element
2 Likes

Thank you! I still get the error saying “revit” is not defined. Any thoughts?

Hopefully someone will correct me if I am wrong.

Because you have the r in Revit as lowercase, the code is not recognizing revit as a type of reference, so it doesn’t know what to do with it. Change the r to uppercase in order to pull references from Revit.

FYI, the code I added has the option of getting the current document.

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

Hello @CHumphreyVWEB7 and welcome

in addition to @staylor 's answer, your code uses variables specific to pyRevit or RevitPythonShell ( __revit__ ) for Dynamo it’s different

1 Like