-
Notifications
You must be signed in to change notification settings - Fork 59
/
OutputMaterial.C
242 lines (202 loc) · 8.04 KB
/
OutputMaterial.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
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
#include "OutputMaterial.h"
#include <maya/MDataHandle.h>
#include <maya/MStatus.h>
#include "AssetNode.h"
#include "util.h"
OutputMaterial::OutputMaterial(HAPI_NodeId assetId)
: myAssetId(assetId),
myNodeId(-1),
myMaterialLastCookCount(0),
myBakeTexture(0)
{
}
MStatus
OutputMaterial::compute(const MTime &time,
const MPlug &materialPlug,
MDataBlock &data,
MDataHandle &materialHandle,
bool bakeTexture)
{
HAPI_Result hapiResult;
data.setClean(materialPlug);
update(materialHandle);
MDataHandle nodeIdHandle =
materialHandle.child(AssetNode::outputMaterialNodeId);
nodeIdHandle.setInt(myNodeId);
if (myNodeId < 0)
{
return MStatus::kFailure;
}
MDataHandle nameHandle =
materialHandle.child(AssetNode::outputMaterialName);
MDataHandle ambientHandle =
materialHandle.child(AssetNode::outputMaterialAmbientColor);
MDataHandle diffuseHandle =
materialHandle.child(AssetNode::outputMaterialDiffuseColor);
MDataHandle specularHandle =
materialHandle.child(AssetNode::outputMaterialSpecularColor);
MDataHandle alphaHandle =
materialHandle.child(AssetNode::outputMaterialAlphaColor);
MDataHandle texturePathHandle =
materialHandle.child(AssetNode::outputMaterialTexturePath);
HAPI_MaterialInfo materialInfo;
CHECK_HAPI(HAPI_GetMaterialInfo(
Util::theHAPISession.get(), myNodeId, &materialInfo));
if (myNodeInfo.totalCookCount > myMaterialLastCookCount ||
materialInfo.hasChanged || bakeTexture != myBakeTexture)
{
myBakeTexture = bakeTexture;
std::vector<HAPI_ParmInfo> parms(myNodeInfo.parmCount);
HAPI_GetParameters(Util::theHAPISession.get(), myNodeId, &parms[0], 0,
myNodeInfo.parmCount);
int ambientParmIndex = Util::findParm(parms, "ogl_amb");
int diffuseParmIndex = Util::findParm(parms, "ogl_diff");
int alphaParmIndex = Util::findParm(parms, "ogl_alpha");
int specularParmIndex = Util::findParm(parms, "ogl_spec");
int texturePathSHParmIndex = Util::findParm(parms, "ogl_tex#", 1);
float valueHolder[4];
nameHandle.setString(Util::HAPIString(myNodeInfo.nameSH));
if (ambientParmIndex >= 0)
{
HAPI_GetParmFloatValues(
Util::theHAPISession.get(), myNodeId, valueHolder,
parms[ambientParmIndex].floatValuesIndex, 3);
ambientHandle.set3Float(
valueHolder[0], valueHolder[1], valueHolder[2]);
}
if (specularParmIndex >= 0)
{
HAPI_GetParmFloatValues(
Util::theHAPISession.get(), myNodeId, valueHolder,
parms[specularParmIndex].floatValuesIndex, 3);
specularHandle.set3Float(
valueHolder[0], valueHolder[1], valueHolder[2]);
}
if (diffuseParmIndex >= 0)
{
HAPI_GetParmFloatValues(
Util::theHAPISession.get(), myNodeId, valueHolder,
parms[diffuseParmIndex].floatValuesIndex, 3);
diffuseHandle.set3Float(
valueHolder[0], valueHolder[1], valueHolder[2]);
}
if (alphaParmIndex >= 0)
{
HAPI_GetParmFloatValues(Util::theHAPISession.get(), myNodeId,
valueHolder,
parms[alphaParmIndex].floatValuesIndex, 1);
float alpha = 1 - valueHolder[0];
alphaHandle.set3Float(alpha, alpha, alpha);
}
if (texturePathSHParmIndex >= 0)
{
HAPI_ParmInfo texturePathParm;
HAPI_GetParameters(Util::theHAPISession.get(), myNodeId,
&texturePathParm, texturePathSHParmIndex, 1);
int texturePathSH;
HAPI_GetParmStringValues(Util::theHAPISession.get(), myNodeId, true,
&texturePathSH,
texturePathParm.stringValuesIndex, 1);
bool hasTextureSource =
((std::string)Util::HAPIString(texturePathSH)).size() > 0;
bool canRenderTexture = false;
if (hasTextureSource && bakeTexture)
{
// this could fail if texture parameter is empty
hapiResult = HAPI_RenderTextureToImage(
Util::theHAPISession.get(), myNodeId,
texturePathSHParmIndex);
canRenderTexture = hapiResult == HAPI_RESULT_SUCCESS;
}
int destinationFilePathSH = 0;
MString destinationFolderPath;
MGlobal::executeCommand(
"workspace -expandName "
"`workspace -q -fileRuleEntry sourceImages`;",
destinationFolderPath);
if (canRenderTexture && bakeTexture)
{
// this could fail if the image planes don't exist
hapiResult = HAPI_ExtractImageToFile(
Util::theHAPISession.get(), myNodeId, HAPI_PNG_FORMAT_NAME,
"C A", destinationFolderPath.asChar(), NULL,
&destinationFilePathSH);
if (HAPI_FAIL(hapiResult))
{
DISPLAY_ERROR("Could not extract image to directory:\n"
" ^1s",
destinationFolderPath);
DISPLAY_ERROR_HAPI_STATUS_CALL();
}
}
if (hasTextureSource && !bakeTexture)
{
// if baking is off but the expected texture file exists
// keep using it
hapiResult = HAPI_GetImageFilePath(
Util::theHAPISession.get(), myNodeId, HAPI_PNG_FORMAT_NAME,
"C A", destinationFolderPath.asChar(), NULL,
texturePathSHParmIndex, &destinationFilePathSH);
MString texturePath = Util::HAPIString(destinationFilePathSH);
MString cmd = "filetest -e \"" + texturePath + "\"";
int fileExists;
MGlobal::executeCommand(cmd, fileExists);
if (!fileExists)
destinationFilePathSH = 0;
}
if (destinationFilePathSH > 0)
{
MString texturePath = Util::HAPIString(destinationFilePathSH);
texturePathHandle.set(texturePath);
}
}
myMaterialLastCookCount = myNodeInfo.totalCookCount;
}
return MStatus::kSuccess;
}
void
OutputMaterial::update(MDataHandle &materialHandle)
{
MDataHandle pathHandle =
materialHandle.child(AssetNode::outputMaterialPath);
std::string path = pathHandle.asString().asChar();
if (myNodePath != path)
{
myNodeId = -1;
}
// find node id from path
if (myNodeId < 0)
{
int count;
CHECK_HAPI(
HAPI_ComposeChildNodeList(Util::theHAPISession.get(), myAssetId,
HAPI_NODETYPE_SHOP | HAPI_NODETYPE_VOP,
HAPI_NODEFLAGS_ANY, true, &count));
std::vector<HAPI_NodeId> nodeIds(count);
if (count)
{
CHECK_HAPI(HAPI_GetComposedChildNodeList(
Util::theHAPISession.get(), myAssetId, &nodeIds[0], count));
}
for (size_t i = 0; i < nodeIds.size(); i++)
{
HAPI_NodeId testNodeId = nodeIds[i];
HAPI_StringHandle testPath;
CHECK_HAPI(HAPI_GetNodePath(
Util::theHAPISession.get(), testNodeId, myAssetId, &testPath));
if (Util::HAPIString(testPath) == path)
{
myNodeId = testNodeId;
break;
}
}
}
if (myNodeId < 0)
{
return;
}
myNodePath = path;
// get material info
CHECK_HAPI(
HAPI_GetNodeInfo(Util::theHAPISession.get(), myNodeId, &myNodeInfo));
}