// MelBotWars utility script to unify imported 6/24/02 // rigidBodies to use the same default "rigidSolver" // Usage: open arena scene, then import all melBots and source and run this script // // Updated 8/4/02 to automatically connect to gravity // Updated 8/17/02 to save initial state at the start to avoid rigidBodies popping out of place proc melBotRebuildConstraint(string $constraint) { // set rigidSolver as the current solver // setAttr rigidSolver.current 1; // find the parent node for the constraint // string $parent[] = `listRelatives -p -f $constraint`; print $parent; // get standard attributes for constraint node // int $interPen = `getAttr($constraint + ".interpenetrate")`; int $constraintType = `getAttr($constraint + ".constraintType")`; float $position[] = `xform -q -ws -t $constraint`; float $rotation[] = `xform -q -ws -ro $constraint`; // get rigidBody connections to constraint // string $rigidBody1 = `connectionInfo -sfd ($constraint + ".rb1")`; string $rigidBody2 = `connectionInfo -sfd ($constraint + ".rb2")`; // get rid of .message attribute in rigidBody name // string $regularExpr = ".message"; $rigidBody1 = `substitute $regularExpr $rigidBody1 ""`; $rigidBody2 = `substitute $regularExpr $rigidBody2 ""`; // declare variable to store the constraint type // string $type, $cmd; string $tmpConstraintType; $tmpConstraintType = $constraintType; switch ($tmpConstraintType) { case "1": $type = "pin"; print ("Deleting original constraint...\n"); delete $constraint; $cmd = ("constrain -" + $type + " -name " + $constraint + " -i " + $interPen + " " + $rigidBody1 + " " + $rigidBody2 + ";"); print ("Creating new constraint...\n"); print ($cmd + "\n"); string $newConstraint = `eval $cmd`; if (`size $parent` != 0){ print ("Parenting constraint ...\n"); parent -r $newConstraint $parent[0]; } $cmd = ("xform -ws -t " + $position[0] + " " + $position[1] + " " + $position[2] + " " + $newConstraint + ";\n"); print ("Moving constraint ...\n"); print ($cmd + "\n"); eval $cmd; break; case "2": $type = "nail"; print ("Deleting original constraint...\n"); delete $constraint; $cmd = ("constrain -" + $type + " -name " + $constraint + " -i " + $interPen + " " + $rigidBody1 + ";"); print ("Creating new constraint...\n"); print ($cmd + "\n"); string $newConstraint = `eval $cmd`; if (`size $parent` != 0){ print ("Parenting constraint ...\n"); parent -r $newConstraint $parent[0]; } $cmd = ("xform -ws -t " + $position[0] + " " + $position[1] + " " + $position[2] + " " + $newConstraint + ";\n"); print ("Moving constraint...\n"); print ($cmd + "\n"); eval $cmd; break; case "7": $type = "spring"; float $springStiff = `getAttr($constraint + ".springStiffness")`; float $springDamp = `getAttr($constraint + ".springDamping")`; float $springRestLen = `getAttr($constraint + ".springRestLength")`; print ("Deleting original constraint...\n"); delete $constraint; $cmd = ("constrain -" + $type + " -name " + $constraint + " -i " + 1 + " -st " + $springStiff + " -d " + $springDamp + " -rl " + $springRestLen + " " + $rigidBody1 + " " + $rigidBody2 + ";"); // to create a spring between two rigid bodies they both must be active // int $rigidState1, $rigidState2; if ($rigidBody1 != ""){ $rigidState1 = `getAttr ($rigidBody1 + ".active")`; setAttr ($rigidBody1 + ".active") 1; } if ($rigidBody2 != ""){ $rigidState2 = `getAttr ($rigidBody2 + ".active")`; setAttr ($rigidBody2 + ".active") 1; } // now we can create the new constraint // print ("Creating new constraint...\n"); print ($cmd + "\n"); string $newConstraint = `eval $cmd`; // now we reset the rigidBody state // if ($rigidBody1 != "")setAttr ($rigidBody1 + ".active") $rigidState1; if ($rigidBody2 != "")setAttr ($rigidBody2 + ".active") $rigidState2; // set the interpentration for the new spring (bug) // setAttr ($newConstraint + ".interpenetrate") $interPen; if (`size $parent` != 0){ print ("Parenting constraint ...\n"); parent $newConstraint $parent[0]; } break; case "8": $type = "directionalHinge "; print ("Deleting original constraint...\n"); delete $constraint; $cmd = ("constrain -" + $type + " -name " + $constraint + " -i " + $interPen + " " + $rigidBody1 + " " + $rigidBody2 + ";"); print ("Creating new constraint...\n"); print ($cmd + "\n"); string $newConstraint = `eval $cmd`; if (`size $parent` != 0){ print ("Parenting constraint ...\n"); parent -r $newConstraint $parent[0]; } $cmd = ("xform -ws -t " + $position[0] + " " + $position[1] + " " + $position[2] + " " + $newConstraint + ";\n"); print ("Moving constraint...\n"); print ($cmd + "\n"); eval $cmd; $cmd = ("xform -ws -ro " + $rotation[0] + " " + $rotation[1] + " " + $rotation[2] + " " + $newConstraint + ";\n"); print ("Rotation constraint ...\n"); print ($cmd + "\n"); eval $cmd; break; default: print("// Constraint type " + $constraintType + " is ignored //\n"); break; } } proc melBotResolveRigidConstraints(){ for ($node in `ls -type "rigidConstraint"`){ string $tmp[] =`listConnections -type rigidSolver ($node + ".rotate")`; if (`size $tmp` < 1)$tmp = `listConnections -type rigidSolver ($node + ".translate")`; if (`size $tmp` < 1)$tmp[0] = "none"; string $currentSolver = $tmp[0]; if ($currentSolver != "rigidSolver"){ print ("Rebuilding Constraint " + $node + "\n"); melBotRebuildConstraint $node; } } } proc melBotResolveRigidBodies(){ if (!`objExists rigidSolver`)rigidSolver -create "rigidSolver"; int $generalForceSize = `getAttr -size rigidSolver.generalForce`; for ($node in `ls -type "rigidBody"`){ if (`rigidBody -q -solver $node` != "rigidSolver"){ rigidBody -e -solver "rigidSolver" $node; } } } proc melBotConnectToGravity(){ // Check to see if the gravity field exists with the correct name, if not then create it // if (! objExists("gravityField1")) { gravity -pos 0 0 0 -m 32.0 -att 0 -dx 0 -dy -1 -dz 0 -mxd -1 -vsh none -vex 0 -vof 0 0 0 -vsw 360 -tsr 0.5 -n "gravityField1"; } select -r `listTransforms "-type rigidBody"`; select -add gravityField1; AffectSelectedObject; select -cl; } global proc melBotConnectToSolver(){ // Save initial state for all the rigidBodies to avoid any objects popping saveInitialState -all; // Force scene to go to start frame // dynRunup 1; currentTime 1; // connect rigidBodies to "rigidSolver" // melBotResolveRigidBodies; // connect rigidConstraints to "rigidSolver" // melBotResolveRigidConstraints; // delete additional solvers // select `ls -type rigidSolver`; select -tgl "rigidSolver"; delete `ls -sl`; // Force scene to go to start frame // dynRunup 1; currentTime 1; // Connect all rigidBodies to gravity // melBotConnectToGravity; }