Python Script Editor unstable (Revit 2025/ Dynamo v 3.3.0 )

Hi,

I always get wired problem when I use custom python scripte(CPython3) in dynamo.

I don’t think my scripte has such problem, because somethings it works.

import clr
import sys
import System
from System import Array
from System.Collections.Generic import *
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")

import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")

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

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk 
from Autodesk.Revit import DB 
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

lst_worksetsname = IN[0]

def create_worksets(d, names):
    if d.IsWorkshared:
        new_worksets = []
        for name in names:
            if WorksetTable.IsWorksetNameUnique(d, name):
               
                ws = DB.Workset.Create(d,name)
                new_worksets.append(ws)
        return new_worksets
        

def get_worksetsname(worksets):
    lst_ws_name = []
    for ws in worksets:
        name = ws.Name
        lst_ws_name.append(name)
    return lst_ws_name

TransactionManager.Instance.EnsureInTransaction(doc)



OUT = create_worksets(doc, lst_worksetsname)

TransactionManager.Instance.TransactionTaskDone()


any folks here have ideas?
how to fix the problem

It looks like you may be using code more suited to pyRevit.
Post your full code including imports.
Ensure you surround the entire code block with triple backticks (```) so that it displays properly
or paste the code, select/highlight all the code and hit the code format </> in the menu

Hi

In addition to what has been said,

  • your code is incomplete; the imports are missing.
  • Remove the ‘**’ characters from your code.
  • You should use the new Python engine, ‘PythonNet3’. (version 1.1.1 via package manager)

Hi Mike,

Thank you for your reply. I’m reposting my code below—could you please share any advice on how to fix the issue?

You’re correct: I usually write code for pyRevit. However, in this case I need to use an Excel file as an input, which is challenging with pyRevit. Unfortunately, my company does not allow us to install additional Python dependencies such as NumPy or Pandas to read Excel files.

Thanks in advance.

hi c.poupin,
Thank you for your reply, I will check the new python Engine.
I repost my code again with import.

Thx.

Is this a need or a want? CSV will be more portable, stable, robust, and allows using excel as the editor if that’s the goal.

Excel is not a mandatory requirement; CSV is fine.

Python can read CSV just fine, so you can skip excel. That said I’m not a fan on the Python 2 dependencies in that other tool - they’re a massive security risk.

I did a quick pass at a definition that attempts to make worksets by names here which should work just fine in the default Python engine from 2024 to 2027 (all supported builds). It might give you some ideas on the root cause as well as how to circumvent your issue.

########################################
############## Properties ##############
########################################
__author__ = 'Jacob Small'
__version__ = '0.1.0'
__description__ = "Generate worksets from a provided list of names."
__RevitBuilds__ = "2024, 2027"
__DynamoBuilds__ = "2.19, 4.0"
__ReleaseNotes__ = "POC for educational purposes only"
__Dependancies__ = "None"
__Copyright__ = "2026, Autodesk Inc."
__License__ = "Apache 2"



########################################
### Configure the Python environment ###
########################################
### standard imports ###
import sys #add the sys class to the Python environment so we can work with the sys objects
import clr #add the CLR (common language runtime) class to the Python environment so we can work with .net libraries
### Dynamo Revit imports ###
clr.AddReference("RevitNodes") #add Dynamo's Revit nodes library to the clr
import Revit #import Dynamo's Revit node class
clr.ImportExtensions(Revit.Elements) #add the element conversion methods to the CLR
clr.AddReference("RevitServices") #add the Revit services library to the CLR
import RevitServices #import the Revit services class to the Python environment
from RevitServices.Persistence import DocumentManager #import the document manager class to the Python environment 
from RevitServices.Transactions import TransactionManager #import the transaction manager class to the Python environment 
### Revit API imports ###
clr.AddReference("RevitAPI") #add the Revit API to the CLR
import Autodesk #add the Autodesk class to the Python environment 
from Autodesk.Revit.DB import * #import every class of the Revit API to the Python environment



#########################################
######## Classes and Definitions ########
#########################################
def GenerateWorksets(doc, names): 
    if doc.IsWorkshared: #if the document is workshared
        errors = [] #set error to a empty list
        results = [] #set results to an empty list
        newWorksetNames = [i for i in names if WorksetTable.IsWorksetNameUnique(doc, i)] #get the new workset names by filter out names not unique
        unusedNames = [i for i in names if not i in newWorksetNames] #get the list of used names by taking thsoe which were not unique
        if newWorksetNames: #if there are new workset names
            transaction = Transaction(doc, "Generate Worksets") #create a transaction to make the worksets
            transaction.Start() #start the transaction
            for name in newWorksetNames: #for each name
                try: #try
                    workset = Workset.Create(doc, name) #create the workset
                    results.append(workset) #append the workset to the results list
                except: #if the try fails
                    import traceback #import traceback
                    msg = traceback.format_exc() #format the exception
                    errors.append([name, msg]) #return the name that failed and the exception 
            transaction.Commit() #commit the transaciton
        else: #if all names were in use 
            errors.append("All workset names are already in use. Provide new list or use the ones you already have.") #append a helpful message to the errors list
        return {"Results":results, "Errors": errors}
    else: #if the document wasn't workshared
        sys.exit("\r\rDocument is not workshared. Enable worksharing and try again.\r\r") #close the python engine with an error message instructing the user on what to do



#########################################
###### Global variables and inputs ######
#########################################
doc = DocumentManager.Instance.CurrentDBDocument #the current Revit document
proviedWorksetNames = IN[0] #the list of workset names from IN[0] of the Dynamo environment
if not proviedWorksetNames.__class__ == list: proviedWorksetNames = [proviedWorksetNames] #ensure that workset names is a list



#########################################
###### Code execution starts here #######
#########################################
TransactionManager.Instance.ForceCloseTransaction() #force the transactions to close incase that's part of the issue
newWorksets = GenerateWorksets(doc, proviedWorksetNames) #try to generate the worksets



#########################################
##### Return the results to Dynamo ######
#########################################
OUT = newWorksets #return the new worksets to the Dynamo environment

Thank you jacob, looks fantastic !