Very simple question that I did not achieve after many attempts perhaps because playing with sublists.
I just want to clean the nulls of a list of sublists, it works great with OOTB node and package nodes but I do not make it work in python, any idea? the sublist is composed by elements with the green ids
The python node used tried to clean the list but nothing happened:
def ClearList(_list):
out = []
for _list1 in _list:
if _list1 is None:
continue
if isinstance(_list1, list):
_list1 = ClearList(_list1)
if not _list1:
continue
out.append(_list1)
return out
OUT = ClearList(Views)
I want to think it is the reason, my question to you if wanting to reuse this code can I delete just the current document variables and give as input my current script list of Elements?
import sys
import clr
import System
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB 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
ueWrapper = next((m for m in clr.GetClrType(Revit.Elements.ElementWrapper)\
.GetMethods() if m.Name == "Wrap"), None)
if ueWrapper is None:
raise Exception("Error Method", "method 'Revit.Elements.ElementWrapper.Wrap' not found")
#views = FilteredElementCollector(doc).OfClass(View).ToElements()
outlist = []
for i in views:
iswrapable = Revit.Elements.ElementWrapper.Wrap(i, True)
if iswrapable is None :
outlist.append(ueWrapper.Invoke(None, (i, True) ))
else:
outlist.append(i)
OUT = outlist
I got this warning after adding that code: Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed. Traceback (most recent call last): File "<string>", line 140, in <module> TypeError: expected LinePatternElement, got List[Element]
This means that somewhere in your code you’re passing a list, but it expects an LinePatternElement. Output your data type before that issue so you can see if you need to pull something out of a list or initiate a loop.
The output is still the same in this case with the function code:
def ClearList(primList):
result = []
for sublist in primList:
if sublist is "":
continue
elif sublist is None:
continue
if isinstance(sublist, list):
sublist = ClearList(sublist)
if not sublist:
continue
result.append(sublist)
return result
OUT = ClearList(Views)
The issue is that the null is not coming from the python list. It’s coming from the conversion to Dynamo. It looks like you’re gathering all views, likely including some 3D View Templates, in which case Dynamo will fail to convert those views and return nulls instead. So you actually have to filter out 3D View Templates, not nulls, from your list of python views.
So is possible to make it as a list and clean the nulls afterwards? Why the solution of @c.poupin does not work? why the clean OOTB node or any other can clean nulls?
It looks like his solution does work. Like you said, the ootb nodes will clean the nulls in the list. The issue is that your list doesn’t actually have any nulls in python. Hence, removing nulls in python will still output a list with nulls.