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