-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathOutputMaterial.C
210 lines (184 loc) · 6.51 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
#include "OutputMaterial.h"
#include <maya/MDataHandle.h>
#include <maya/MStatus.h>
#include "AssetNode.h"
#include "util.h"
OutputMaterial::OutputMaterial() :
myNodeId(-1),
myMaterialLastCookCount(0)
{
}
MStatus
OutputMaterial::compute(
const MTime &time,
MDataHandle &materialHandle
)
{
HAPI_Result hapiResult;
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)
{
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)
{
// this could fail if texture parameter is empty
hapiResult = HAPI_RenderTextureToImage(
Util::theHAPISession.get(),
myNodeId,
texturePathSHParmIndex
);
canRenderTexture = hapiResult == HAPI_RESULT_SUCCESS;
}
int destinationFilePathSH = 0;
if(canRenderTexture)
{
MString destinationFolderPath;
MGlobal::executeCommand("workspace -expandName "
"`workspace -q -fileRuleEntry sourceImages`;",
destinationFolderPath);
// 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(destinationFilePathSH > 0)
{
MString texturePath = Util::HAPIString(destinationFilePathSH);
texturePathHandle.set(texturePath);
}
}
myMaterialLastCookCount = myNodeInfo.totalCookCount;
}
materialHandle.setClean();
ambientHandle.setClean();
diffuseHandle.setClean();
specularHandle.setClean();
alphaHandle.setClean();
texturePathHandle.setClean();
return MStatus::kSuccess;
}