PDA

View Full Version : reassign root function


Hobbs
10-06-2008, 08:48 PM
Has anyone written a more elegant version of the Reassign Root function that can be found in BoneTools?

To me, and forgive my ignorance, there seems to be a lot of filler in there. I just figured i would ask before i deconstructed it more thoroughly myself.

I just need a very cut and dry reorder the bone hierarchy. There is no IK chain etc that needs to be factored in.

Hobbs
10-06-2008, 11:54 PM
here's a quick,dirty, and commented version. Its a stripped down version of what is in max. I've only tested this on freshly created skeletons, with no stretching or IK's applied to it..

I'm sure their are bugs, so let me know

fn ReassignRoot currentBone =
(
undo "Reassign Root" on
(
with redraw off
(
with animate off
(
-- Local declarations
local parentBone = currentBone.parent -- holds the current parent bone

local deleteBoneArr = #(currentBone) -- will hold all the bones in the scene, begin by holding the one brought in
local chlBone = #() -- stores the children bones, will always be an array

local prevBone = undefined

-- Related Functiosn
--\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
-- copy properties from one to another
--=================================================
fn createBone_From currentBone prevBone =
(
-- get bone endpoint
endPoint = [currentBone.length,0,0] * currentBone.objectTransform
-- create new bone
newBoneA = boneSys.createBone endPoint currentBone.transform.translation currentBone.dir

-- assign properties from currentBone
newBoneA.name = currentBone.name
newBoneA.wirecolor = currentBone.wirecolor
newBoneA.width = currentBone.width
newBoneA.height = currentBone.height
-- assign the parent
newBoneA.parent = prevBone

return newBoneA
);
-- Begin Code
--\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
-- Find all the children
case currentBone.children.count of
(
0:( currentBone = parentBone
parentBone = currentBone.parent
append deleteBoneArr currentBone
)
default:chlBone = currentBone.children
);

-- If there is a parent bone then proceed
if (parentBone != undefined) then
(
do( -- bone creation loop

-- copy old bone's parameters to the new bone
newBoneA = createBone_From currentBone prevBone

-- if bone has children, and parent isn't undefined then
if (parentBone.children.count > 1) and (parentBone.parent != undefined) then
parentBone.children.parent = newBoneA

-- if this new bone has no children, then delete it.
if newBoneA.children == 0 then delete newBoneA

-- if there are children, then relink all their parents.
if chlBone != undefined then chlBone.parent = newBoneA

-- setup for the next pass
prevBone = newBoneA
currentBone = parentBone
parentBone = currentBone.parent

-- add the old bone to delete array
append deleteBoneArr currentBone
)
while (parentBone != undefined) -- bone creation loop

-- If the new bone has more then one child
if currentBone.children.count > 1 then
(
-- copy old bone's parameters to the new bone
newBoneA = createBone_From currentBone prevBone

-- link children bones to newBoneA
currentBone.parent = newBoneA

newBoneA.realignBoneToChild()

)
-- if the new bone has only one or less child, this code seems really weird
else
(
-- copy old bone's parameters to the new bone
parentBone = createBone_From currentBone prevBone
-- create another bone
newBoneA = BoneSys.createBone parentBone.transform.translation (parentBone.transform.translation+6) parentBone.dir

newBoneA.width = parentBone.width
newBoneA.height = parentBone.height
newBoneA.rotation = parentBone.rotation
newBoneA.pos = parentBone.transform.translation

in coordSys Local move newBoneA [parentBone.length,0,0]
newBoneA.parent=parentBone
newBoneA.taper=90
newBoneA.length=(parentBone.width+parentBone.height)/2
newBoneA.wirecolor=parentBone.wirecolor
)
-- delete all old bones, if not deleted
for b in deleteBoneArr do
if not isDeleted b do delete b


) -- if (parentBone != undefined) then
)
)
)
)

Hobbs
10-07-2008, 12:13 AM
forgot to change one line

in

if currentBone.children.count > 1 then
(

-- copy old bone's parameters to the new bone

newBoneA = createBone_From currentBone prevBone

-- link children bones to newBoneA

currentBone.parent = newBoneA

)

should be currentBone.children.parent = newBoneA

CGTalk Moderation
10-07-2008, 12:13 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.