/* bennett@cc.gatech.edu */ /* http://www.cc.gatech.edu/gvu/perception/projects/motionMatching/cameraTrack/ */ #include #include #include #include #include #include // this is a class that i store the camera information in that I just read in from the file. class Camera { public: int f; // frame double tx, ty, tz, rx, ry, rz, sx, sy, sz; double hfa, vfa; // Horizontal and Vertical Film Aperature int fl; // Focal Length double fstop, fd; // fstop and Focus Distance }; // this is the function that Maya calls when the command is invoked. MStatus cameraPath( const MArgList& args ) { printf("\nRunning cameraPath.\n"); int i; MString camera_name, file_name; // Parse the arguments. for ( i = 0; i < args.length(); i++ ) if ( MString( "-n" ) == args.asString( i ) ) camera_name = args.asString( ++i ); else if ( MString( "-f" ) == args.asString( i ) ) file_name = args.asString( ++i ); // check to see if a file was specified. if (file_name.length() == 0) { printf("File name not specified.\n"); return MS::kFailure; } MStatus status; MFnCamera cameraFn; // create the camera MObject camera = cameraFn.create(&status); if (status == MS::kFailure) printf("Error creating the camera.\n"); // Create various objects to access the cameras components. MFnDagNode fnSet( camera, &status); if ( MS::kSuccess != status ) printf("Failure to create function set\n"); MDagPath dp; if (cameraFn.getPath(dp) != MS::kSuccess) printf("Failure creating dagpath\n"); // set the name of the camera if (camera_name.length() > 0) { fnSet.setName(camera_name, &status); if (status == MS::kFailure) printf("Error setting the camera name.\n"); } // The MFnAnimCurves are the objects that "inject" a value into the correct attribute of // the camera for each frame. // Create the MFnAnimCurves MString attrName( "translateX"); const MObject attrtx = fnSet.attribute (attrName, &status); if ( MS::kSuccess != status ) printf( "Failure to find attribute translateX\n"); MFnAnimCurve acFnSettx; acFnSettx.create ( dp.transform(), attrtx, NULL, &status); if ( MS::kSuccess != status ) printf("Failure creating MFnAnimCurve function set (translateX)\n"); attrName.set( "translateY"); const MObject attrty = fnSet.attribute (attrName, &status); if ( MS::kSuccess != status ) printf( "Failure to find attribute translateY\n"); MFnAnimCurve acFnSetty; acFnSetty.create ( dp.transform(), attrty, NULL, &status); if ( MS::kSuccess != status ) printf("Failure creating MFnAnimCurve function set (translateX)\n"); attrName.set( "translateZ"); const MObject attrtz = fnSet.attribute (attrName, &status); if ( MS::kSuccess != status ) printf( "Failure to find attribute translateZ\n"); MFnAnimCurve acFnSettz; acFnSettz.create ( dp.transform(), attrtz, NULL, &status); if ( MS::kSuccess != status ) printf("Failure creating MFnAnimCurve function set (translateY)\n"); attrName.set( "rotateX"); const MObject attrrx = fnSet.attribute (attrName, &status); if ( MS::kSuccess != status ) printf( "Failure to find attribute rotateX\n"); MFnAnimCurve acFnSetrx; acFnSetrx.create ( dp.transform(), attrrx, NULL, &status); if ( MS::kSuccess != status ) printf("Failure creating MFnAnimCurve function set (translateZ)\n"); attrName.set( "rotateY"); const MObject attrry = fnSet.attribute (attrName, &status); if ( MS::kSuccess != status ) printf( "Failure to find attribute rotateY\n"); MFnAnimCurve acFnSetry; acFnSetry.create ( dp.transform(), attrry, NULL, &status); if ( MS::kSuccess != status ) printf("Failure creating MFnAnimCurve function set (rotateY)\n"); attrName.set( "rotateZ"); const MObject attrrz = fnSet.attribute (attrName, &status); if ( MS::kSuccess != status ) printf( "Failure to find attribute rotateZ\n"); MFnAnimCurve acFnSetrz; acFnSetrz.create ( dp.transform(), attrrz, NULL, &status); if ( MS::kSuccess != status ) printf("Failure creating MFnAnimCurve function set (rotateZ)\n"); // Create the MFnAnimCurves for camera shape. MFnDagNode fnSetcam( dp, &status); if ( MS::kSuccess != status ) printf("Failure to create camera function set.\n"); attrName.set( "horizontalFilmAperture" ); const MObject attrhfa = fnSetcam.attribute( attrName, &status); if ( MS::kSuccess != status ) printf( "Failure to find attribute hfa\n"); MFnAnimCurve acFnSethfa; acFnSethfa.create ( dp.node(), attrhfa, NULL, &status); if ( MS::kSuccess != status ) printf("Failure creating MFnAnimCurve function set hfa\n"); attrName.set( "verticalFilmAperture" ); const MObject attrvfa = fnSetcam.attribute( attrName, &status); if ( MS::kSuccess != status ) printf( "Failure to find attribute vfa\n"); MFnAnimCurve acFnSetvfa; acFnSetvfa.create ( dp.node(), attrvfa, NULL, &status); if ( MS::kSuccess != status ) printf("Failure creating MFnAnimCurve function set vfa\n"); attrName.set( "focalLength" ); const MObject attrfl = fnSetcam.attribute( attrName, &status); if ( MS::kSuccess != status ) printf( "Failure to find attribute fl\n"); MFnAnimCurve acFnSetfl; acFnSetfl.create ( dp.node(), attrfl, NULL, &status); if ( MS::kSuccess != status ) printf("Failure creating MFnAnimCurve function set focal length\n"); attrName.set( "fStop" ); const MObject attrfs = fnSetcam.attribute( attrName, &status); if ( MS::kSuccess != status ) printf( "Failure to find attribute fstop\n"); MFnAnimCurve acFnSetfs; acFnSetfs.create ( dp.node(), attrfs, NULL, &status); if ( MS::kSuccess != status ) printf("Failure creating MFnAnimCurve function set fstop\n"); attrName.set( "focusDistance" ); const MObject attrfd = fnSetcam.attribute( attrName, &status); if ( MS::kSuccess != status ) printf( "Failure to find attribute fd\n"); MFnAnimCurve acFnSetfd; acFnSetfd.create ( dp.node(), attrfd, NULL, &status); if ( MS::kSuccess != status ) printf("Failure creating MFnAnimCurve function set fd\n"); // open file filebuf fb; if (fb.open(file_name.asChar(),ios::in) == NULL) printf("Error opening camera track file.\n"); istream file(&fb); Camera cdata; // read in data while ( file.peek() != EOF ) { file >> cdata.f; file >> cdata.tx; file >> cdata.ty; file >> cdata.tz; file >> cdata.rx; file >> cdata.ry; file >> cdata.rz; file >> cdata.hfa; file >> cdata.vfa; file >> cdata.fl; file >> cdata.fstop; file >> cdata.fd; // add a key frame for each paramater to the correct MFnAnimCurve acFnSettx.addKeyframe(MTime(cdata.f,MTime::uiUnit()),cdata.tx); acFnSetty.addKeyframe(MTime(cdata.f,MTime::uiUnit()),cdata.ty); acFnSettz.addKeyframe(MTime(cdata.f,MTime::uiUnit()),cdata.tz); acFnSetrx.addKeyframe(MTime(cdata.f,MTime::uiUnit()),cdata.rx); acFnSetry.addKeyframe(MTime(cdata.f,MTime::uiUnit()),cdata.ry); acFnSetrz.addKeyframe(MTime(cdata.f,MTime::uiUnit()),cdata.rz); acFnSethfa.addKeyframe(MTime(cdata.f,MTime::uiUnit()),cdata.hfa); acFnSetvfa.addKeyframe(MTime(cdata.f,MTime::uiUnit()),cdata.vfa); acFnSetfl.addKeyframe(MTime(cdata.f,MTime::uiUnit()),cdata.fl); acFnSetfs.addKeyframe(MTime(cdata.f,MTime::uiUnit()),cdata.fstop); acFnSetfd.addKeyframe(MTime(cdata.f,MTime::uiUnit()),cdata.fd); } return MS::kSuccess; } DeclareSingle( cameraPath );