Python - Set different ViewTemplate to multiple FloorPlanView

Good evening everyone,

I’m trying to set the parameter “ViewTemplate” to multiple FloorPlanView with a Python Node.

The catch is that I have a list with different ViewTemplate in it…

Right now, it’s looping well but overwritting to the last value in the list and not splitting it…

Those anybody had this problem or a similar one ?

Do I need a while loop ?

This is the code that I’m using :

import clr 

clr.AddReference("RevitAPI")
clr.AddReference("RevitServices")

from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

FloorPlanViews = UnwrapElement(IN[0])
FViewTemplates = UnwrapElement(IN[1])
Bool_NextAction = IN[2]

if Bool_NextAction == True:
	TransactionManager.Instance.EnsureInTransaction(doc)
	for i in FloorPlanViews:
		for j in FViewTemplates: 
			SetFViewTemplate = i.get_Parameter(BuiltInParameter.VIEW_TEMPLATE).Set(j.Id)
	TransactionManager.Instance.TransactionTaskDone()
	OUT= FloorPlanViews, True
else:
	OUT = "Nothing to do here"

@BenBimlogic Looks like it’s not working correctly because of the nested for loops (you definitely don’t need them). Try using the zip() function instead and see if it helps.

import clr 

clr.AddReference("RevitAPI")
clr.AddReference("RevitServices")

from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

FloorPlanViews = UnwrapElement(IN[0])
FViewTemplates = UnwrapElement(IN[1])
Bool_NextAction = IN[2]

if Bool_NextAction == True:
	TransactionManager.Instance.EnsureInTransaction(doc)
	for i,j in zip(FloorPlanViews,FViewTemplates): 
		SetFViewTemplate = i.get_Parameter(BuiltInParameter.VIEW_TEMPLATE).Set(j.Id)
	TransactionManager.Instance.TransactionTaskDone()
	OUT= FloorPlanViews, True
else:
	OUT = "Nothing to do here"

NOTE: It’s always a good idea to share a screenshot of your graph which shows the input lists.

2 Likes

Good morning AmolShah,

Thank you for your reply! I’m getting somewhere with your code! I thought the zip function only works with tuple and not list … :sweat_smile:

Here is the graph (what you can’t see in the image is a mirror from the floorplanview group but for ceilingplanview):

the first UI (I selected level 1 and 2 and repeat the two items so I can have 4 new floorplanview) :

image

The second UI (I selected two viewtemplate):

image

And the result that I’m having :

Only half of the views created got a viewtemplate …

image
image
image
image

Is it because I don’t have the same quantity of item from both list or I just don’t understand how the zip function works ? … ahah

Thank you again for your answer :slightly_smiling_face:

@BenBimlogic

For zip to work properly you need to have both lists of the same length. If not then it will follow shortest lacing. See how 3 got dropped below.
image

In your case you can use List.Cycle node to match the list length and get the intended result:

import clr 

clr.AddReference("RevitAPI")
clr.AddReference("RevitServices")

from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

toList = lambda x : x if hasattr(x, '__iter__') else [x]

FloorPlanViews = toList(UnwrapElement(IN[0]))
FViewTemplates = toList(UnwrapElement(IN[1]))
Bool_NextAction = IN[2]


if Bool_NextAction == True:
	TransactionManager.Instance.EnsureInTransaction(doc)
	for i,j in zip(FloorPlanViews,FViewTemplates): 
		SetFViewTemplate = i.get_Parameter(BuiltInParameter.VIEW_TEMPLATE).Set(j.Id)
	TransactionManager.Instance.TransactionTaskDone()
	OUT= FloorPlanViews, True

else:
	OUT = "Nothing to do here"

NOTE : I added toList() to make sure that the loop works even when it has only 1 element in it.

2 Likes

Thank you AmolShah for your time :beers: ! Everything working properly, I owe you a beer ahah! I’ll make sure to re-read the python documentation on list/tuple and zip ahah.

1 Like

@BenBimlogic Awesome! I’m glad I could be of assistance. Cheers :beers:

1 Like

Good day!
This is a great implementation. I applied it but one problem appeared.
I have such mistake after compilation:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Tracking (last call last):
File “”, line 34, in
EnvironmentError: System.Runtime.InteropServices.SEHException (0x80004005): An external component threw an exception.
at Element.setParameterValue (Element *, ElementId, ParameterValue *)
at Autodesk.Revit.DB.Parameter.Set (ElementId value)
at Microsoft.Scripting.Interpreter.FuncCallInstruction3.Run (InterpretedFrame) at Microsoft.Scripting.Interpreter.Interpreter.Run (InterpretedFrame) at Microsoft.Scripting.Interpreter.LightLambda.Run4 [T0, T1, T2, T3, TRet] (T0 arg0, T1 arg1, T2 arg2, T3 arg3) at System.Dynamic.UpdateDelegates.UpdateAndExecute3 [T0, T1, T2, TRet] (CallSite site, T0 arg0, T1 arg1, T2 arg2) at Microsoft.Scripting.Interpreter.DynamicInstruction4.Run (InterpretedFrame)
at Microsoft.Scripting.Interpreter.Interpreter.Run (InterpretedFrame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2 [T0, T1, TRet] (T0 arg0, T1 arg1)
at IronPython.Compiler.PythonScriptCode.RunWorker (CodeContext ctx)
at Microsoft.Scripting.Hosting.ScriptSource.Execute (ScriptScope scope)
at DSIronPython.IronPythonEvaluator.EvaluateIronPythonScript (string code, IList bindingNames, IList bindingValues)

line 34 is: SetFViewTemplate = i.get_Parameter(BuiltInParameter.VIEW_TEMPLATE).Set(j.Id)

My code is almost the same of yours.
What can I do with that?