Help error in a python node

Good morning, I had this routine but it wasn’t me who developed it, and in an attempt to use it in revit 2025 it is reporting this error, could anyone help me?

import clr
import math

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
#The inputs to this node will be stored as a list in the IN variables.
if isinstance(IN[0], list):
        fittings = UnwrapElement(IN[0])
        toggle = 0
else:
        toggle = 1
        fittings = [UnwrapElement(IN[0])]
        
        
def getConnSysType(connector):
        domain = connector.Domain
        if domain == Domain.DomainHvac:
                return connector.DuctSystemType.ToString()
        elif domain == Domain.DomainPiping:
                return connector.PipeSystemType.ToString()
        elif domain == Domain.DomainElectrical:
                return connector.ElectricalSystemType.ToString()
        else:
                return None
        

p = []
dir = []
fd = []
ref = []
conns = []
descript = []
H = []
W = []
R = []
MEP = []
Sys = []
Shap = []
sysClass = []

for fitting in fittings:
        
        
        points = []
        direction = []
        flowdir = []
        refs = []
        connlist = []
        description = []
        height = []
        width = []
        radius = []
        MEPSystem = []
        systemType = []
        shape = []
        systemclass = []
        
        try:
                connectors = fitting.MEPModel.ConnectorManager.Connectors
        except:
                try:
                        connectors = fitting.ConnectorManager.Connectors
                except:                 
                        p.append(None)
                        dir.append(None)
                        fd.append(None)
                        ref.append(None)
                        conns.append(None)
                        descript.append(None)
                        H.append(None)
                        W.append(None)
                        R.append(None)
                        Shap.append(None)
                        MEP.append(None)
                        Sys.append(None)
                        sysClass.append(None)
                        continue
        for conn in connectors:
                connlist.append(conn)
                description.append(conn.Description)
                try:
                        height.append(conn.Height)
                        width.append(conn.Width)
                        radius.append(None)
                except:
                        try:
                                radius.append(conn.Radius)
                                height.append(None)
                                width.append(None)
                        except:
                                radius.append(None)
                                height.append(None)
                                width.append(None)
                shape.append((conn.Shape).ToString())
                
                try:
                        MEPSystem.append(conn.MEPSystem.Name)
                        systype = doc.GetElement(conn.MEPSystem.GetTypeId())
                        systemType.append(systype)                      
                except:
                        MEPSystem.append(None)
                        systemType.append(None)
                
                try:            
                        systemclass.append(getConnSysType(conn))
                except:
                        systemclass.append(None)
                
                points.append(conn.Origin.ToPoint())
                direction.append(conn.CoordinateSystem.BasisZ.ToVector())
                
                try:
                        flowdir.append(conn.Direction.ToString())
                except:
                        flowdir.append(None)
                for c in conn.AllRefs:
                        refs.append(c.Owner)
        p.append(points)
        dir.append(direction)
        fd.append(flowdir)
        ref.append(refs)
        conns.append(connlist)
        descript.append(description)
        H.append(height)
        W.append(width)
        R.append(radius)
        MEP.append(MEPSystem)
        Sys.append(systemType)
        Shap.append(shape)
        sysClass.append(systemclass)


#Assign your output to the OUT variable.
if toggle:
        OUT = points, flowdir, refs, direction, connlist, description, height, width, radius, MEPSystem, systemType, shape, systemclass
else:
        OUT = p,fd,ref, dir, conns, descript, H, W, R, MEP, Sys, Shap, sysClass
import clr
import math

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference(“RevitAPI”)
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
#The inputs to this node will be stored as a list in the IN variables.
if isinstance(IN[0], list):
fittings = UnwrapElement(IN[0])
toggle = 0
else:
toggle = 1
fittings = [UnwrapElement(IN[0])]

def getConnSysType(connector):
domain = connector.Domain
if domain == Domain.DomainHvac:
return connector.DuctSystemType.ToString()
elif domain == Domain.DomainPiping:
return connector.PipeSystemType.ToString()
elif domain == Domain.DomainElectrical:
return connector.ElectricalSystemType.ToString()
else:
return None

