forked from DCCStudios/Maya_NifTools_Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNifTranslator.cpp
More file actions
444 lines (379 loc) · 16.7 KB
/
Copy pathNifTranslator.cpp
File metadata and controls
444 lines (379 loc) · 16.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
#include "NifTranslator.h"
#include "include/Importers/NiflyImporter.h"
#include "include/Common/NiflyExportBridge.h"
#include "include/Exporters/NiflyExportingFixture.h"
#include "include/Common/NifDragAndDropCallback.h"
#include "include/Custom Nodes/NifBhkRigidBody.h"
#include "include/Custom Nodes/NifBhkShape.h"
#include <fstream>
#include <ctime>
#include <iomanip>
#include <filesystem>
//#define _DEBUG
ofstream out;
static NifDragAndDropCallback* gNifDragAndDropCallback = NULL;
//--Function Definitions--//
//--NifTranslator::identifyFile--//
MPxFileTranslator::MFileKind NifTranslator::identifyFile(const MFileObject& fileName, const char* buffer, short size) const {
MString fName = fileName.name();
if (fName.toUpperCase() != "NIF" && fName.toUpperCase() != "KF")
return kNotMyFileType;
return kIsMyFileType;
}
//--Plug-in Load/Unload--//
//--initializePlugin--//
MStatus initializePlugin(MObject obj) {
#ifdef _DEBUG
out.open("C:\\Maya NIF Plug-in Log.txt", ofstream::binary);
#endif
MStatus status;
MFnPlugin plugin(obj, "NifTools", PLUGIN_VERSION);
status = plugin.registerFileTranslator(TRANSLATOR_NAME,
"nifTranslator.rgb",
NifTranslator::creator,
"nifTranslatorOpts",
NULL,
false);
if (!status) {
status.perror("registerFileTranslator");
return status;
}
status = plugin.registerNode("nifDismemberPartition", NifDismemberPartition::id, NifDismemberPartition::creator, NifDismemberPartition::initialize, MPxNode::kDependNode);
if (!status) {
status.perror("registerNifDismemberPartition");
return status;
}
status = plugin.registerNode("nifBhkRigidBody", NifBhkRigidBody::id,
NifBhkRigidBody::creator, NifBhkRigidBody::initialize);
if (!status) {
status.perror("registerNode nifBhkRigidBody failed");
MGlobal::displayError(MString("registerNode nifBhkRigidBody failed: ") + status.errorString());
}
else {
MGlobal::displayInfo("registerNode nifBhkRigidBody succeeded.");
}
// AETemplate for nifBhkRigidBody, defined inline as a MEL string instead
// of a separate scripts/AE*.mel file - keeps the whole plugin self
// contained, with nothing extra to lose during distribution/install.
// This only customizes the "Bhk Flags" section (2-column checkbox grid
// instead of Maya's default one-per-row stack); everything else on the
// node falls through to -addExtraControls, which reproduces Maya's
// normal automatic layout unchanged.
{
MString aeTemplateScript =
"global proc AEnifBhkRigidBody_bhkFlagsNew(string $a0, string $a1, string $a2, string $a3, string $a4, string $a5, string $a6, string $a7, string $a8, string $a9, string $a10, string $a11) {\n"
" string $names[12];\n"
" $names[0]=\"Active\"; $names[1]=\"Reset Trans\"; $names[2]=\"Notify\"; $names[3]=\"Set Local\";\n"
" $names[4]=\"Dbg Display\"; $names[5]=\"Use Vel\"; $names[6]=\"Reset\"; $names[7]=\"Sync On Update\";\n"
" $names[8]=\"Blend Pos\"; $names[9]=\"Always Blend\"; $names[10]=\"Anim Targeted\"; $names[11]=\"Dismembered Limb\";\n"
" string $attrs[12];\n"
" $attrs[0]=$a0; $attrs[1]=$a1; $attrs[2]=$a2; $attrs[3]=$a3; $attrs[4]=$a4; $attrs[5]=$a5;\n"
" $attrs[6]=$a6; $attrs[7]=$a7; $attrs[8]=$a8; $attrs[9]=$a9; $attrs[10]=$a10; $attrs[11]=$a11;\n"
" columnLayout -adjustableColumn 1;\n"
" int $i;\n"
" for ($i = 0; $i < 6; $i++) {\n"
" rowLayout -numberOfColumns 4 -columnWidth4 150 20 150 20 -columnAttach4 \"right\" \"left\" \"right\" \"left\";\n"
" text -label ($names[$i] + \" \");\n"
" checkBox -label \"\" (\"bhkFlagBox\" + $i);\n"
" text -label ($names[$i+6] + \" \");\n"
" checkBox -label \"\" (\"bhkFlagBox\" + ($i+6));\n"
" setParent ..;\n"
" }\n"
" setParent ..;\n"
" AEnifBhkRigidBody_bhkFlagsReplace($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7,$a8,$a9,$a10,$a11);\n"
"}\n"
"global proc AEnifBhkRigidBody_bhkFlagsReplace(string $a0, string $a1, string $a2, string $a3, string $a4, string $a5, string $a6, string $a7, string $a8, string $a9, string $a10, string $a11) {\n"
" string $attrs[12];\n"
" $attrs[0]=$a0; $attrs[1]=$a1; $attrs[2]=$a2; $attrs[3]=$a3; $attrs[4]=$a4; $attrs[5]=$a5;\n"
" $attrs[6]=$a6; $attrs[7]=$a7; $attrs[8]=$a8; $attrs[9]=$a9; $attrs[10]=$a10; $attrs[11]=$a11;\n"
" int $i;\n"
" for ($i = 0; $i < 12; $i++) {\n"
" connectControl (\"bhkFlagBox\" + $i) $attrs[$i];\n"
" }\n"
"}\n"
"global proc AEnifBhkRigidBodyTemplate(string $nodeName) {\n"
" editorTemplate -suppress \"caching\";\n"
" editorTemplate -suppress \"frozen\";\n"
" editorTemplate -suppress \"nodeState\";\n"
" editorTemplate -beginScrollLayout;\n"
" editorTemplate -addControl \"targetShape\";\n"
" editorTemplate -beginLayout \"Bhk Flags\" -collapse 0;\n"
" editorTemplate -callCustom \"AEnifBhkRigidBody_bhkFlagsNew\" \"AEnifBhkRigidBody_bhkFlagsReplace\" \"bhkFlagActive\" \"bhkFlagResetTrans\" \"bhkFlagNotify\" \"bhkFlagSetLocal\" \"bhkFlagDbgDisplay\" \"bhkFlagUseVel\" \"bhkFlagReset\" \"bhkFlagSyncOnUpdate\" \"bhkFlagBlendPos\" \"bhkFlagAlwaysBlend\" \"bhkFlagAnimTargeted\" \"bhkFlagDismemberedLimb\";\n"
" editorTemplate -endLayout;\n"
" editorTemplate -addControl \"isHavokT\";\n"
" editorTemplate -addControl \"layer\";\n"
" editorTemplate -addControl \"havokFilterFlags\";\n"
" editorTemplate -addControl \"motionSystem\";\n"
" editorTemplate -addControl \"qualityType\";\n"
" editorTemplate -addControl \"deactivatorType\";\n"
" editorTemplate -addControl \"solverDeactivation\";\n"
" editorTemplate -addControl \"collisionResponse\";\n"
" editorTemplate -addControl \"mass\";\n"
" editorTemplate -addControl \"friction\";\n"
" editorTemplate -addControl \"restitution\";\n"
" editorTemplate -addControl \"linearDamping\";\n"
" editorTemplate -addControl \"angularDamping\";\n"
" editorTemplate -addControl \"maxLinearVelocity\";\n"
" editorTemplate -addControl \"maxAngularVelocity\";\n"
" editorTemplate -addControl \"penetrationDepth\";\n"
" editorTemplate -addControl \"havokTranslation\";\n"
" editorTemplate -addControl \"havokRotation\";\n"
" editorTemplate -addControl \"havokRotationW\";\n"
" editorTemplate -addControl \"center\";\n"
" editorTemplate -addControl \"inertiaRow1\";\n"
" editorTemplate -addControl \"inertiaRow2\";\n"
" editorTemplate -addControl \"inertiaRow3\";\n"
" editorTemplate -endScrollLayout;\n"
"}\n";
MStatus aeStatus = MGlobal::executeCommand(aeTemplateScript, true, false);
if (!aeStatus) {
MGlobal::displayWarning("Failed to register AEnifBhkRigidBodyTemplate - Bhk Flags will show in Maya's default one-column layout instead of 2 columns.");
}
}
status = plugin.registerNode("nifBhkShape", NifBhkShape::id,
NifBhkShape::creator, NifBhkShape::initialize);
if (!status) {
status.perror("registerNode nifBhkShape failed");
MGlobal::displayError(MString("registerNode nifBhkShape failed: ") + status.errorString());
}
else {
MGlobal::displayInfo("registerNode nifBhkShape succeeded.");
}
// AETemplate for nifBhkShape - just suppresses the standard
// Caching/Frozen/Node State rows (same reasoning as nifBhkRigidBody:
// compute() does nothing, so there's nothing for these to apply to) and
// lists the node's two real attributes explicitly so the page reads as
// one unified flow instead of splitting into a separate Extra Attributes
// section.
{
MString aeShapeTemplateScript =
"global proc AEnifBhkShapeTemplate(string $nodeName) {\n"
" editorTemplate -suppress \"caching\";\n"
" editorTemplate -suppress \"frozen\";\n"
" editorTemplate -suppress \"nodeState\";\n"
" editorTemplate -beginScrollLayout;\n"
" editorTemplate -addControl \"material\";\n"
" editorTemplate -addControl \"materialOverriddenByList\";\n"
" editorTemplate -addControl \"radius\";\n"
" editorTemplate -endScrollLayout;\n"
"}\n";
MStatus aeShapeStatus = MGlobal::executeCommand(aeShapeTemplateScript, true, false);
if (!aeShapeStatus) {
MGlobal::displayWarning("Failed to register AEnifBhkShapeTemplate.");
}
}
status = plugin.registerNode("bsLightningShader", BSLightningShader::id, BSLightningShader::creator, BSLightningShader::initialize, MPxNode::kDependNode);
if (!status) {
status.perror("registerBSLightningShader");
return status;
}
gNifDragAndDropCallback = new NifDragAndDropCallback();
MStatus dropStatus = MExternalDropCallback::addCallback(gNifDragAndDropCallback);
if (!dropStatus) {
dropStatus.perror("MExternalDropCallback::addCallback");
MGlobal::displayWarning("Failed to register NIF/KF drag-and-drop support.");
delete gNifDragAndDropCallback;
gNifDragAndDropCallback = NULL;
}
MGlobal::executeCommand("nifTranslatorMenuCreate");
return status;
}
//--uninitializePlugin--//
MStatus uninitializePlugin(MObject obj)
{
MStatus status;
MFnPlugin plugin(obj);
if (gNifDragAndDropCallback) {
MExternalDropCallback::removeCallback(gNifDragAndDropCallback);
delete gNifDragAndDropCallback;
gNifDragAndDropCallback = NULL;
}
status = plugin.deregisterFileTranslator(TRANSLATOR_NAME);
if (!status) {
status.perror("deregisterFileTranslator");
return status;
}
status = plugin.deregisterNode(NifDismemberPartition::id);
if (!status) {
status.perror("deregisterFileTranslator");
return status;
}
status = plugin.deregisterNode(NifBhkRigidBody::id);
if (!status) {
status.perror("deregisterNode nifBhkRigidBody failed");
}
status = plugin.deregisterNode(NifBhkShape::id);
if (!status) {
status.perror("deregisterNode nifBhkShape failed");
}
MGlobal::executeCommand("nifTranslatorMenuRemove");
if (!status) {
status.perror("deregisterNifDismemberPartition");
return status;
}
return status;
}
//--NifTranslator::reader--//
MStatus NifTranslator::reader(const MFileObject& file, const MString& optionsString, MPxFileTranslator::FileAccessMode mode) {
NifTranslatorDataRef translator_data(new NifTranslatorData());
NifTranslatorOptionsRef translator_options(new NifTranslatorOptions());
NifTranslatorUtilsRef translator_utils(new NifTranslatorUtils(translator_data, translator_options));
translator_options->ParseOptionsString(optionsString);
const char* logPath = "C:\\Users\\sauron\\Documents\\maya\\2025\\scripts\\nifTranslator_debug.log";
std::string filename = file.fullName().asChar();
{
std::ofstream log(logPath, std::ios::out | std::ios::app);
if (log.is_open()) {
std::time_t now = std::time(nullptr);
log << "[NifTranslator::reader] time=" << std::put_time(std::localtime(&now), "%F %T") << std::endl;
log << "[NifTranslator::reader] file=" << filename << std::endl;
}
}
if (NiflyImporter::ShouldUseNifly(filename)) {
{
std::ofstream log(logPath, std::ios::out | std::ios::app);
if (log.is_open()) {
log << "[NifTranslator::reader] Using NiflyImporter for modern NIF" << std::endl;
}
}
NiflyImportOptions niflyOptions;
niflyOptions.importScale = translator_options->importScale;
niflyOptions.texturePath = translator_options->texturePath;
niflyOptions.importFileDir = file.rawPath().asChar();
niflyOptions.useNameMangling = translator_options->useNameMangling;
niflyOptions.importNormalizedWeights = translator_options->importNormalizedWeights;
NiflyImporter niflyImporter;
niflyImporter.SetOptions(niflyOptions);
MStatus status = niflyImporter.Import(file);
if (status == MStatus::kSuccess) {
return status;
}
std::string error = niflyImporter.GetLastError();
{
std::ofstream log(logPath, std::ios::out | std::ios::app);
if (log.is_open()) {
log << "[NifTranslator::reader] NiflyImporter failed: " << error << std::endl;
}
}
MGlobal::displayError(MString("NIF import failed: ") + error.c_str());
return MStatus::kFailure;
}
{
std::ofstream log(logPath, std::ios::out | std::ios::app);
if (log.is_open()) {
log << "[NifTranslator::reader] Using niflib for legacy NIF" << std::endl;
}
}
NifImportingFixtureRef importer;
ImportType import_type = ImportType::Default;
try {
Header file_header = ReadHeader(filename.c_str());
vector<string> block_types = file_header.getBlockTypes();
vector<unsigned short> block_types_index = file_header.getBlockTypeIndex();
if (block_types[block_types_index[0]] == NiControllerSequence::TYPE.GetTypeName()) {
import_type = ImportType::AnimationKF;
}
else if (block_types[block_types_index[0]] == BSFadeNode::TYPE.GetTypeName() ||
file_header.getUserVersion() == 12 ||
(file_header.getUserVersion() == 11 && file_header.getUserVersion2() == 57) ||
(file_header.getUserVersion() == 11 && file_header.getUserVersion2() == 34)) {
import_type = ImportType::SkyrimFallout;
}
else {
for (size_t i = 0; i < block_types.size(); i++) {
if (block_types[i] == BSDismemberSkinInstance::TYPE.GetTypeName() || block_types[i] == BSShaderTextureSet::TYPE.GetTypeName()) {
import_type = ImportType::SkyrimFallout;
}
}
}
{
std::ofstream log(logPath, std::ios::out | std::ios::app);
if (log.is_open()) {
log << "[NifTranslator::reader] importType=" << static_cast<int>(import_type) << std::endl;
}
}
if (import_type == ImportType::AnimationKF) {
importer = new NifKFImportingFixture(translator_options, translator_data, translator_utils);
}
else if (import_type == ImportType::SkyrimFallout) {
importer = new NifSkyrimImportingFixture(translator_options, translator_data, translator_utils);
}
else if (import_type == ImportType::Default) {
importer = new NifDefaultImportingFixture(translator_data, translator_options, translator_utils);
}
return importer->ReadNodes(file);
}
catch (const std::exception& e) {
std::ofstream log(logPath, std::ios::out | std::ios::app);
if (log.is_open()) {
log << "[NifTranslator::reader] exception: " << e.what() << std::endl;
}
MGlobal::displayError(MString("NIF import failed: ") + e.what());
return MStatus::kFailure;
}
catch (...) {
std::ofstream log(logPath, std::ios::out | std::ios::app);
if (log.is_open()) {
log << "[NifTranslator::reader] unknown exception." << std::endl;
}
MGlobal::displayError("NIF import failed: unknown exception.");
return MStatus::kFailure;
}
}
//--NifTranslator::writer--//
MStatus NifTranslator::writer(const MFileObject& file, const MString& optionsString, MPxFileTranslator::FileAccessMode mode) {
NifTranslatorOptionsRef translator_options(new NifTranslatorOptions());
NifTranslatorDataRef translator_data(new NifTranslatorData());
NifTranslatorUtilsRef translator_utils(new NifTranslatorUtils(translator_data, translator_options));
translator_options->ParseOptionsString(optionsString);
NifExportingFixtureRef exporting_fixture;
string export_type = "geometry";
if (translator_options->exportType.length() > 1) {
export_type = translator_options->exportType;
}
currentExportType = export_type;
auto resolvedName = file.resolvedName();
auto resolvedFileName = file.resolvedFullName();
auto rawName = file.rawName();
auto rawFullName = file.rawFullName();
MGlobal::displayInfo(resolvedName);
MGlobal::displayInfo(resolvedFileName);
MGlobal::displayInfo(rawName);
MGlobal::displayInfo(rawFullName);
if (export_type == "geometry" || export_type == "nifanimation") {
if (translator_options->exportMaterialType == "standardmaterial") {
exporting_fixture = new NifDefaultExportingFixture(translator_data, translator_options, translator_utils);
}
if (translator_options->exportMaterialType == "falloutmaterial") {
exporting_fixture = new NifFalloutExportingFixture(
translator_data, translator_options, translator_utils);
}
if (translator_options->exportMaterialType == "skyrimmaterial") {
NiflyExportOptions niflyOptions;
niflyOptions.exportedShapes = translator_options->exportedShapes;
niflyOptions.exportUserVersion2 = translator_options->exportUserVersion2;
NiflyExportingFixture niflyExporter(niflyOptions);
return niflyExporter.WriteNodes(file);
}
}
if (export_type == "animation") {
exporting_fixture = new NifKFExportingFixture(translator_options, translator_data, translator_utils);
}
if (exporting_fixture != NULL) {
MStatus status = exporting_fixture->WriteNodes(file);
if (status != MStatus::kSuccess) {
return status;
}
if ((export_type == "geometry" || export_type == "nifanimation") && translator_options->exportMaterialType == "skyrimmaterial") {
std::string resaveError;
if (!ResaveWithNifly(file, resaveError)) {
MGlobal::displayWarning(MString(resaveError.c_str()));
}
else {
MGlobal::displayInfo("Nifly resave completed.");
}
}
return status;
}
return MStatus::kFailure;
}