Python script only runs when Dynamo is open

Hi Everyone,

i’m working on a script that transfer grids from a linked model to the current one.
It has been working fine all day during my itesting and itterating but all of a sudden it only works when i open dynamo… ( not even a dynamo project, just dynamo home screen is already enough).

Anybody has any idea what’s happen here?
I run the script trough pyrevit

It seems like the script can’t find the Active Document when dynamo is closed.

script.py (1.4 KB)

## IMPORT LEVEL & GRIDS ##

#IMPORTS
import sys
import System

from itertools import compress

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

import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *


#Document information
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
docs = app.Documents

#docs & docnames
titles,documents = [],[]

for dc in docs:
	if dc.Title != doc.Title:
		documents.Add(dc)
		titles.Add(dc.Title)
	else:
		docu=dc
		titl=dc.Title

documents = [docu]+documents
titles = [titl]+titles

#filter doc for linked files
docmask = []

for i in titles:
	docmask.Add(i == doc.Title)

docmask = [not i for i in docmask]
_linkDoc = list(compress(documents,docmask))

#retrieve grids from Linked model
try:
    grids = FilteredElementCollector(_linkDoc[0]).OfCategory(BuiltInCategory.OST_Grids).WhereElementIsNotElementType().ToElements()
except:
    print("No Grids Where found in the Linked model")

print ("----------")

#retrieve gridsCurves
curves,gridnames = [],[]
for i in grids:
	curves.Add(Grid.Curve)
	gridnames.Add(Grid.Name)
	
print(curves)
print("-------NAMES-----")
print(gridnames)

I’ve seen a few threads with similar issues, pyrevit in later builds seems to not always run dynamo successfully due to addin conflicts. Try disabling most of them to see if one may be preventing it from working.

The pyrevit forums or github may be a better place to get help or report a potential issue to the developer.

2 Likes

Just use the pyRevit wrapper for document.

uidoc = __revit__.ActiveUIDocument 
doc = __revit__.ActiveUIDocument.Document

You don’t have to load the dynamo references, so it will load faster. There isn’t anything in this script that needs dynamo. And yes - in my experience you need dynamo open to reference its objects.

And FWIW, I am getting memory issues between running pyRevit IronPython after something in Dynamo using CPython.

3 Likes

Thx for the Forum link Gavin!
I will have a look over there and see if i can find some more inforamtion :slight_smile: .

1 Like

Hi Aaron,

Your feedback is certainly true, but my problem is that the script react differently when Dynamo is open or closed.
Which I find very strange, especially since it has nothing to do with it (as you say).

Well, it does. All of this is Dynamo. So., Dynamo is needed. Zap that and replace RevitServices.Persistence import DocumentManager with what I listed, and everything will run fine.

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *

Thx Aaron this solves the dynamo depencie but it doens’t give me the linked doc’s anymore only the active current doc.

Just use the api.
rvtLinks = FilteredElementCollector(doc).OfClass(RevitLinkInstance).ToElements()
Returns all instances.
rvtLinks = FilteredElementCollector(doc).OfClass(RevitLinkType).ToElements()
Returns the types.