[Closed] Remove Empty Groups
I’m little confused, To remove empty groups this code doesn’t work:
for o in objects where isgroupmember o and not isgrouphead (P = o.parent) do
(
detachNodesFromGroup o
o.parent = P
)
But this one works:
for o in (for o in objects where isgroupmember o and not isgrouphead o.parent collect o) do
(
P = o.parent
detachNodesFromGroup o
o.parent = P
)
What is difference?
First of all we have to define what “empty group” is.
so… what is “empty group” in your meaning?
Sorry, I forget to describe it, empty group means all members of a group are linked to another object and group head exist with no children.
This empty groups doen’t allow to create assembly for all objects.we can check it out with
assemblymgr.canassemble objects
hmm…
try :
delete objects
d = dummy()
setGroupHead d on
d.children.count
assemblymgr.canassemble d
we need a better criteria
The problem is I had to create assembly for the scene which is not created by me. but I guess the problem happen when group is open and you change member’s parent to the object outside of the group:
clearlistener()
(
delete objects
obj = sphere()
parentObj = box pos:[100,0,0]
grpHead = group obj
format "Can Assemble Check 1: %
" (assemblymgr.canassemble objects)
setgroupopen grpHead true
obj.parent = parentObj
format "Can Assemble Check 2: %
" (assemblymgr.canassemble objects)
)
In this case we can’t create assembly and also we can’t close the group. after running the sample code, select all objects, go to the group menu and hit close. You got an error:
Can't find open group head to close
another question?
what if a grouphead is nested with ‘empty’ or not ‘empty’ heads?
the “empty” group head in my view is a grouphead object that doesn’t have any children or all it children are “empty” group heads…
nested means groups are put into another… hierarchy of heads
so following my definition of “empty” we have to find all headgroups which meet this criteria
i can’t test it right now but here is my way to check if “empty”:
fn isEmptyGroupHead node empty:on = isgrouphead node and
(
if not empty do for child in node.children do
(
if not isgroupmember child then empty = off
else isEmptyGroupHead child empty:empty
)
empty
)
if node is “empty” grouphead we can delete it but have to open it before deletion
I’m agree with your concept, we can test it in further examples, but for now take a look at this example:
(
-- Creating the problem:
delete objects
members = for i = 1 to 2 collect sphere pos:[i*50,0,0]
grpHead = group members
setgroupopen grpHead true
parentObj = box()
members.parent = parentObj
-- Solutions that does not work:
setgroupopen grpHead false
setGroupHead grpHead false
ungroup grpHead
explodegroup grpHead
delete grpHead
format "Can Assemble: %
" (assemblymgr.canassemble objects)
)
No hierarchical group, just a simple example.After changing the parent, none of the above solutions works on group head.
a possible solution:
(
...
-- Solutions that does not work:
--setgroupopen grpHead false
--setGroupHead grpHead false
--ungroup grpHead
--explodegroup grpHead
delete grpHead
for n in objects do setgroupmember n off
format "Can Assemble: %
" (assemblymgr.canassemble objects)
)
Yes, this works. But I don’t want to destroy all groups in the scene, So:
for o in objects where isgroupmember o and not isgrouphead o.parent do setgroupmember o false