Recreate nested groups

I want to create nested groups. In this example i want to create 3 groups in order based on a list.
Starting with group #1#2 → group #3 should contain a familyinstance + group #1 (called “fotöljer”)

But this does not seem to work. Group #3 does not contain group #1 when done. Even though the output from my node is containing the last collection with the correct ID:s.
Do i need to regenerate in between creating groups? Or restart transactions somehow? I Think i am already doing that? :slight_smile:

Any help would be appreciated. I have tried as a test to create a group containing a familyinstane+a group and it works fine if the group already exist. But not if the group is created in the same node.

#Standard boilerplate code
import clr
import sys
#sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import System
from System import Array

from System.Collections.Generic import *
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")

import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

import Autodesk
from Autodesk.Revit.DB import ElementId

#----------------------------------------------

doc = DocumentManager.Instance.CurrentDBDocument

def flatten(l):
    return [item for sublist in l for item in sublist]

def find_group(elem_name):
    group_elem = FilteredElementCollector(doc).OfClass(Group).WhereElementIsNotElementType().ToElements()
    elems_inf = []
    
    for e in group_elem:
        if (e.Name == elem_name):
            elems_inf.append(e)
    return elems_inf[0]

gMembers = UnwrapElement(IN[0])
gNames = UnwrapElement(IN[1])
gTypes = UnwrapElement(IN[2])


myCollection = List[ElementId]()

for index, name in enumerate(gMembers):

    temp = gMembers[index]
    type = gTypes[index]
    
    #doc.Regenerate()
    TransactionManager.Instance.EnsureInTransaction(doc)  
    
    myCollection.Clear()
    
    for i,j in zip(temp,type):
        
        if (j == "System.String"):
            elem = find_group(i)                 
            myCollection.Add(elem.Id)            
        else:
            myCollection.Add(i.Id)                        
            
            
    new_group = doc.Create.NewGroup(myCollection)
    new_group.GroupType.Name = gNames[index]    
       
    
    TransactionManager.Instance.TransactionTaskDone()
    
OUT = myCollection

I solved it. If anyone else is wondering i just had to add the line:

TransactionManager.Instance.ForceCloseTransaction()

To force an update and make the recently created elements available for the next loop in this script.

1 Like