Get the Parent Group

You could modify the code in Element.Group to be a recursive function like so:

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

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

elements = UnwrapElement(IN[0])
elementlist = list()

def parentGroup(item):
	if item.Document.GetElement(item.GroupId) == None:
		elementlist.append(item)
	else:
		parentGroup(item.Document.GetElement(item.GroupId))

for elem in elements:
	parentGroup(elem)

OUT = elementlist

This way (verse your other method of collecting groups) would maintain list structure to get the top parent group of each respective element. If you eliminated the portion that removes elements that aren’t in groups, the script will just return the non-grouped element

PS I’m only sharing this as an example of making the script recursive, I do not condone nesting nested groups :sweat_smile:

6 Likes