Get Index error - python

hi, All

I get a error “indexerror: index out of range” in these python script.

This is my python code. :point_down: :point_down: :point_down:

import clr
import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

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

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

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

paramName = IN[0]

if not isinstance(IN[1], list):
	elements = [IN[1]]
else:
	elements = IN[1]

if not isinstance(IN[2], list):
	paramValues = [IN[2]]
else:
	paramValues = IN[2]

def GetBuiltInParam(paramName):
	builtInParams = System.Enum.GetValues(BuiltInParameter)
	test = []
	for i in builtInParams:
		if i.ToString() == paramName:
			test.append(i)
			break
		else:
			continue
	return test[0]
try:
	errorReport = None
	TransactionManager.Instance.EnsureInTransaction(doc)
	
	bipName = GetBuiltInParam(paramName)
	for i, j in zip(elements, paramValues):
		param = UnwrapElement(i).get_Parameter(bipName)
		if param.StorageType == StorageType.ElementId:
			id = ElementId(j)
			param.Set(id)
		else:
			param.Set(j)
	
	TransactionManager.Instance.TransactionTaskDone()
except:
	import traceback
	errorReport = traceback.format_exc()

if errorReport == None:
	OUT = IN[1]
else:
	OUT = errorReport

hi @robert12546358

can you provide the revit and dynamo file?

-biboy

Not required revit and dynamo file i give you a small graph.

python code in node Set Builtin Parameter.

I’d guess your custom function isn’t finding any builtin parameters with a matching name. Could have something to do with your list structure. It looks like you’re only looping through the main list.

First thing first, there are lots of redundant nested list in your code, secondly, i dont see a reason why there is a need to chop your list before the input. But to simply solve your error, its because your custom def “test” variable is an empty list, thats why you can call test[0].

i also do not see a point in doing try except in this approach. i would rather make the node turn yellow and tell the user that there is an actual error going on within the script.

Your code will fail as well because the data type you are feeding in does not work. if you are looking to set parameter value by name, why not use the OOTB node??

1 Like