forked from sideeffects/HoudiniEngineForMaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssetSubCommandLoadAsset.C
170 lines (142 loc) · 4.51 KB
/
AssetSubCommandLoadAsset.C
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
#include "AssetSubCommandLoadAsset.h"
#include <maya/MFileObject.h>
#include <maya/MGlobal.h>
#include <maya/MPlug.h>
#include <maya/MPxCommand.h>
#include "AssetNode.h"
#include "AssetSubCommandSync.h"
#include "util.h"
AssetSubCommandLoadAsset::AssetSubCommandLoadAsset(
const MString &otlFilePath,
const MString &assetName
) :
myOTLFilePath(otlFilePath),
myAssetName(assetName),
myAssetSubCommandSync(NULL)
{
}
AssetSubCommandLoadAsset::~AssetSubCommandLoadAsset()
{
}
MStatus
AssetSubCommandLoadAsset::doIt()
{
MStatus status;
// create houdiniAsset node
MObject assetNodeObj = myDagModifier.createNode(
AssetNode::typeId, MObject::kNullObj, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
// rename houdiniAsset node
{
int slashPosition = myAssetName.index('/');
MString nodeName;
if(slashPosition >= 0)
{
const char* tempName = myAssetName.asChar();
// keep the part that's before the node table name
// i.e. namespace
for(int i = slashPosition; i-- > 0;)
{
if(!(('A' <= tempName[i] &&
tempName[i] <= 'Z') ||
('a' <= tempName[i] &&
tempName[i] <= 'z')))
{
nodeName += myAssetName.substring(0, i);
break;
}
}
// keep everything that's after the slash
nodeName += myAssetName.substring(
slashPosition + 1,
myAssetName.length() - 1
) + "1";
}
else
{
// this is probably an invalid node type name
nodeName = myAssetName;
}
nodeName = Util::sanitizeStringForNodeName(nodeName);
status = myDagModifier.renameNode(assetNodeObj, nodeName);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
// set otl file attribute
{
MPlug plug(assetNodeObj, AssetNode::otlFilePath);
status = myDagModifier.newPlugValueString(plug, myOTLFilePath);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
// set asset name attribute
{
MPlug plug(assetNodeObj, AssetNode::assetName);
status = myDagModifier.newPlugValueString(plug, myAssetName);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
// time1.outTime -> houdiniAsset.inTime
{
MObject srcNode = Util::findNodeByName("time1");
MPlug srcPlug = MFnDependencyNode(srcNode).findPlug("outTime");
MPlug dstPlug(assetNodeObj, AssetNode::inTime);
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
// cannot simply call redoIt, because when we use AssetSubCommandSync, we
// need to distinguish between doIt and redoIt
status = myDagModifier.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
MFnDependencyNode assetNodeFn(assetNodeObj);
// The asset should have been instantiated by now. If we couldn't
// instantiate the asset, then don't operate on the asset any further. This
// avoids generating repeated errors.
{
AssetNode* assetNode = dynamic_cast<AssetNode*>(assetNodeFn.userNode());
if(!assetNode->getAsset())
{
// If we couldn't instantiate the asset, then an error message
// should have displayed already. No need to display error here.
status = MStatus::kFailure;
}
}
// Only attempt to sync if the node is valid
if(!MFAIL(status))
{
myAssetSubCommandSync = new AssetSubCommandSync(assetNodeObj);
myAssetSubCommandSync->doIt();
}
// select the node
MGlobal::select(assetNodeObj);
// set result
MPxCommand::setResult(assetNodeFn.name());
return MStatus::kSuccess;
}
MStatus
AssetSubCommandLoadAsset::redoIt()
{
MStatus status;
status = myDagModifier.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
if(myAssetSubCommandSync)
{
status = myAssetSubCommandSync->redoIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
}
return MStatus::kSuccess;
}
MStatus
AssetSubCommandLoadAsset::undoIt()
{
MStatus status;
if(myAssetSubCommandSync)
{
status = myAssetSubCommandSync->undoIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
}
status = myDagModifier.undoIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
return MStatus::kSuccess;
}
bool AssetSubCommandLoadAsset::isUndoable() const
{
return true;
}