-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
460 lines (387 loc) · 14.7 KB
/
Copy pathutils.cpp
File metadata and controls
460 lines (387 loc) · 14.7 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "utils.h"
#include <maya/MGlobal.h>
#include <maya/MItDependencyNodes.h>
#include <maya/MDGModifier.h>
#include <maya/MDagModifier.h>
#include <maya/MPlugArray.h>
#include <maya/MDagPath.h>
#include <maya/MMatrix.h>
#include <maya/MSelectionList.h>
#include <maya/MFnMesh.h>
#include <maya/MFnSingleIndexedComponent.h>
#include <windows.h>
#include <sstream>
#include <cstring>
// Anonymous namespace to keep this constant's linkage internal to this file.
namespace {
const uint32_t magicBitsMask3DDecode[] = {
0x00000000, 0x000003ff, 0x000300ff, 0x0300f00f, 0x30c30c3, 0x9249249
};
}
namespace Utils {
uint32_t toMortonCode(uint32_t x, uint32_t y, uint32_t z) {
auto spreadBits = [](uint32_t value) -> uint32_t {
value = (value | (value << 16)) & 0x030000FF;
value = (value | (value << 8)) & 0x0300F00F;
value = (value | (value << 4)) & 0x030C30C3;
value = (value | (value << 2)) & 0x09249249;
return value;
};
uint32_t xBits = spreadBits(x);
uint32_t yBits = spreadBits(y) << 1;
uint32_t zBits = spreadBits(z) << 2;
return xBits | yBits | zBits;
}
void fromMortonCode(uint32_t mortonCode, uint32_t& x, uint32_t& y, uint32_t& z) {
auto compactBits = [](uint32_t value) -> uint32_t {
value &= 0x09249249;
value = (value ^ (value >> 2)) & 0x030C30C3;
value = (value ^ (value >> 4)) & 0x0300F00F;
value = (value ^ (value >> 8)) & 0x030000FF;
value = (value ^ (value >> 16)) & 0x000003FF;
return value;
};
x = compactBits(mortonCode);
y = compactBits(mortonCode >> 1);
z = compactBits(mortonCode >> 2);
}
DWORD loadResourceFile(HINSTANCE pluginInstance, int id, const wchar_t* type, void** resourceData) {
HRSRC hResource = FindResource(pluginInstance, MAKEINTRESOURCE(id), type);
if (!hResource) {
MGlobal::displayError(MString("Failed to find resource with ID: ") + id);
return 0;
}
HGLOBAL hResourceData = LoadResource(pluginInstance, hResource);
if (!hResourceData) {
MGlobal::displayError(MString("Failed to load resource with ID: ") + id);
return 0;
}
*resourceData = LockResource(hResourceData);
if (!*resourceData) {
MGlobal::displayError(MString("Failed to lock resource with ID: ") + id);
return 0;
}
DWORD resourceSize = SizeofResource(pluginInstance, hResource);
if (resourceSize == 0) {
MGlobal::displayError(MString("Failed to get size of resource with ID: ") + id);
return 0;
}
return resourceSize;
}
void loadMELScriptByResourceID(HINSTANCE pluginInstance, int resourceID) {
void* data = nullptr;
DWORD size = loadResourceFile(pluginInstance, resourceID, L"MEL", &data);
if (size == 0) {
MGlobal::displayError("Failed to load MEL script resource.");
return;
}
MString melScript(static_cast<char*>(data), size);
// Execute the MEL script to load it into memory
MStatus status = MGlobal::executeCommand(melScript);
if (status != MS::kSuccess) {
MGlobal::displayError("Failed to execute MEL script: " + status.errorString());
}
}
/**
* Extracts a resource from the plugin .mll file (which contains windows resource files), and writes it to the specified output file path.
* This allows us to put files in the plugin and extract them to disk at runtime, e.g. icon files.
*/
bool extractResourceToFile(HINSTANCE pluginInstance, int resourceID, const wchar_t* type, const MString& outputFilePath) {
void* data = nullptr;
DWORD size = Utils::loadResourceFile(pluginInstance, resourceID, type, &data);
if (size == 0) return false;
// Ensure folder exists
std::wstring wpath = std::wstring(outputFilePath.asWChar());
size_t lastSlash = wpath.find_last_of(L"/\\");
if (lastSlash != std::wstring::npos) {
std::wstring dir = wpath.substr(0, lastSlash);
CreateDirectoryW(dir.c_str(), nullptr); // OK if already exists
}
HANDLE hFile = CreateFileW(wpath.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (hFile == INVALID_HANDLE_VALUE) {
MGlobal::displayError("Failed to create icon file: " + outputFilePath);
return false;
}
DWORD written = 0;
BOOL ok = WriteFile(hFile, data, size, &written, nullptr);
CloseHandle(hFile);
if (!ok || written != size) {
MGlobal::displayError("Failed to write icon file completely: " + outputFilePath);
return false;
}
return true;
}
std::string HResultToString(const HRESULT& hr) {
std::string result;
char* msgBuf = nullptr;
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
DWORD langId = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
// Try to get system message string for the HRESULT
if (FormatMessageA(flags, nullptr, hr, langId, (LPSTR)&msgBuf, 0, nullptr) && msgBuf) {
result = msgBuf;
LocalFree(msgBuf);
} else {
result = "Unknown error";
}
// Append HRESULT hex
std::stringstream ss;
ss << " (HRESULT: 0x" << std::hex << std::uppercase << static_cast<unsigned long>(hr);
// Extract facility and code
WORD facility = HRESULT_FACILITY(hr);
WORD code = HRESULT_CODE(hr);
ss << ", Facility: " << std::dec << facility << ", Code: " << code;
// Check if it's a wrapped Win32 error
if (facility == FACILITY_WIN32) {
char* win32Msg = nullptr;
if (FormatMessageA(flags, nullptr, code, langId, (LPSTR)&win32Msg, 0, nullptr) && win32Msg) {
ss << ", Win32 message: \"" << win32Msg << "\"";
LocalFree(win32Msg);
}
}
ss << ")";
result += ss.str();
return result;
}
uint16_t floatToHalf(float value) {
uint32_t bits;
std::memcpy(&bits, &value, sizeof(bits));
uint32_t sign = (bits >> 16) & 0x8000;
uint32_t exponent = ((bits >> 23) & 0xFF) - 112;
uint32_t mantissa = bits & 0x007FFFFF;
if (exponent <= 0) {
// Subnormal or zero
return sign;
} else if (exponent >= 31) {
// Inf or NaN
return sign | 0x7C00;
} else {
return sign | (exponent << 10) | (mantissa >> 13);
}
}
uint32_t packTwoFloatsInUint32(float a, float b) {
uint32_t ha = static_cast<uint32_t>(floatToHalf(a));
uint32_t hb = static_cast<uint32_t>(floatToHalf(b));
return (hb << 16) | ha;
}
MFloatVector sign(const MFloatVector& v) {
return MFloatVector(
(v.x > 0) ? 1.0f : (v.x < 0) ? -1.0f : 0.0f,
(v.y > 0) ? 1.0f : (v.y < 0) ? -1.0f : 0.0f,
(v.z > 0) ? 1.0f : (v.z < 0) ? -1.0f : 0.0f
);
}
/**
* Logical indices are sparse, mapped to contiguous physical indices.
* This method finds the next available logical index for creating a new plug in the array.
*/
uint getNextArrayPlugIndex(const MObject& dependencyNode, const MObject& arrayAttribute) {
MStatus status;
uint nextIndex = 0;
MPlug arrayPlug(dependencyNode, arrayAttribute);
const uint numElements = arrayPlug.evaluateNumElements(&status);
for (uint i = 0; i < numElements; ++i) {
uint idx = arrayPlug.elementByPhysicalIndex(i, &status).logicalIndex();
if (idx >= nextIndex) {
nextIndex = idx + 1;
}
}
return nextIndex;
}
MPlug getGlobalTimePlug() {
MItDependencyNodes it(MFn::kTime);
// Assumes there's only one time node in the scene, which is a pretty safe assumption.
if (!it.isDone()) {
MFnDependencyNode timeNode(it.thisNode());
return timeNode.findPlug("outTime", false);
}
return MPlug();
}
void connectPlugs(
const MPlug& srcPlug,
const MPlug& dstPlug,
bool breakConnection
) {
MDGModifier dgMod;
breakConnection ?
dgMod.disconnect(srcPlug, dstPlug) :
dgMod.connect(srcPlug, dstPlug);
dgMod.doIt();
}
void disconnectPlugs(
const MPlug& srcPlug,
const MPlug& dstPlug
) {
MDGModifier dgMod;
dgMod.disconnect(srcPlug, dstPlug);
dgMod.doIt();
}
void removePlugMultiInstance(const MPlug& plug, int logicalIndexToRemove) {
MDGModifier dgMod;
MPlug plugToRemove = plug;
if (logicalIndexToRemove != -1) {
plugToRemove = plug.elementByLogicalIndex(logicalIndexToRemove);
}
dgMod.removeMultiInstance(plugToRemove, true);
dgMod.doIt();
}
int arrayPlugNumElements(const MObject& dependencyNode, const MObject& arrayAttribute) {
return MPlug(dependencyNode, arrayAttribute).evaluateNumElements();
}
MPxNode* connectedNode(const MPlug& plug, bool nodeIsSource) {
MPlugArray conns;
if (!plug.connectedTo(conns, nodeIsSource, !nodeIsSource) || conns.length() == 0) return nullptr;
MObject connectedObj = conns[0].node(); // API returns a plug array but util assumes only one connection
MFnDependencyNode fnNode(connectedObj);
return fnNode.userNode();
}
MObject createDGNode(const MString& typeName)
{
MDGModifier dgMod;
MObject nodeObj = dgMod.createNode(typeName);
dgMod.doIt();
return nodeObj;
}
void deleteDGNode(const MObject& nodeObj)
{
MDGModifier dgMod;
dgMod.deleteNode(nodeObj);
dgMod.doIt();
}
MObject createDagNode(const MString& typeName, const MObject& parent, const MString& name, MDagModifier* dagMod)
{
MDagModifier localMod;
MDagModifier* mod = dagMod ? dagMod : &localMod;
MObject nodeObj = mod->createNode(typeName, parent);
mod->doIt();
MFnDependencyNode fnNode(nodeObj);
fnNode.setName(name);
return nodeObj;
}
MMatrix getWorldMatrixWithoutScale(const MObject& object) {
MDagPath objPath;
MStatus status = MDagPath::getAPathTo(object, objPath);
if (status != MS::kSuccess) {
return MMatrix::identity;
}
MMatrix worldMatrix = objPath.inclusiveMatrix();
MTransformationMatrix transformMatrix(worldMatrix);
double qx = 0.0, qy = 0.0, qz = 0.0, qw = 1.0;
transformMatrix.getRotationQuaternion(qx, qy, qz, qw);
MVector translation = transformMatrix.getTranslation(MSpace::kWorld);
MTransformationMatrix unscaledMatrix;
unscaledMatrix.setTranslation(translation, MSpace::kWorld);
unscaledMatrix.setRotationQuaternion(qx, qy, qz, qw);
return unscaledMatrix.asMatrix();
}
// Note: this isn't particularly fast (iterates over all nodes), so don't use in performance-critical paths.
// (A better method may involve selection lists / using Maya's built in name resolution)
MObject getNodeFromName(const MString& name) {
MItDependencyNodes it;
for (; !it.isDone(); it.next()) {
MFnDependencyNode fnNode(it.thisNode());
if (fnNode.name() == name) {
return it.thisNode();
}
}
return MObject::kNullObj;
}
MObject getMostRecentlySelectedObject() {
MSelectionList selectionList;
MGlobal::getActiveSelectionList(selectionList);
if (selectionList.length() == 0) {
return MObject::kNullObj;
}
MObject selectedObj;
selectionList.getDependNode(selectionList.length() - 1, selectedObj);
return selectedObj;
}
bool tryGetShapePathFromObject(const MObject& object, MDagPath& shapePath) {
MStatus status = MDagPath::getAPathTo(object, shapePath);
if (status != MS::kSuccess) return false; // Not a DAG object
if (shapePath.hasFn(MFn::kTransform)) {
shapePath.extendToShape();
return shapePath.hasFn(MFn::kShape);
}
if (shapePath.hasFn(MFn::kShape)) {
return true;
}
return false;
}
MDagPath getDagPathFromName(const MString& name) {
MSelectionList selectionList;
selectionList.add(name);
MDagPath dagPath;
selectionList.getDagPath(0, dagPath);
return dagPath;
}
// Use MEL to query and perform linking (in this case, C++ is _much_ more verbose and this isn't performance critical)
void transferUVLinks(const MDagPath& srcMeshPath, const MDagPath& dstMeshPath) {
MFnMesh srcMeshFn(srcMeshPath);
// This command gets all texture nodes linked to a given UV set.
MString queryLinkTemplateCmd = "uvLink -q -uvs " + srcMeshPath.fullPathName() + ".uvSet[^1s].uvSetName;";
MString addLinkTemplateCmd = "uvLink -make -uvs " + dstMeshPath.fullPathName() + ".uvSet[^1s].uvSetName -texture \"^2s\";";
MString uvSetNumStr;
MString queryLinksCmd;
MStringArray textureNameLinks;
MString addLinkCmd;
for (int i = 0; i < srcMeshFn.numUVSets(); ++i) {
textureNameLinks.clear();
uvSetNumStr.set(i);
queryLinksCmd = queryLinkTemplateCmd;
queryLinksCmd.format(queryLinkTemplateCmd, uvSetNumStr);
MGlobal::executeCommand(queryLinksCmd, textureNameLinks);
for (uint j = 0; j < textureNameLinks.length(); ++j) {
MString textureName = textureNameLinks[j];
addLinkCmd = addLinkTemplateCmd;
addLinkCmd.format(addLinkTemplateCmd, uvSetNumStr, textureName);
MGlobal::executeCommand(addLinkCmd);
}
}
}
bool MStringArrayContains(const MStringArray& array, const MString& value) {
for (unsigned int i = 0; i < array.length(); ++i) {
if (array[i] == value) {
return true;
}
}
return false;
}
void deleteDefaultUVSet(const MString& meshName) {
// Can't delete the default UV set while it's current - reorder UV sets, first, if possible.
MStringArray allUVSets;
MGlobal::executeCommand("polyUVSet -q -allUVSets " + meshName, allUVSets);
MString firstUVSet = allUVSets[0];
if (allUVSets.length() < 2) {
MGlobal::displayWarning(MString("cubit cannot delete default UV set ") + firstUVSet + " because it's the only UV set");
return;
}
MString secondUVSet = allUVSets[1];
MGlobal::executeCommand("polyUVSet -reorder -uvSet " + firstUVSet + " -newUVSet " + secondUVSet + " " + meshName + ";");
MGlobal::executeCommand("polyUVSet -delete -uvSet " + firstUVSet + " " + meshName + ";");
}
MString getActiveModelPanelName() {
MString result;
MGlobal::executeCommand("playblast -ae", result);
// Parse the result to get the active model panel name (result is in form MainPane|viewPanes|modelPanel4|modelPanel4|modelPanel4)
MStringArray parts;
result.split('|', parts);
return parts[parts.length() - 1];
}
MStringArray getAllModelPanelNames() {
MStringArray panelNames;
MGlobal::executeCommand("getPanel -type \"modelPanel\"", panelNames);
return panelNames;
}
MObject combineFaceComponents(MObjectArray& faceComponents) {
MFnSingleIndexedComponent fnCombinedComponent;
MObject combinedComponent = fnCombinedComponent.create(MFn::kMeshPolygonComponent);
MFnSingleIndexedComponent fnComponent_i;
for (uint i = 0; i < faceComponents.length(); ++i) {
fnComponent_i.setObject(faceComponents[i]);
for (int j = 0; j < fnComponent_i.elementCount(); ++j) {
fnCombinedComponent.addElement(fnComponent_i.element(j));
}
}
return combinedComponent;
}
} // namespace Utils