Search code examples
pythonautodeskrevit

The managed object is not valid - Dynamo Crashes


The following error in Dynamo is bugging me since many hours.

Warning:

IronPythonEvaluator.EvaluateIronPythonScript operation failed.

Traceback (most recent call last):

File " < string > ", line 33, in < module>

Exception: The managed object is not valid.

enter image description here

I’m not sure why the error has occur would appreciate if someone share its solution with me. Thank you


Solution

  • import clr
    
    clr.AddReference('ProtoGeometry')
    import Autodesk.DesignScript.Geometry
    
    # Import Element wrapper extension methods
    clr.AddReference("RevitNodes")
    import Revit
    
    clr.ImportExtensions(Revit.Elements)
    
    # Import geometry conversion extension methods
    clr.ImportExtensions(Revit.GeometryConversion)
    
    # Import DocumentManager and TransactionManager
    clr.AddReference("RevitServices")
    import RevitServices
    from RevitServices.Persistence import DocumentManager
    from RevitServices.Transactions import TransactionManager
    
    doc = DocumentManager.Instance.CurrentDBDocument
    uiapp = DocumentManager.Instance.CurrentUIApplication
    app = uiapp.Application
    
    # Import RevitAPI
    clr.AddReference("RevitAPI")
    from Autodesk.Revit.DB import *
    
    import System
    from System import Array
    from System.Collections.Generic import *
    
    import sys
    
    pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
    sys.path.append(pyt_path)
    

    The above are my standard imports while working with Dynamo/Revit API. It has been working for me for a while.

    In all honesty I wish I had an "actual" answer why your script is not working. The only thing that is different than mine, is order in which you reference certain things. That should have no effect on their validity. It's Dynamo though, and it's very capricious.