Script to delete ALL items but 1 with groups

3.16K viewsScripting
0

Hello all,
Need help with scripting to delete all items in a category except 1. I have a script to delete all without any groupings but i can’t find anything that helps with grouped items.

I’ve attached a model that has Matrix1 with groupings and Matrix2 with no groupings. The scripts Action 1 and 2 below the two matrices.

Any help would be greatly appreciated.

0

Ben,
That did exactly what I needed. Thank you very much!

Andy

0

Hi,

One approach is to ungroup the item to be spared, then delete all remaining children in the category (groups or items). I preserved the first item because its index is constant, which simplifies the code. However, you could also adjust to preserve the last item (as your original example did) if desired.

[code:jgcpwjfl]
def firstItem = |Matrix1::Cat1|.items[0]

while (firstItem.parent instanceof Group)
((Group) firstItem.parent).ungroup()

(1..<(|Matrix1::Cat1|.children.size)).each
{
|Matrix1::Cat1|.children[1].delete()
}
[/code:jgcpwjfl]

Regards,
Ben