Panda3D
 All Classes Functions Variables Enumerations
mayaSavePview.cxx
1 // Filename: mayaSavePview.cxx
2 // Created by: drose (27Oct03)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "mayaSavePview.h"
16 
17 #include <maya/MString.h>
18 #include <maya/MFnPlugin.h>
19 #include <maya/MFileIO.h>
20 #include <maya/MArgParser.h>
21 #include <maya/MArgList.h>
22 #include <maya/MSyntax.h>
23 
24 #include <stdlib.h>
25 
26 #ifdef WIN32_VC
27 #include <process.h>
28 #endif
29 
30 ////////////////////////////////////////////////////////////////////
31 // Function: MayaSavePview::Constructor
32 // Access: Public
33 // Description:
34 ////////////////////////////////////////////////////////////////////
35 MayaSavePview::
36 MayaSavePview() {
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: MayaSavePview::doIt
41 // Access: Public, Virtual
42 // Description: Called when the plugin command is invoked.
43 ////////////////////////////////////////////////////////////////////
44 MStatus MayaSavePview::
45 doIt(const MArgList &args) {
46  MStatus result;
47 
48  // First, parse the plugin arguments.
49  MSyntax syntax;
50  syntax.addFlag("a", "animate");
51 
52  MArgParser parser(syntax, args, &result);
53  if (!result) {
54  result.perror("arguments");
55  return result;
56  }
57 
58  bool animate = parser.isFlagSet("a", &result);
59  if (!result) {
60  result.perror("isFlagSet");
61  return result;
62  }
63 
64  // Now make sure the current buffer is saved.
65  result = MFileIO::save(false);
66  if (result != MS::kSuccess) {
67  return result;
68  }
69 
70  MString filename = MFileIO::currentFile();
71 
72  MString pview_args = "-cl";
73  if (animate) {
74  pview_args = "-cla";
75  }
76 
77 #ifdef WIN32_VC
78  // On Windows, we use the spawn function to run pview
79  // asynchronously.
80  MString quoted = MString("\"") + filename + MString("\"");
81  int retval = _spawnlp(_P_DETACH, "pview",
82  "pview", pview_args.asChar(), quoted.asChar(), NULL);
83  if (retval == -1) {
84  return MS::kFailure;
85  }
86 
87 #else // WIN32_VC
88  // On non-Windows (e.g. Unix), we just use the system function,
89  // which runs synchronously. We could fork a process, but no one's
90  // asked for this yet.
91  MString command = MString("pview " + pview_args + MString(" \"") + filename + MString("\""));
92 
93  int command_result = system(command.asChar());
94  if (command_result != 0) {
95  return MS::kFailure;
96  }
97 #endif // WIN32_VC
98 
99  return MS::kSuccess;
100 }
101 
102 ////////////////////////////////////////////////////////////////////
103 // Function: MayaSavePview::creator
104 // Access: Public, Static
105 // Description: This is used to create a new instance of the plugin.
106 ////////////////////////////////////////////////////////////////////
107 void *MayaSavePview::
109  return new MayaSavePview;
110 }
111 
112 
113 
114 ////////////////////////////////////////////////////////////////////
115 // Function: initializePlugin
116 // Description: Called by Maya when the plugin is loaded.
117 ////////////////////////////////////////////////////////////////////
118 EXPCL_MISC MStatus
119 initializePlugin(MObject obj) {
120  MFnPlugin plugin(obj, "VR Studio", "1.0");
121  MStatus status;
122  status = plugin.registerCommand("pview", MayaSavePview::creator);
123  if (!status) {
124  status.perror("registerCommand");
125  }
126 
127  return status;
128 }
129 
130 ////////////////////////////////////////////////////////////////////
131 // Function: uninitializePlugin
132 // Description: Called by Maya when the plugin is unloaded.
133 ////////////////////////////////////////////////////////////////////
134 EXPCL_MISC MStatus
135 uninitializePlugin(MObject obj) {
136  MFnPlugin plugin(obj);
137  MStatus status;
138  status = plugin.deregisterCommand("pview");
139 
140  if (!status) {
141  status.perror("deregisterCommand");
142  }
143  return status;
144 }
static void * creator()
This is used to create a new instance of the plugin.
virtual MStatus doIt(const MArgList &args)
Called when the plugin command is invoked.
This class serves as a plug-in to Maya to save the scene and view it using the external pview program...
Definition: mayaSavePview.h:65