[Closed] Replace groups with dummies while keeping hierarchy intact
What I am trying to do is to preserve the hierarchy of my imported object, but replace all groups with dummies so that I can modify individual pieces. If there is a single child, just move the child in place of the parent, otherwise give the children the same name as the parent with an incremental number at the end. Some of the children will have the same name, in case that matters.
I have done the above in C# in Unity3D, but am having a hard time writing the code in MaxScript.
Something like:
function ProcessImported parent parentDummy
(
loop through all children of the parent
(
check if the child has children
yes- create a dummy parent and place it at the center of the child then call the function again to handle the children
no- copy the mesh data
check if the parent has only 1 child
yes- set the name of the child to that of the parent and remove the parentDummy
no- set the name of the child to that of the parent with an incremental number at the end then set as a child to the parentDummy
)
)
Any help is greatly appreciated.
Select the root node of your object hierarchy and run this:
fn replaceWithDummy obj =
(
d = dummy()
d.transform = obj.transform
d.pos = obj.center
d.parent = obj.parent
for child in obj.children do
(
child.parent = d
replaceWithDummy child
)
delete obj
)
replaceWithDummy $ --run recursive function on currently selected root node
Keep in mind this is a pretty simple solution that won't work with skin modifiers, special controllers, etc. It will only work if the hierarchy consists of normal objects parented to one another.
This doesn't do the renaming that you wanted (I couldn't understand what you were asking for in that regard), but that would be easy to add to this.
Thank you ivanisavich for your reply.
I have no clue what I’m doing wrong, but I get an error (– Unknown property: “transform” in $selection) when I run it. I selected the the root node of the imported Step file then ran the script. I commented out the transform assignment, then got another error at the parent assignment.
As for the naming, I want to have unique names for all objects. All the Step files I have imported thus far have had a few different names for all the geometry. Because of this, I used the parent name instead of ‘revolve1’. When a parent has 2+ children, just using the parent name is not enough, so I appended an incremental number. Another direction is to rename all helpers and objects with an incremental prefix.
Written out with more detail:
function ProcessStepFile stepParent helperParent
(
foreach(child in stepParent)
(
if(child contains children)
(
If there is geometry attached to this parent, process it differently
Create a new helper (dummy)
Position dummy to the center of the child
Set the parent of the dummy to helperParent
Call function again on child (traverse the hierarchy)
)
else
(
Copy the geometry of the child (or change its parent to the helperParent)
if(child's parent contains more than 1 child)
(
Set the name of the child to that of the parent
Because there is a single child, we don't need the parent, so remove the parent and move the child up a level
)
else
(
Set the name of the child to that of the parent with an incremental number at the end
Set the child's parent to the helperParent
)
)
)
)
Did you have more than 1 object selected before running the script? “$selection” usually (always?) denotes an array of selected objects.
You can ensure an object has a unique name by giving it a name with the following function:
obj.name = uniquename obj.name
Your uniquename is certainly easier than what I implemented today. Thank you!
Made some progress today by looking at many other scripts. Having a bit of a hard time finding how to either 1) change the parent of a grouped object (child containing geometry) to that of a dummy, 2) make a copy of the grouped object (child containing geometry) then set the parent to that of a dummy, or 3) ungroup the current group till there are no more groups for the child to be in, then either change the parent to a dummy or make a copy and assign that to a dummy.
Any guidance as to which approach is better and some of the terms in MaxScript I will need to accomplish it?
Thank you