MStatus customAttrCmd::action( int flag ) // // Description // Do the actual work here to move the objects by the // command's delta value. The objects will come from those // on the active selection list. // { MStatus stat; double d = delta; switch( flag ) { case UNDOIT: // undo d = -d; break; case REDOIT: // redo break; case DOIT: // do command break; default: break; } // Create a selection list iterator // MSelectionList slist; MGlobal::getActiveSelectionList( slist ); MItSelectionList iter( slist, MFn::kInvalid, &stat ); if ( MS::kSuccess == stat ) { MDagPath mdagPath; // Item dag path MObject mComponent; // Current component // Processs all selected objects // for ( ; !iter.isDone(); iter.next() ) { // Get path and possibly a component // iter.getDagPath( mdagPath, mComponent ); MFnTransform transFn( mdagPath, &stat ); if ( MS::kSuccess == stat ) { // If the selected object is of type rockingTransform, // then set the appropriate plug value depending on which axes // the command is operating on. if (transFn.typeId() == rockingTransformNode::id) { MPlug plg = transFn.findPlug(customAttributeString); double val; plg.getValue(val); plg.setValue(val+d); } continue; } } // for } else { cerr << "Error creating selection list iterator" << endl; } return MS::kSuccess; }
MStatus moveCmd::action( int flag ) // // Description // Do the actual work here to move the objects by vector // { MStatus stat; MVector vector = delta; switch( flag ) { case UNDOIT: // undo vector.x = -vector.x; vector.y = -vector.y; vector.z = -vector.z; break; case REDOIT: // redo break; case DOIT: // do command break; default: break; } // Create a selection list iterator // MSelectionList slist; MGlobal::getActiveSelectionList( slist ); MItSelectionList iter( slist, MFn::kInvalid, &stat ); if ( MS::kSuccess == stat ) { MDagPath mdagPath; // Item dag path MObject mComponent; // Current component MSpace::Space spc = MSpace::kWorld; // Translate all selected objects // for ( ; !iter.isDone(); iter.next() ) { // Get path and possibly a component // iter.getDagPath( mdagPath, mComponent ); MFnTransform transFn( mdagPath, &stat ); if ( MS::kSuccess == stat ) { stat = transFn.translateBy( vector, spc ); CHECKRESULT(stat,"Error doing translate on transform"); continue; } MItCurveCV cvFn( mdagPath, mComponent, &stat ); if ( MS::kSuccess == stat ) { for ( ; !cvFn.isDone(); cvFn.next() ) { stat = cvFn.translateBy( vector, spc ); CHECKRESULT(stat,"Error setting CV"); } cvFn.updateCurve(); } MItSurfaceCV sCvFn( mdagPath, mComponent, true, &stat ); if ( MS::kSuccess == stat ) { for ( ; !sCvFn.isDone(); sCvFn.nextRow() ) { for ( ; !sCvFn.isRowDone(); sCvFn.next() ) { stat = sCvFn.translateBy( vector, spc ); CHECKRESULT(stat,"Error setting CV"); } } sCvFn.updateSurface(); } MItMeshVertex vtxFn( mdagPath, mComponent, &stat ); if ( MS::kSuccess == stat ) { for ( ; !vtxFn.isDone(); vtxFn.next() ) { stat = vtxFn.translateBy( vector, spc ); CHECKRESULT(stat,"Error setting Vertex"); } vtxFn.updateSurface(); } } // for } else { cerr << "Error creating selection list iterator" << endl; } return MS::kSuccess; }
void liqBoundingBoxLocator::draw( M3dView & view, const MDagPath & path, M3dView::DisplayStyle style, M3dView::DisplayStatus status ) { MFnDagNode dagFn( thisMObject() ); MFnDagNode transFn( dagFn.parent( 0 ) ); MStatus stat; bool drawBox( 0 ); MPlug plug = dagFn.findPlug( "drawBox", stat ); if ( stat == MS::kSuccess ) plug.getValue( drawBox ); if ( !drawBox ) return; MDoubleArray bb( 6 ); MGlobal::executeCommand( MString( "exactWorldBoundingBox " ) + transFn.fullPathName(), bb ); pts = shared_array< MPoint >( new MPoint[ 8 ] ); pts[0].x = bb[0]; pts[0].y = bb[4]; pts[0].z = bb[2]; pts[1].x = bb[0]; pts[1].y = bb[4]; pts[1].z = bb[5]; pts[2].x = bb[3]; pts[2].y = bb[4]; pts[2].z = bb[5]; pts[3].x = bb[3]; pts[3].y = bb[4]; pts[3].z = bb[2]; pts[4].x = bb[0]; pts[4].y = bb[1]; pts[4].z = bb[2]; pts[5].x = bb[0]; pts[5].y = bb[1]; pts[5].z = bb[5]; pts[6].x = bb[3]; pts[6].y = bb[1]; pts[6].z = bb[5]; pts[7].x = bb[3]; pts[7].y = bb[1]; pts[7].z = bb[2]; MMatrix m( path.inclusiveMatrix() ); for( unsigned i( 0 ); i < 8; i++ ) pts[i] *= m.inverse(); // draw box view.beginGL(); view.setDrawColor( MColor( .57f, 0, .57f ) ); glPushAttrib( GL_CURRENT_BIT ); glBegin(GL_LINE_STRIP); glVertex3f( (float)pts[0].x, (float)pts[0].y, (float)pts[0].z ); glVertex3f( (float)pts[1].x, (float)pts[1].y, (float)pts[1].z ); glVertex3f( (float)pts[2].x, (float)pts[2].y, (float)pts[2].z ); glVertex3f( (float)pts[3].x, (float)pts[3].y, (float)pts[3].z ); glVertex3f( (float)pts[0].x, (float)pts[0].y, (float)pts[0].z ); glEnd(); glBegin(GL_LINE_STRIP); glVertex3f( (float)pts[4].x, (float)pts[4].y, (float)pts[4].z ); glVertex3f( (float)pts[5].x, (float)pts[5].y, (float)pts[5].z ); glVertex3f( (float)pts[6].x, (float)pts[6].y, (float)pts[6].z ); glVertex3f( (float)pts[7].x, (float)pts[7].y, (float)pts[7].z ); glVertex3f( (float)pts[4].x, (float)pts[4].y, (float)pts[4].z ); glEnd(); glBegin(GL_LINE_STRIP); glVertex3f( (float)pts[0].x, (float)pts[0].y, (float)pts[0].z ); glVertex3f( (float)pts[4].x, (float)pts[4].y, (float)pts[4].z ); glEnd(); glBegin(GL_LINE_STRIP); glVertex3f( (float)pts[1].x, (float)pts[1].y, (float)pts[1].z ); glVertex3f( (float)pts[5].x, (float)pts[5].y, (float)pts[5].z ); glEnd(); glBegin(GL_LINE_STRIP); glVertex3f( (float)pts[2].x, (float)pts[2].y, (float)pts[2].z ); glVertex3f( (float)pts[6].x, (float)pts[6].y, (float)pts[6].z ); glEnd(); glBegin(GL_LINE_STRIP); glVertex3f( (float)pts[3].x, (float)pts[3].y, (float)pts[3].z ); glVertex3f( (float)pts[7].x, (float)pts[7].y, (float)pts[7].z ); glEnd(); view.endGL(); }
void NifAnimationImporter::ImportControllers( NiAVObjectRef niAVObj, MDagPath & path ) { list<NiTimeControllerRef> controllers = niAVObj->GetControllers(); //Iterate over the controllers, reacting properly to each type for ( list<NiTimeControllerRef>::iterator it = controllers.begin(); it != controllers.end(); ++it ) { if ( (*it)->IsDerivedType( NiKeyframeController::TYPE ) ) { //--NiKeyframeController--// NiKeyframeControllerRef niKeyCont = DynamicCast<NiKeyframeController>(*it); NiKeyframeDataRef niKeyData = niKeyCont->GetData(); MFnTransform transFn( path.node() ); MFnAnimCurve traXFn; MFnAnimCurve traYFn; MFnAnimCurve traZFn; MObject traXcurve = traXFn.create( transFn.findPlug("translateX") ); MObject traYcurve = traYFn.create( transFn.findPlug("translateY") ); MObject traZcurve = traZFn.create( transFn.findPlug("translateZ") ); MTimeArray traTimes; MDoubleArray traXValues; MDoubleArray traYValues; MDoubleArray traZValues; vector<Key<Vector3> > tra_keys = niKeyData->GetTranslateKeys(); for ( size_t i = 0; i < tra_keys.size(); ++i) { traTimes.append( MTime( tra_keys[i].time, MTime::kSeconds ) ); traXValues.append( tra_keys[i].data.x ); traYValues.append( tra_keys[i].data.y ); traZValues.append( tra_keys[i].data.z ); //traXFn.addKeyframe( tra_keys[i].time * 24.0, tra_keys[i].data.x ); //traYFn.addKeyframe( tra_keys[i].time * 24.0, tra_keys[i].data.y ); //traZFn.addKeyframe( tra_keys[i].time * 24.0, tra_keys[i].data.z ); } traXFn.addKeys( &traTimes, &traXValues ); traYFn.addKeys( &traTimes, &traYValues ); traZFn.addKeys( &traTimes, &traZValues ); KeyType kt = niKeyData->GetRotateType(); if ( kt != XYZ_ROTATION_KEY ) { vector<Key<Quaternion> > rot_keys = niKeyData->GetQuatRotateKeys(); MFnAnimCurve rotXFn; MFnAnimCurve rotYFn; MFnAnimCurve rotZFn; MObject rotXcurve = rotXFn.create( transFn.findPlug("rotateX") ); //rotXFn.findPlug("rotationInterpolation").setValue(3); MObject rotYcurve = rotYFn.create( transFn.findPlug("rotateY") ); //rotYFn.findPlug("rotationInterpolation").setValue(3); MObject rotZcurve = rotZFn.create( transFn.findPlug("rotateZ") ); //rotZFn.findPlug("rotationInterpolation").setValue(3); MTimeArray rotTimes; MDoubleArray rotXValues; MDoubleArray rotYValues; MDoubleArray rotZValues; MEulerRotation mPrevRot; for( size_t i = 0; i < rot_keys.size(); ++i ) { Quaternion niQuat = rot_keys[i].data; MQuaternion mQuat( niQuat.x, niQuat.y, niQuat.z, niQuat.w ); MEulerRotation mRot = mQuat.asEulerRotation(); MEulerRotation mAlt; mAlt[0] = PI + mRot[0]; mAlt[1] = PI - mRot[1]; mAlt[2] = PI + mRot[2]; for ( size_t j = 0; j < 3; ++j ) { double prev_diff = abs(mRot[j] - mPrevRot[j]); //Try adding and subtracting multiples of 2 pi radians to get //closer to the previous angle while (true) { double new_angle = mRot[j] - (PI * 2); double diff = abs( new_angle - mPrevRot[j] ); if ( diff < prev_diff ) { mRot[j] = new_angle; prev_diff = diff; } else { break; } } while (true) { double new_angle = mRot[j] + (PI * 2); double diff = abs( new_angle - mPrevRot[j] ); if ( diff < prev_diff ) { mRot[j] = new_angle; prev_diff = diff; } else { break; } } } for ( size_t j = 0; j < 3; ++j ) { double prev_diff = abs(mAlt[j] - mPrevRot[j]); //Try adding and subtracting multiples of 2 pi radians to get //closer to the previous angle while (true) { double new_angle = mAlt[j] - (PI * 2); double diff = abs( new_angle - mPrevRot[j] ); if ( diff < prev_diff ) { mAlt[j] = new_angle; prev_diff = diff; } else { break; } } while (true) { double new_angle = mAlt[j] + (PI * 2); double diff = abs( new_angle - mPrevRot[j] ); if ( diff < prev_diff ) { mAlt[j] = new_angle; prev_diff = diff; } else { break; } } } //Try taking advantage of the fact that: //Rotate(x,y,z) = Rotate(pi + x, pi - y, pi +z) double rot_diff = ( (abs(mRot[0] - mPrevRot[0]) + abs(mRot[1] - mPrevRot[1]) + abs(mRot[2] - mPrevRot[2]) ) / 3.0 ); double alt_diff = ( (abs(mAlt[0] - mPrevRot[0]) + abs(mAlt[1] - mPrevRot[1]) + abs(mAlt[2] - mPrevRot[2]) ) / 3.0 ); if ( alt_diff < rot_diff ) { mRot = mAlt; } mPrevRot = mRot; rotTimes.append( MTime(rot_keys[i].time, MTime::kSeconds) ); rotXValues.append( mRot[0] ); rotYValues.append( mRot[1] ); rotZValues.append( mRot[2] ); } rotXFn.addKeys( &rotTimes, &rotXValues ); rotYFn.addKeys( &rotTimes, &rotYValues ); rotZFn.addKeys( &rotTimes, &rotZValues ); } } } }
MStatus NifDefaultImportingFixture::ReadNodes( const MFileObject& file ) { try { //out << "Reading NIF File..." << endl; //Read NIF file NiObjectRef root = ReadNifTree( file.fullName().asChar() ); //out << "Importing Nodes..." << endl; //Import Nodes, starting at each child of the root NiNodeRef root_node = DynamicCast<NiNode>(root); if ( root_node != NULL ) { //Root is a NiNode and may have children this->translatorData->importedSceneRoot = root_node; //Check if the user wants us to try to find the bind pose if ( this->translatorOptions->importBindPose ) { SendNifTreeToBindPos( root_node ); } //Check if the user wants us to try to combine new skins with //an existing skeleton if ( this->translatorOptions->importCombineSkeletons ) { //Enumerate existing nodes by name this->translatorData->existingNodes.clear(); MItDag dagIt( MItDag::kDepthFirst); for ( ; !dagIt.isDone(); dagIt.next() ) { MFnTransform transFn( dagIt.item() ); //out << "Adding " << transFn.name().asChar() << " to list of existing nodes" << endl; MDagPath nodePath; dagIt.getPath( nodePath ); this->translatorData->existingNodes[ transFn.name().asChar() ] = nodePath; } //Adjust NiNodes in the original file that match names //in the maya scene to have the same transforms before //importing the new mesh over the top of the old one NiAVObjectRef rootAV = DynamicCast<NiAVObject>(root); if ( rootAV != NULL ) { this->translatorUtils->AdjustSkeleton( rootAV ); } } //Check if the root node has a non-identity transform if ( root_node->GetLocalTransform() == Matrix44::IDENTITY ) { //Root has no transform, so treat it as the scene root vector<NiAVObjectRef> root_children = root_node->GetChildren(); bool reserved = MProgressWindow::reserve(); if(reserved == true) { MProgressWindow::setProgressMin(0); MProgressWindow::setProgressMax(root_children.size() - 1); MProgressWindow::setTitle("Importing nodes"); MProgressWindow::startProgress(); MProgressWindow::setInterruptable(false); for ( unsigned int i = 0; i < root_children.size(); ++i ) { this->nodeImporter->ImportNodes( root_children[i], this->translatorData->importedNodes ); MProgressWindow::advanceProgress(1); } MProgressWindow::endProgress(); } else { for ( unsigned int i = 0; i < root_children.size(); ++i ) { this->nodeImporter->ImportNodes( root_children[i], this->translatorData->importedNodes ); } } } else { //Root has a transform, so it's probably part of the scene this->nodeImporter->ImportNodes( StaticCast<NiAVObject>(root_node), this->translatorData->importedNodes ); } } else { NiAVObjectRef rootAVObj = DynamicCast<NiAVObject>(root); if ( rootAVObj != NULL ) { //Root is importable, but has no children this->nodeImporter->ImportNodes( rootAVObj, this->translatorData->importedNodes ); } else { //Root cannot be imported MGlobal::displayError( "The root of this NIF file is not derived from the NiAVObject class. It cannot be imported." ); return MStatus::kFailure; } } //--Import Materials--// //out << "Importing Materials..." << endl; NiAVObjectRef rootAVObj = DynamicCast<NiAVObject>(root); if ( rootAVObj != NULL ) { //Root is importable this->materialImporter->ImportMaterialsAndTextures( rootAVObj ); } //--Import Meshes--// //out << "Importing Meshes..." << endl; //Iterate through all meshes that were imported. //This had to be deffered because all bones must exist //when attaching skin bool reserved; reserved = MProgressWindow::reserve(); if(reserved == true) { MProgressWindow::setProgressMin(0); MProgressWindow::setProgressMax(this->translatorData->importedMeshes.size() - 1); MProgressWindow::setTitle("Importing meshes"); MProgressWindow::startProgress(); for ( unsigned i = 0; i < this->translatorData->importedMeshes.size(); ++i ) { //out << "Importing mesh..." << endl; //Import Mesh MDagPath meshPath = this->meshImporter->ImportMesh( this->translatorData->importedMeshes[i].first, this->translatorData->importedMeshes[i].second); MProgressWindow::advanceProgress(1); } MProgressWindow::endProgress(); } else { for ( unsigned i = 0; i < this->translatorData->importedMeshes.size(); ++i ) { //out << "Importing mesh..." << endl; //Import Mesh MDagPath meshPath = this->meshImporter->ImportMesh( this->translatorData->importedMeshes[i].first, this->translatorData->importedMeshes[i].second); } } //out << "Done importing meshes." << endl; //--Import Animation--// //out << "Importing Animation keyframes..." << endl; //Iterate through all imported nodes, looking for any with animation keys //for ( map<NiAVObjectRef,MDagPath>::iterator it = this->translatorData->importedNodes.begin(); it != this->translatorData->importedNodes.end(); ++it ) { // //Check to see if this node has any animation controllers // if ( it->first->IsAnimated() ) { // this->animationImporter->ImportControllers( it->first, it->second ); // } //} //out << "Deselecting anything that was selected by MEL commands" << endl; MGlobal::clearSelectionList(); //Clear temporary data //out << "Clearing temporary data" << endl; this->translatorData->Reset(); } catch( exception & e ) { MGlobal::displayError( e.what() ); return MStatus::kFailure; } catch( ... ) { MGlobal::displayError( "Error: Unknown Exception." ); return MStatus::kFailure; } return MStatus::kSuccess; }
MStatus richMoveCmd::action( int flag ) // // Description // Do the actual work here to move the objects by vector // { MStatus stat; MVector vector = delta; switch( flag ) { case UNDOIT: // undo vector.x = -vector.x; vector.y = -vector.y; vector.z = -vector.z; break; case REDOIT: // redo break; case DOIT: // do command break; default: break; } // Create a selection list iterator // MSelectionList slist; MSpace::Space spc = MSpace::kWorld; MRichSelection rs; MGlobal::getRichSelection( rs); // Translate all selected objects // rs.getSelection( slist); if( !slist.isEmpty()) { for ( MItSelectionList iter( slist, MFn::kInvalid); !iter.isDone(); iter.next() ) { // Get path and possibly a component // MDagPath mdagPath; // Item dag path MObject mComponent; // Current component iter.getDagPath( mdagPath, mComponent ); MPlane seam; rs.getSymmetryPlane( mdagPath, spc, seam); if( mComponent.isNull()) { // Transform move MFnTransform transFn( mdagPath, &stat ); if ( MS::kSuccess == stat ) { stat = transFn.translateBy( vector, spc ); CHECKRESULT(stat,"Error doing translate on transform"); continue; } } else { // Component move iter.getDagPath( mdagPath, mComponent ); for( MItGeometry geoIter( mdagPath, mComponent); !geoIter.isDone(); geoIter.next()) { MVector origPosition = geoIter.position( spc); MWeight weight = geoIter.weight(); // Calculate the soft move MVector position = origPosition + vector * weight.influence(); // Calculate the soft seam position += seam.normal() * (weight.seam() * (seam.directedDistance( origPosition) - seam.directedDistance( position))); geoIter.setPosition( position, spc); } } } } // Translate all symmetry objects // slist.clear(); rs.getSymmetry( slist); if( !slist.isEmpty()) { for ( MItSelectionList iter( slist, MFn::kInvalid); !iter.isDone(); iter.next() ) { // Get path and possibly a component // MDagPath mdagPath; // Item dag path MObject mComponent; // Current component iter.getDagPath( mdagPath, mComponent ); MPlane seam; rs.getSymmetryPlane( mdagPath, spc, seam); // Reflect our world space move // MMatrix symmetryMatrix; rs.getSymmetryMatrix( mdagPath, spc, symmetryMatrix); MVector symmetryVector = vector * symmetryMatrix; if( mComponent.isNull()) { // Transform move MFnTransform transFn( mdagPath, &stat ); if ( MS::kSuccess == stat ) { stat = transFn.translateBy( symmetryVector, spc ); CHECKRESULT(stat,"Error doing translate on transform"); continue; } } else { // Component move iter.getDagPath( mdagPath, mComponent ); for( MItGeometry geoIter( mdagPath, mComponent); !geoIter.isDone(); geoIter.next()) { MVector origPosition = geoIter.position( spc); MWeight weight = geoIter.weight(); // Calculate the soft move MVector position = origPosition + symmetryVector * weight.influence(); // Calculate the soft seam position += seam.normal() * (weight.seam() * (seam.directedDistance( origPosition) - seam.directedDistance( position))); geoIter.setPosition( position, spc); } } } } return MS::kSuccess; }