Getting a Python script to work on multiple objects/elements/views?

I have a python script that works beautifully when given a single Schedule View, but I am having one heck of a time trying to get it to work with a list of views. At this point I’m pretty stumped.

When the script receives a single Schedule View input, it will search for a particular column by name, and proceed to check the “Hidden Field” box in order to hide said field. I’m newer to python so this very well could be the issue, but how do I get the script to loop for the remainder of the list of views?

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
from Revit.Elements import *
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.GeometryReferences)
import System
from System.Collections.Generic import *

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

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

view = doc.ActiveView

def U(elem): 
	a = UnwrapElement(elem)
	return a
#IN[0] = ViewSchedule
#x = 0
S = IN[0]
for x, val in S:
	print S
schedule = UnwrapElement(S)
colName = "Weight (ea)";


definit = schedule.Definition
countParameters = definit.GetFieldCount()
# Parameter names
names = []
for i in range(countParameters):
	Parname = definit.GetField(i).ColumnHeading # Parameter Column name
	if Parname == colName:
		index = i

field = schedule.Definition.GetField(index) # schedule field

TransactionManager.Instance.EnsureInTransaction(doc)
field.IsHidden = IN[1] # 1 = Hide, 0 = Show
TransactionManager.Instance.TransactionTaskDone()
OUT = schedule

It’s usually best to unwrap your elements at the input.

S = UnwrapElement(IN[0])
schedule = S

At this point there’s really no reason to define S and schedule individually. It’s redundant for a single item. However, if you have a list of input views you would create a for loop:

S = UnwrapElement(IN[0])
for schedule in S:
    [rest of code to be run on each schedule view]

If you had an output for each schedule you would likely need another container list for each of those outputs. But in this case it looks like you’re just returning the initial schedule or list of schedules.

I had started out with the schedule like you have suggested. Unfortunately it gives me an error. IN[0] is a list of 13 Schedule Viewports. The only way I can get it to run without issue is by specifying an item within the list like so
schedule = UnwrapElement(IN[0][0])

And this is the error that it gives me.

Lazy option which I think will most likely work:

Put it in a custom node. Set list level to @1.

1 Like

You didn’t do the second part which is to create a for loop to run through each schedule in the list.

Setting the list level to @1 didn’t seem to help.

@Nick_Boyts Sorry I missed that. When I put in the for loop like you suggested, if gives me an “Unexpected Token ‘=’” error. I’m assuming it doesn’t like my formatting?

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
from Revit.Elements import *
clr.ImportExtensions(Revit.GeometryConversion)
from Revit.GeometryConversion import *
clr.ImportExtensions(Revit.GeometryReferences)
from Revit.GeometryReferences import *
import System
from System.Collections.Generic import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import *
#TransactionManager

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

view = doc.ActiveView

def U(elem): 
	a = UnwrapElement(elem)
	return a

colName = "Weight (ea)";

S = UnwrapElement(IN[0])
for schedule in S:
	[definit = schedule.Definition
	countParameters = definit.GetFieldCount()
# Parameter names
	names = []
	for i in range(countParameters):
		Parname = definit.GetField(i).ColumnHeading # Parameter Column name
		if Parname == colName:
			index = i
	
	field = schedule.Definition.GetField(index) # schedule field
	
	TransactionManager.Instance.EnsureInTransaction(doc)
	field.IsHidden = IN[1] # 1 = Hide, 0 = Show
	TransactionManager.Instance.TransactionTaskDone()
	]

OUT = schedule

Remove the brackets from lines 38 and 52. That’s why it’s throwing an exception.

1 Like

Yeah. Sorry, the brackets aren’t part of the code.
Just the usual indentation is needed.

That certainly helped. But I’m starting to think Dynamo likes seeing me sad.

hold up
I caught my mistake
I think

No problem

There are only two inputs in the node (IN[0] and IN[1]), therefore IN[2] does not exist.

Well boys we did it! Thank you all!

It successfully hides all “Weight (ea)” schedule columns, or unhides!
I had been working on this since Friday. Thank you @Nick_Boyts and @cgartland and @PauLtus

3 Likes