p = []
dir =[]
fd =[]
ref =[]
conns =[]
descript =[]
H =[]
W =[]
R =[]
MEP =[]
Sys =[]
Shap =[]
sysClass =[]

for fitting in fittings:
points = []
    direction = []
    flowdir = []
    refs = []
    connlist = []
    description = []
    height = []
    width = []
    radius = []
    MEPSystem = []
    systemType = []
    shape = []
    systemclass = []
    
    try:
            connectors = fitting.MEPModel.ConnectorManager.Connectors
    except:
            try:
                    connectors = fitting.ConnectorManager.Connectors
            except:                 
                    p.append(None)
                    dir.append(None)
                    fd.append(None)
                    ref.append(None)
                    conns.append(None)
                    descript.append(None)
                    H.append(None)
                    W.append(None)
                    R.append(None)
                    Shap.append(None)
                    MEP.append(None)
                    Sys.append(None)
                    sysClass.append(None)
                    continue
    for conn in connectors:
            connlist.append(conn)
            description.append(conn.Description)
            try:
                    height.append(conn.Height)
                    width.append(conn.Width)
                    radius.append(None)
            except:
                    try:
                            radius.append(conn.Radius)
                            height.append(None)
                            width.append(None)
                    except:
                            radius.append(None)
                            height.append(None)
                            width.append(None)
            shape.append((conn.Shape).ToString())
            
            try:
                    MEPSystem.append(conn.MEPSystem.Name)
                    systype = doc.GetElement(conn.MEPSystem.GetTypeId())
                    systemType.append(systype)                      
            except:
                    MEPSystem.append(None)
                    systemType.append(None)
            
            try:            
                    systemclass.append(getConnSysType(conn))
            except:
                    systemclass.append(None)
            
            points.append(conn.Origin.ToPoint())
            direction.append(conn.CoordinateSystem.BasisZ.ToVector())
            
            try:
                    flowdir.append(conn.Direction.ToString())
            except:
                    flowdir.append(None)
            for c in conn.AllRefs:
                    refs.append(c.Owner)
    p.append(points)
    dir.append(direction)
    fd.append(flowdir)
    ref.append(refs)
    conns.append(connlist)
    descript.append(description)
    H.append(height)
    W.append(width)
    R.append(radius)
    MEP.append(MEPSystem)
    Sys.append(systemType)
    Shap.append(shape)
    sysClass.append(systemclass)

# 🏈 Assign your output to the OUT variable.
if toggle:
OUT = points, flowdir, refs, direction, connlist, description, height, width, radius, MEPSystem, systemType, shape, systemclass
else:
OUT = p,fd,ref, dir, conns, descript, H, W, R, MEP, Sys, Shap, sysClass

paste your code to
grafik

1 Like

The ToString() method is an old IronPython method. You can use str() instead, which is the standard Python method to stringify a value.

myValue.ToString() => str(myValue)

3 Likes

I don’t understand, should I paste this text on line 106?

You should replace the ToString() method with the str() method wherever it’s used.

it’s a Pythonnet2 issue, temporarily try this workaround

def getConnSysType(connector):
        domain = connector.Domain
        if domain == Domain.DomainHvac:
                return System.Enum.GetName(DuctSystemType, connector.DuctSystemType)
        elif domain == Domain.DomainPiping:
                return System.Enum.GetName(PipeSystemType, connector.PipeSystemType)
        elif domain == Domain.DomainElectrical:
                return System.Enum.GetName(ElectricalSystemType, connector.ElectricalSystemType)
        else:
                return None

this is fixed with PythonNet3 and as I indicated in this example

alternatively, you can also use IronPython3

2 Likes

none of the alternatives worked

Ok - why didn’t they work? What was the error message? Can you post what you tried?

https://drive.google.com/drive/folders/1jVU1d4r1Subjih8IMe9zC3ED6Q6E4f7Q?usp=sharing

here in the link above is the rvt file and python so we can observe the erro.

see now

Hi @tiago_cslsemobs

following your last dyn, you switcjed the engine to IronPython2 so it seems that you have another problem

  • try to reduce the number of nodes
  • if the problem persists start a new topic with the dyn and the rvt file (it is missing in your link)

