Loop not working result index error not solvable

Goodday

Im having trouble with applying a loop to my python script.

What im trying to accomplish is to fill in 2 (Instance) parameters of a family placed in a family, and after it fills in the parameters it needs to save the family and continue with the next value’s in the list.

This process needs to be repeated the amount of time it gets input from my excel sheet, in this example there are only 2 options (Those options are defined in a code block not through excel).

Placing a single tube and filling in the parameters and saving it works (Script: Single Version - Working)

But once i add the loop to the script it start to give me the error as you can see in the screenshot.


Containing:

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

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

doc = DocumentManager.Instance.CurrentDBDocument
name = IN[0]
compact = IN[1]
newcentral = IN[2]
isworkshared = IN[3]
elmts = IN[4]
parmNam = IN[5]
val = IN[6]

for i in range(len(name)):
	Revit.Elements.Element.SetParameterByName(elmts[i], parmNam[0], val[i][0])
	Revit.Elements.Element.SetParameterByName(elmts[i], parmNam[1], val[i][1])

	TransactionManager.Instance.ForceCloseTransaction()
	if doc.IsFamilyDocument:
		name += '.rfa'
	else:
		name += '.rvt'	
	opt = SaveAsOptions()
	opt.OverwriteExistingFile = True
	opt.Compact = compact
	if isworkshared and newcentral:
		wsopt = WorksharingSaveAsOptions()
		wsopt.ClearTransmitted = True
		wsopt.SaveAsCentral = True
		opt.SetWorksharingOptions(wsopt)
	try:
		doc.SaveAs(name, opt)
		OUT = 'done'
	except:
		try:
			wsopt.ClearTransmitted = False
			opt.SetWorksharingOptions(wsopt)
			doc.SaveAs(name, opt)
			OUT = 'not done'
		except:
			OUT = 'not done'

i cant find the reason why i get an index error…

Can anyone help me applying the loop to the script?

Thanks already!

Tom B

Working version of the script: (Can’t upload my .py because im a new member sorry!)

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

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

doc = DocumentManager.Instance.CurrentDBDocument
name = IN[0]
compact = IN[1]
newcentral = IN[2]
isworkshared = IN[3]
elmts1 = IN[4]
parmNam1 = IN[5]
val1 = IN[6]

Revit.Elements.Element.SetParameterByName(elmts1[0], parmNam1[0], val1[0])
Revit.Elements.Element.SetParameterByName(elmts1[0], parmNam1[1], val1[1])

TransactionManager.Instance.ForceCloseTransaction()
if doc.IsFamilyDocument:
	name += '.rfa'
else:
	name += '.rvt'
opt = SaveAsOptions()
opt.OverwriteExistingFile = True
opt.Compact = compact
if isworkshared and newcentral:
	wsopt = WorksharingSaveAsOptions()
	wsopt.ClearTransmitted = True
	wsopt.SaveAsCentral = True
	opt.SetWorksharingOptions(wsopt)
try:
	doc.SaveAs(name, opt)
	OUT = 'done'
except:
	try:
		wsopt.ClearTransmitted = False
		opt.SetWorksharingOptions(wsopt)
		doc.SaveAs(name, opt)
		OUT = 'not done'
	except:
		OUT = 'not done'

Dynamo Core: 2.1.0.7500
Dynamo Revit:2.1.0.7733

Hi all,

These are the files where Tom is talking about.

Loop version - Not working.dyn (35.1 KB) Single version - Working.dyn (38.0 KB) Tube with parameters write family.rfa (600 KB)

Hopefully someone can help Tom with this problem.
@jacob.small hope you can point us in the right direction or tag someone who probably could

1 Like

Have you tried just using the single version, setting the python into a custom node, and then letting list lacing and levels manage it?

1 Like

How many family instances get placed by FamilyInstance.ByCoordinates? If it is only one, then you would have a mismatch between the length of elmts and name within your Python script which would be a problem when the loop gets to the second iteration and tries to call elmts[1] which doesn’t exist and throws the index out of range error you can see.

3 Likes

thanks, the problem is solved now.

It had something to do indeed with the elmts[1] changing it to elmts[0] solved the problem.