attempt to modify the model outside of transaction
For some reason I can't post to Dynamobim forum. so I am posting here.
I wrote a python script that sets the parameter of some elements using the Set method of the parameter. It works for some elements but for most it doesn't with error message: "attempt to modify the model outside of transaction".
This is the whole code:
import System
import clr
clr.AddReference('RevitAPI')
clr.AddReference("RevitServices")
from Autodesk.Revit.DB import FilteredElementCollector
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
cats = doc.Settings.Categories
# lookup tabe accepts csv format such as: foo,bar
lookupTable = {line.split(",")[0]:line.split(",")[1] for line in IN[0].splitlines()}
param = IN[1]
typeLimit = None
try:
typeLimit = set(IN[2].splitlines())
except:
pass
modifiedElements = []
for cat in cats:
if cat.Name not in lookupTable:
continue
collector = FilteredElementCollector(doc)
collector.OfCategoryId(cat.Id)
for item in collector.ToElements():
if typeLimit and str(item.GetType()).split(".")[-1] not in typeLimit:
continue
for p in item.Parameters:
if p.Definition.Name == param:
#try:
p.Set(lookupTable[cat.Name])
modifiedElements.append(item)
#except:
#pass
OUT = modifiedElements
you need to use a transaction or our transaction manger - see this snippet: https://github.com/DynamoDS/Dynamo/wiki/Python-0.6.3-to-0.7.x-Migration#transactions
That worked like a charm! thanks for quick reply:)
BTW @mjkkirschner - That wiki page should be updated (there's stuff about units in there that nobody needs anymore), renamed to something like "Working with Python nodes" and added to the wiki menu. The two sections on Python nodes in the Primer don't contain all the juicy details, so this page is still the best available resource on Python nodes - if people can find it...
@andydandy74 "if people can find ..." I for one couldn't!