I apologize, I already attached the rvt file to the link

Hi,

After switching Revit to Portuguese and running your script with your Revit file, I don’t see any errors. Make a new topic if necessary

yes once it works but after you date the revit and open the project again it stops working

There are some issues with the code:
Overwriting buitlin types and imported classes dir and MEPSystem with empty lists will cause errors.
Rather than trying / excepting are there checks in the API?
Here’s how to get connectors with a function

def get_connectors(element):
    # Requires an element with connectors
    if isinstance(element, FamilyInstance) and element.MEPModel:
        return element.MEPModel.ConnectorManager.Connectors
    if isinstance(element, MEPCurve):
        return element.ConnectorManager.Connectors

I’ve taken the liberty to refactor your code

Refactored code
import clr

from System import Enum

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Mechanical import DuctSystemType
from Autodesk.Revit.DB.Plumbing import PipeSystemType
from Autodesk.Revit.DB.Electrical import ElectricalSystemType

clr.AddReference("RevitNodes")
import Revit

clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)


def getConnSysType(connector):
    domain = connector.Domain
    if domain == Domain.DomainHvac:
        return Enum.GetName(DuctSystemType, connector.DuctSystemType)
    if domain == Domain.DomainPiping:
        return Enum.GetName(PipeSystemType, connector.PipeSystemType)
    if domain == Domain.DomainElectrical:
        return Enum.GetName(ElectricalSystemType, connector.ElectricalSystemType)


def get_connectors(element):
    # Requires an element with connectors
    if isinstance(element, FamilyInstance) and element.MEPModel:
        return element.MEPModel.ConnectorManager.Connectors
    if isinstance(element, MEPCurve):
        return element.ConnectorManager.Connectors
    # Element is not a fitting or MEP element
    return str(type(element))


doc = DocumentManager.Instance.CurrentDBDocument

fittings = UnwrapElement(IN[0]) if isinstance(IN[0], list) else [UnwrapElement(IN[0])]

results = []
for fitting in fittings:
    connectors = get_connectors(fitting)
    if not connectors:
        # Element does not have connectors
        results.append([[fitting.Id, connectors]] + [None] * 13)
        continue

    for conn in connectors:
        item_results = []
        item_results.append(conn.Owner.Id)
        item_results.append(conn)
        item_results.append(conn.Description)
        item_results.append(Enum.GetName(ConnectorProfileType, conn.Shape))

        if conn.Shape == ConnectorProfileType.Round:
            item_results.extend([None] * 2)
            item_results.append(conn.Radius)
        elif conn.Shape in [
            ConnectorProfileType.Rectangular,
            ConnectorProfileType.Oval,
        ]:
            item_results.append(conn.Height)
            item_results.append(conn.Width)
            item_results.append(None)
        else:
            item_results.extend([None] * 3)

        if conn.MEPSystem:
            item_results.append(conn.MEPSystem.Name)
            mepsystype = doc.GetElement(conn.MEPSystem.GetTypeId())
            item_results.append(mepsystype)
        else:
            item_results.extend([None] * 2)

        item_results.append(getConnSysType(conn))
        item_results.append(conn.Origin.ToPoint())
        item_results.append(conn.CoordinateSystem.BasisZ.ToVector())
        item_results.append(Enum.GetName(FlowDirectionType, conn.Direction))
        item_results.append(list(conn.AllRefs))

        results.append(item_results)

# Check for single result
if len(results) == 1:
    OUT = results[0]
else:
    # This will return a list of information collected by connector
    OUT = results

    # This will return a list of information collected by values
    # Order of values:
    # fitting, connector, description, shape, height, width, radius, mepsystem, mepsystemtype, systemclass, origin, direction, flowdir, refs
    OUT = zip(*results)

Good morning, which of the nodes did you modify, can you put the file here?

I reviewed the code at the top of this thread.
I did have a look at the dyn, however I could not untangle the logic to be able to provide a review.
You can use the code in the drop down
image


your code didn’t work

I mean, the python script node isn’t showing an error. Nor can we see that’s happening as the previews below the node are not visible. Its not easy to tell whats happening here when you dont show the output nor the error messages