[Closed] Loop through objects and group
I am trying to loop though all of the objects in my scene and group them in blocks of 300. I have the script for looping though object but need to know the syntax for grouping. I did a search here and in Maxscript help and came up empty.
(
Objs = for o in objects collect o
for o in Objs do
(
--add o.name to array
i=i+1
--add o.name + "_bone" to array
i=i+1
--when i==300
--select everything in array
--group it
)
)
Thanks in advance!
Not sure what you are trying to achieve, but parts of this don’t make any sense to me. First: why the i=i+1 twice and why would you collect names of objects in an array?
Try this
theArray = objects as array
theGroup =#()
for i = 1 to 300 do
(
oldname = theArray[i].name
theArray[i].name = oldname + "_bone"
append theGroup theArray[i]
)
--convert theGroup array into group
Not tested, no idea thugh how to group the objects though.
(
local theObjects = objects as array --get all objects
local groupsSoFar = 0 --init. a counter for groups so far
local groupCount = 300 --this is the number of objects per group
while groupsSoFar*groupCount <= theObjects.count do --repeat until all objects are in
(
--collect objects for the current group and group them
group (for i = 1 to groupCount while (theIndex = i+groupsSoFar*groupCount) <= theObjects.count collect theObjects[theIndex])
groupsSoFar +=1 --increment the groups counter
)
)
(
max_count = 300
node_group = #()
for obj in (objects as array) where not isGroupMember obj and not isGroupHead obj and
(append node_group obj).count == max_count do
(
group node_group
node_group = #()
)
if node_group.count > 0 do group node_group
)
Bobo and Denis, both of your scripts work very well for grouping 300 objs thank you. My situation is a little bit tricky as I have one object say called “Box01” and Box01 is skinned to “Box01_bone”. What I need to make sure of is that Box01 and Box01_bone get put into the same group as they must both be exported in the same package to the unreal engine.
So here are the steps,
loop through objects
get first objects name add to group
get first objects name + “_bone” add to group
do this till the group is >= 300 (if the last name entered into the group doesn’t have a “_bone” then do one more.)
start next group
crazy right!
I will play with the scripts you have provided and see if I can hack it together. Thanks dudes!
I have combined the first two and got this which is creating a group properly but only one, I think this is b/c this “theArray.count” is 300 and when it reaches that it thinks its done. I keep trying.
theArray = objects as array
theGroup =#()
(
local groupsSoFar = 0 --init. a counter for groups so far
local groupCount = 300 --this is the number of objects per group
while groupsSoFar*groupCount <= theArray.count do --repeat until all objects are in
(
for i = 1 to 300 do
(
oldname = theArray[i].name
theArray[i].name = oldname + "_bone"
append theGroup theArray[i]
)
group (theGroup)
groupsSoFar +=1 --increment the groups counter
)
)
(
max_count = 300
node_groups = #()
fn addToAvailableGroup &list nodes maxcount:300 = -- add to group with number of members less then max_count
(
local done = off
for k=1 to list.count while not done where done = ((list[k].count + nodes.count) <= maxcount) do list[k] = join list[k] nodes
if not done do append list nodes
)
-- group skin nodes (mesh + bones) by skin modifier
skin_modifiers = getclassinstances Skin -- all skin modifiers
for sk in skin_modifiers do -- collect skin meshes with their skin bones
(
skinned_nodes = refs.dependentnodes sk
skin_bones = for n in (refs.dependson sk) where isvalidnode n collect n
skin_nodes = makeUniqueArray (skinned_nodes + skin_bones)
addToAvailableGroup &node_groups skin_nodes maxcount:max_count
)
-- add free node to any not fully-packed group
for obj in (objects as array) where not isGroupMember obj and not isGroupHead obj do
addToAvailableGroup &node_groups #(obj) maxcount:max_count
-- now group them
for gr in node_groups do group gr
)