Notifications
Clear all

[Closed] Why is Script Exploding Group?

If I run this script

fn FindAllChildren obj =
(
 -- Adapted by Gavin Greenwalt from the script by: Kevin Hayes "SelectAllChildren"
 if obj != undefined then
 (
  AllChildren = #()
  function GetChildrenFromSelection ObjectNode =
  (
   
   function SelectAllSubChildren subchildarray childarray=
   (
	local tempchildren = #()
	
	for i = 1 to SubChildArray.count do
	(
	 if (finditem ChildArray SubChildArray[i]) == 0 do
	 (
	  append ChildArray SubChildArray[i]
	 )   
	)
	
	for each in SubChildArray do
	(
	 for i = 1 to each.children.count do
	 (
	  append tempchildren each.children[i]
	 )
	)  
	return tempchildren 
   )
   
   CArray = ObjectNode.children
   SubCArray = CArray
   while SubCArray.count != 0 do
   (
	SubCArray = SelectAllSubChildren SubCArray CArray
   )
   return CArray
  )
  
  childarray = GetChildrenFromSelection obj
  for o in childarray do (append childrenmerges o)
  return AllChildren
 )
 else
 (
  return undefined
 ) 
)

On a grouphead that contains other nested groups… it ungroups everything inside of it. All I want is an array of values not for anything to be changed. Any idea why It’s exploding it? Am I not noticing a line of code like “obj.parent = […]”?

1 Reply

Found the problem. Somehow CArray was inheriting the value of the children themselves instead of just ingesting it into an array. So as CArray was modified and distorted so too was the child list of the input object.

Functional Code:

fn FindAllChildren obj =
(
 -- Adapted by Gavin Greenwalt from the script by: Kevin Hayes "SelectAllChildren"
 if obj != undefined then
 (
  AllChildren = #()
  function GetChildrenFromSelection ObjectNode =
  (
   
   function SelectAllSubChildren subchildarray childarray=
   (
	local tempchildren, ChildArray, SubChildArray
	
	tempchildren = #()
	
	for i = 1 to SubChildArray.count do
	(
	 if (finditem ChildArray SubChildArray[i]) == 0 do
	 (
	  append ChildArray SubChildArray[i]
	 )   
	)
	
	for j = 1 to SubChildArray.count do
	(
	 for i = 1 to SubChildArray[j].children.count do
	 (
	  append tempchildren SubChildArray[j].children[i]
	  -- CHECK LINE OF CODE --
	 )
	)  
	return tempchildren 
   )
   CArray = #()
   for o in ObjectNode.Children do 
   (
	append CArray o
   )
   SubCArray = CArray
   while SubCArray.count != 0 do
   (
	SubCArray = SelectAllSubChildren SubCArray CArray
   )
   return CArray
  )
  
  childarray = GetChildrenFromSelection obj
  for o in childarray do (append allchildren o)
  return AllChildren
 )
 else
 (
  return undefined
 ) 
)