Panda3D
 All Classes Functions Variables Enumerations
mayaSavePview.cxx
00001 // Filename: mayaSavePview.cxx
00002 // Created by:  drose (27Oct03)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "mayaSavePview.h"
00016 
00017 #include <maya/MString.h>
00018 #include <maya/MFnPlugin.h>
00019 #include <maya/MFileIO.h>
00020 #include <maya/MArgParser.h>
00021 #include <maya/MArgList.h>
00022 #include <maya/MSyntax.h>
00023 
00024 #include <stdlib.h>
00025 
00026 #ifdef WIN32_VC
00027 #include <process.h>
00028 #endif
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: MayaSavePview::Constructor
00032 //       Access: Public
00033 //  Description:
00034 ////////////////////////////////////////////////////////////////////
00035 MayaSavePview::
00036 MayaSavePview() {
00037 }
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //     Function: MayaSavePview::doIt
00041 //       Access: Public, Virtual
00042 //  Description: Called when the plugin command is invoked.
00043 ////////////////////////////////////////////////////////////////////
00044 MStatus MayaSavePview::
00045 doIt(const MArgList &args) {
00046   MStatus result;
00047 
00048   // First, parse the plugin arguments.
00049   MSyntax syntax;
00050   syntax.addFlag("a", "animate");
00051 
00052   MArgParser parser(syntax, args, &result);
00053   if (!result) {
00054     result.perror("arguments");
00055     return result;
00056   }
00057 
00058   bool animate = parser.isFlagSet("a", &result);
00059   if (!result) {
00060     result.perror("isFlagSet");
00061     return result;
00062   }
00063   
00064   // Now make sure the current buffer is saved.
00065   result = MFileIO::save(false);
00066   if (result != MS::kSuccess) {
00067     return result;
00068   }
00069 
00070   MString filename = MFileIO::currentFile();
00071 
00072   MString pview_args = "-cl";
00073   if (animate) {
00074     pview_args = "-cla";
00075   }
00076 
00077 #ifdef WIN32_VC
00078   // On Windows, we use the spawn function to run pview
00079   // asynchronously.
00080   MString quoted = MString("\"") + filename + MString("\"");
00081   int retval = _spawnlp(_P_DETACH, "pview", 
00082                         "pview", pview_args.asChar(), quoted.asChar(), NULL);
00083   if (retval == -1) {
00084     return MS::kFailure;
00085   }
00086 
00087 #else  // WIN32_VC
00088   // On non-Windows (e.g. Unix), we just use the system function,
00089   // which runs synchronously.  We could fork a process, but no one's
00090   // asked for this yet.
00091   MString command = MString("pview " + pview_args + MString(" \"") + filename + MString("\""));
00092 
00093   int command_result = system(command.asChar());
00094   if (command_result != 0) {
00095     return MS::kFailure;
00096   }
00097 #endif // WIN32_VC
00098 
00099   return MS::kSuccess;
00100 }
00101 
00102 ////////////////////////////////////////////////////////////////////
00103 //     Function: MayaSavePview::creator
00104 //       Access: Public, Static
00105 //  Description: This is used to create a new instance of the plugin.
00106 ////////////////////////////////////////////////////////////////////
00107 void *MayaSavePview::
00108 creator() {
00109   return new MayaSavePview;
00110 }
00111 
00112 
00113 
00114 ////////////////////////////////////////////////////////////////////
00115 //     Function: initializePlugin
00116 //  Description: Called by Maya when the plugin is loaded.
00117 ////////////////////////////////////////////////////////////////////
00118 EXPCL_MISC MStatus 
00119 initializePlugin(MObject obj) {
00120   MFnPlugin plugin(obj, "VR Studio", "1.0");
00121   MStatus status;
00122   status = plugin.registerCommand("pview", MayaSavePview::creator);
00123   if (!status) {
00124     status.perror("registerCommand");
00125   }
00126 
00127   return status;
00128 }
00129 
00130 ////////////////////////////////////////////////////////////////////
00131 //     Function: uninitializePlugin
00132 //  Description: Called by Maya when the plugin is unloaded.
00133 ////////////////////////////////////////////////////////////////////
00134 EXPCL_MISC MStatus
00135 uninitializePlugin(MObject obj) {
00136   MFnPlugin plugin(obj);
00137   MStatus status;
00138   status = plugin.deregisterCommand("pview");
00139 
00140   if (!status) {
00141     status.perror("deregisterCommand");
00142   }
00143   return status;
00144 }
 All Classes Functions Variables Enumerations