forked from ahmidou/SpliceMaya
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.cpp
345 lines (280 loc) · 11.6 KB
/
plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// [andrew 20130620] all hell breaks loose in Linux if these aren't included first
#include <QtCore/QDataStream>
#include <QtCore/QMetaType>
#include <QtGui/QTextEdit>
#include <QtGui/QComboBox>
#include <QtCore/QEvent>
#include "plugin.h"
#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>
#include <maya/MSceneMessage.h>
#include <maya/MDGMessage.h>
#include <maya/MUiMessage.h>
#include <maya/MQtUtil.h>
#include <maya/MCommandResult.h>
#include <maya/MFileIO.h>
#include <FabricSplice.h>
#include "FabricSpliceMayaNode.h"
#include "FabricSpliceMayaDeformer.h"
#include "FabricSpliceCommand.h"
#include "FabricSpliceEditorCmd.h"
#include "FabricSpliceMayaData.h"
#include "FabricSpliceToolContext.h"
#include "FabricSpliceRenderCallback.h"
#include "ProceedToNextSceneCommand.h"
#ifdef _MSC_VER
#define MAYA_EXPORT extern "C" __declspec(dllexport) MStatus _cdecl
#else
#define MAYA_EXPORT extern "C" MStatus
#endif
// FE Owned IDs 0x0011AE40 - 0x0011AF3F
const MTypeId gFirstValidNodeID(0x0011AE40);
// FabricSpliceMayaNode 0x0011AE41
// FabricSpliceMayaDeformer 0x0011AE42
// FabricSpliceInlineGeometry 0x0011AE43 /* no longer in use, but not available */
// FabricSpliceMayaDebugger 0x0011AE44 /* no longer in use, but not available */
// FabricSpliceMayaData 0x0011AE45
const MTypeId gLastValidNodeID(0x0011AE49);
MCallbackId gOnSceneNewCallbackId;
MCallbackId gOnSceneLoadCallbackId;
MCallbackId gOnSceneSaveCallbackId;
MCallbackId gOnMayaExitCallbackId;
MCallbackId gOnSceneImportCallbackId;
MCallbackId gOnSceneExportCallbackId;
MCallbackId gOnSceneReferenceCallbackId;
MCallbackId gOnSceneImportReferenceCallbackId;
MCallbackId gRenderCallback0;
MCallbackId gRenderCallback1;
MCallbackId gRenderCallback2;
MCallbackId gRenderCallback3;
MCallbackId gRenderCallback4;
MCallbackId gOnNodeAddedCallbackId;
MCallbackId gOnNodeRemovedCallbackId;
MCallbackId gBeforeSceneOpenCallbackId;
MString gLastLoadedScene;
MString mayaGetLastLoadedScene()
{
return gLastLoadedScene;
}
MString gModuleFolder;
void initModuleFolder(MFnPlugin &plugin){
MString pluginPath = plugin.loadPath();
MString lastFolder("plug-ins");
gModuleFolder = pluginPath.substring(0, pluginPath.length() - lastFolder.length() - 2);
}
MString getModuleFolder()
{
return gModuleFolder;
}
void onSceneSave(void *userData){
MStatus status = MS::kSuccess;
gLastLoadedScene = MFileIO::beforeSaveFilename(&status);
if(gLastLoadedScene.length() == 0) // this happens during copy & paste
return;
std::vector<FabricSpliceBaseInterface*> instances = FabricSpliceBaseInterface::getInstances();
for(int i = 0; i < instances.size(); ++i){
FabricSpliceBaseInterface *node = instances[i];
node->storePersistenceData(gLastLoadedScene, &status);
}
}
void onSceneNew(void *userData){
MGlobal::executeCommandOnIdle("unloadPlugin \"FabricSpliceManipulation.py\";");
MGlobal::executeCommandOnIdle("loadPlugin \"FabricSpliceManipulation.py\";");
FabricSpliceEditorWidget::postUpdateAll();
FabricSpliceRenderCallback::sDrawContext.invalidate();
FabricSplice::DestroyClient();
}
void onSceneLoad(void *userData){
onSceneNew(userData);
if(getenv("FABRIC_SPLICE_PROFILING") != NULL)
FabricSplice::Logging::enableTimers();
MStatus status = MS::kSuccess;
gLastLoadedScene = MFileIO::currentFile();
std::vector<FabricSpliceBaseInterface*> instances = FabricSpliceBaseInterface::getInstances();
// each node will only restore once, so it's safe for import too
FabricSplice::Logging::AutoTimer persistenceTimer("Maya::onSceneLoad");
for(int i = 0; i < instances.size(); ++i){
FabricSpliceBaseInterface *node = instances[i];
node->restoreFromPersistenceData(gLastLoadedScene, &status);
if( status != MS::kSuccess)
return;
}
FabricSpliceEditorWidget::postUpdateAll();
if(getenv("FABRIC_SPLICE_PROFILING") != NULL)
{
for(unsigned int i=0;i<FabricSplice::Logging::getNbTimers();i++)
{
std::string name = FabricSplice::Logging::getTimerName(i);
FabricSplice::Logging::logTimer(name.c_str());
FabricSplice::Logging::resetTimer(name.c_str());
}
}
}
bool gSceneIsDestroying = false;
void onMayaExiting(void *userData){
gSceneIsDestroying = true;
std::vector<FabricSpliceBaseInterface*> instances = FabricSpliceBaseInterface::getInstances();
for(int i = 0; i < instances.size(); ++i){
FabricSpliceBaseInterface *node = instances[i];
node->resetInternalData();
}
FabricSplice::DestroyClient(true);
}
bool isDestroyingScene()
{
return gSceneIsDestroying;
}
void mayaLogFunc(const MString & message)
{
MGlobal::displayInfo(MString("[Splice] ")+message);
}
void mayaLogFunc(const char * message, unsigned int length)
{
mayaLogFunc(MString(message));
}
bool gErrorEnabled = true;
void mayaErrorLogEnable(bool enable)
{
gErrorEnabled = enable;
}
bool gErrorOccured = false;
void mayaLogErrorFunc(const MString & message)
{
if(!gErrorEnabled)
return;
MGlobal::displayError(MString("[Splice] ")+message);
gErrorOccured = true;
}
void mayaLogErrorFunc(const char * message, unsigned int length)
{
mayaLogErrorFunc(MString(message));
}
void mayaClearError()
{
gErrorOccured = false;
}
MStatus mayaErrorOccured()
{
MStatus result = MS::kSuccess;
if(gErrorOccured)
result = MS::kFailure;
gErrorOccured = false;
return result;
}
void mayaKLReportFunc(const char * message, unsigned int length)
{
MGlobal::displayInfo(MString("[KL]: ")+MString(message));
}
void mayaCompilerErrorFunc(unsigned int row, unsigned int col, const char * file, const char * level, const char * desc)
{
MString line;
line.set(row);
MGlobal::displayInfo("[KL Compiler "+MString(level)+"]: line "+line+", op '"+MString(file)+"': "+MString(desc));
FabricSpliceEditorWidget::reportAllCompilerError(row, col, file, level, desc);
}
void mayaKLStatusFunc(const char * topic, unsigned int topicLength, const char * message, unsigned int messageLength)
{
MGlobal::displayInfo(MString("[KL Status]: ")+MString(message));
}
void mayaRefreshFunc()
{
MGlobal::executeCommandOnIdle("refresh");
}
#if defined(OSMac_)
__attribute__ ((visibility("default")))
#endif
MAYA_EXPORT initializePlugin(MObject obj)
{
MFnPlugin plugin(obj, getPluginName().asChar(), "1.0", "Any");
MStatus status;
status = plugin.registerContextCommand("FabricSpliceToolContext", FabricSpliceToolContextCmd::creator, "FabricSpliceToolCommand", FabricSpliceToolCmd::creator );
loadMenu();
gOnSceneSaveCallbackId = MSceneMessage::addCallback(MSceneMessage::kBeforeSave, onSceneSave);
gOnSceneLoadCallbackId = MSceneMessage::addCallback(MSceneMessage::kAfterOpen, onSceneLoad);
gBeforeSceneOpenCallbackId = MSceneMessage::addCallback(MSceneMessage::kBeforeOpen, onSceneNew);
gOnSceneNewCallbackId = MSceneMessage::addCallback(MSceneMessage::kBeforeNew, onSceneNew);
gOnMayaExitCallbackId = MSceneMessage::addCallback(MSceneMessage::kMayaExiting, onMayaExiting);
gOnSceneExportCallbackId = MSceneMessage::addCallback(MSceneMessage::kBeforeExport, onSceneSave);
gOnSceneImportCallbackId = MSceneMessage::addCallback(MSceneMessage::kAfterImport, onSceneLoad);
gOnSceneReferenceCallbackId = MSceneMessage::addCallback(MSceneMessage::kAfterReference, onSceneLoad);
gOnSceneImportReferenceCallbackId = MSceneMessage::addCallback(MSceneMessage::kAfterImportReference, onSceneLoad);
gRenderCallback0 = MUiMessage::add3dViewPostRenderMsgCallback("modelPanel0", FabricSpliceRenderCallback::draw);
gRenderCallback1 = MUiMessage::add3dViewPostRenderMsgCallback("modelPanel1", FabricSpliceRenderCallback::draw);
gRenderCallback2 = MUiMessage::add3dViewPostRenderMsgCallback("modelPanel2", FabricSpliceRenderCallback::draw);
gRenderCallback3 = MUiMessage::add3dViewPostRenderMsgCallback("modelPanel3", FabricSpliceRenderCallback::draw);
gRenderCallback4 = MUiMessage::add3dViewPostRenderMsgCallback("modelPanel4", FabricSpliceRenderCallback::draw);
gOnNodeAddedCallbackId = MDGMessage::addNodeAddedCallback(FabricSpliceBaseInterface::onNodeAdded);
gOnNodeRemovedCallbackId = MDGMessage::addNodeRemovedCallback(FabricSpliceBaseInterface::onNodeRemoved);
plugin.registerData(FabricSpliceMayaData::typeName, FabricSpliceMayaData::id, FabricSpliceMayaData::creator);
plugin.registerCommand("fabricSplice", FabricSpliceCommand::creator);//, FabricSpliceEditorCmd::newSyntax);
plugin.registerCommand("fabricSpliceEditor", FabricSpliceEditorCmd::creator, FabricSpliceEditorCmd::newSyntax);
plugin.registerCommand("fabricSpliceManipulation", FabricSpliceManipulationCmd::creator);
plugin.registerCommand("proceedToNextScene", ProceedToNextSceneCommand::creator);//, FabricSpliceEditorCmd::newSyntax);
plugin.registerNode("spliceMayaNode", FabricSpliceMayaNode::id, FabricSpliceMayaNode::creator, FabricSpliceMayaNode::initialize);
plugin.registerNode("spliceMayaDeformer", FabricSpliceMayaDeformer::id, FabricSpliceMayaDeformer::creator, FabricSpliceMayaDeformer::initialize, MPxNode::kDeformerNode);
MQtUtil::registerUIType("FabricSpliceEditor", FabricSpliceEditorWidget::creator, "fabricSpliceEditor");
initModuleFolder(plugin);
FabricSplice::Initialize();
FabricSplice::Logging::setLogFunc(mayaLogFunc);
FabricSplice::Logging::setLogErrorFunc(mayaLogErrorFunc);
FabricSplice::Logging::setKLReportFunc(mayaKLReportFunc);
FabricSplice::Logging::setKLStatusFunc(mayaKLStatusFunc);
FabricSplice::Logging::setCompilerErrorFunc(mayaCompilerErrorFunc);
// FabricSplice::SceneManagement::setManipulationFunc(FabricSpliceBaseInterface::manipulationCallback);
MGlobal::executePythonCommandOnIdle("import AEspliceMayaNodeTemplate", true);
return status;
}
#if defined(OSMac_)
__attribute__ ((visibility("default")))
#endif
MAYA_EXPORT uninitializePlugin(MObject obj)
{
MFnPlugin plugin(obj);
MStatus status;
unloadMenu();
plugin.deregisterCommand("fabricSplice");
plugin.deregisterCommand("FabricSpliceEditor");
plugin.deregisterCommand("proceedToNextScene");
plugin.deregisterNode(FabricSpliceMayaNode::id);
plugin.deregisterNode(FabricSpliceMayaDeformer::id);
MSceneMessage::removeCallback(gOnSceneSaveCallbackId);
MSceneMessage::removeCallback(gOnSceneLoadCallbackId);
MSceneMessage::removeCallback(gOnMayaExitCallbackId);
MSceneMessage::removeCallback(gOnSceneImportCallbackId);
MSceneMessage::removeCallback(gOnSceneExportCallbackId);
MSceneMessage::removeCallback(gOnSceneReferenceCallbackId);
MSceneMessage::removeCallback(gOnSceneImportReferenceCallbackId);
MUiMessage::removeCallback(gRenderCallback0);
MUiMessage::removeCallback(gRenderCallback1);
MUiMessage::removeCallback(gRenderCallback2);
MUiMessage::removeCallback(gRenderCallback3);
MUiMessage::removeCallback(gRenderCallback4);
MDGMessage::removeCallback(gOnNodeAddedCallbackId);
MDGMessage::removeCallback(gOnNodeRemovedCallbackId);
plugin.deregisterData(FabricSpliceMayaData::id);
MQtUtil::deregisterUIType("FabricSpliceEditor");
plugin.deregisterContextCommand("FabricSpliceToolContext", "FabricSpliceToolCommand");
FabricSplice::DestroyClient();
FabricSplice::Finalize();
return status;
}
MString getPluginName()
{
return "FabricSpliceMaya";
}
void loadMenu()
{
MString cmd = "source \"FabricSpliceMenu.mel\"; FabricSpliceLoadMenu(\"";
cmd += getPluginName();
cmd += "\");";
MStatus commandStatus = MGlobal::executeCommandOnIdle(cmd, false);
if (commandStatus != MStatus::kSuccess)
MGlobal::displayError("Failed to load FabricSpliceMenu.mel. Adjust your MAYA_SCRIPT_PATH!");
}
void unloadMenu()
{
MString cmd = "source \"FabricSpliceMenu.mel\"; FabricSpliceUnloadMenu();";
MStatus commandStatus = MGlobal::executeCommandOnIdle(cmd, false);
if (commandStatus != MStatus::kSuccess)
MGlobal::displayError("Failed to load FabricSpliceMenu.mel. Adjust your MAYA_SCRIPT_PATH!");
}