Repository navigation
Expand file tree
/
Copy pathInjector.cpp
More file actions
356 lines (292 loc) · 14 KB
/
Copy pathInjector.cpp
File metadata and controls
356 lines (292 loc) · 14 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
#include <fbxsdk.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include <cstring>
// ============================================================================
// Helpers
// ============================================================================
bool ReadString(std::ifstream& in, std::string& str) {
uint32_t len;
in.read(reinterpret_cast<char*>(&len), sizeof(len));
if (!in) return false;
str.resize(len);
if (len > 0) {
in.read(&str[0], len);
}
return !!in;
}
bool ReadIntArray(std::ifstream& in, std::vector<int>& arr) {
uint32_t count;
in.read(reinterpret_cast<char*>(&count), sizeof(count));
if (!in) return false;
arr.resize(count);
if (count > 0) {
in.read(reinterpret_cast<char*>(arr.data()), count * sizeof(int));
}
return !!in;
}
bool ReadValue(std::ifstream& in, const std::string& typeName, std::vector<char>& data) {
if (typeName == "Int" || typeName == "Integer") {
data.resize(sizeof(int));
in.read(data.data(), sizeof(int));
}
else if (typeName == "Blob" || typeName == "KString" || typeName == "String") {
uint32_t size;
in.read(reinterpret_cast<char*>(&size), sizeof(size));
if (!in) return false;
data.resize(size);
if (size > 0) {
in.read(data.data(), size);
}
}
else {
std::cerr << "\nError: Unknown property type '" << typeName << "'" << std::endl;
return false;
}
return !!in;
}
// ============================================================================
// date Structure
// ============================================================================
struct GeometryData {
std::map<std::string, std::pair<std::string, std::vector<char>>> properties;
std::string userDataName;
std::vector<int> islandGroupIDs;
bool hasIslandData = false;
};
// ============================================================================
// Load Data form file
// ============================================================================
void LoadAllDataFromFile(const std::string& dataFilePath,
std::map<std::string, std::pair<std::string, std::vector<char>>>& documentProperties,
std::map<std::string, GeometryData>& geometryData) {
std::ifstream dataFile(dataFilePath, std::ios::binary);
if (!dataFile.is_open()) {
std::cerr << "Error: Could not open data file.\n";
return;
}
std::cout << "\n=== Loading data from file ===" << std::endl;
while (dataFile) {
char marker;
dataFile.read(&marker, sizeof(marker));
if (dataFile.eof()) break;
if (marker == 'G') {
std::string objectName, propName, typeName;
ReadString(dataFile, objectName);
ReadString(dataFile, propName);
ReadString(dataFile, typeName);
std::vector<char> data;
if (!ReadValue(dataFile, typeName, data)) break;
documentProperties[propName] = { typeName, data };
std::cout << " [Document] Property '" << propName << "' (" << data.size() << " bytes)" << std::endl;
}
else if (marker == 'M') {
std::string meshName, propName, typeName;
ReadString(dataFile, meshName);
ReadString(dataFile, propName);
ReadString(dataFile, typeName);
std::vector<char> data;
if (!ReadValue(dataFile, typeName, data)) break;
geometryData[meshName].properties[propName] = { typeName, data };
std::cout << " [" << meshName << "] Property '" << propName << "' (" << data.size() << " bytes)" << std::endl;
}
else if (marker == 'I') {
std::string meshName, userDataName;
ReadString(dataFile, meshName);
ReadString(dataFile, userDataName);
std::vector<int> groupIDs;
if (!ReadIntArray(dataFile, groupIDs)) break;
geometryData[meshName].userDataName = userDataName;
geometryData[meshName].islandGroupIDs = groupIDs;
geometryData[meshName].hasIslandData = true;
std::cout << " [" << meshName << "] UserData '" << userDataName << "' (" << groupIDs.size() << " IDs)" << std::endl;
}
}
dataFile.close();
}
// ============================================================================
// Inject FbxDocument
// ============================================================================
void InjectDocumentRizomData(FbxScene* scene,
std::map<std::string, std::pair<std::string, std::vector<char>>>& properties) {
std::cout << "\n=== Injecting RizomUV data into FbxDocument ===" << std::endl;
FbxDocument* rootDocument = scene->GetRootDocument();
if (!rootDocument) return;
rootDocument->SetName("Scene");
FbxProperty rizomProp = FbxProperty::Create(rootDocument, FbxIntDT, "RizomUV");
if (rizomProp.IsValid() && properties.count("RizomUV")) {
rizomProp.Set(*(reinterpret_cast<int*>(properties["RizomUV"].second.data())));
std::cout << " Created: RizomUV (int)" << std::endl;
}
FbxProperty sceneProp = FbxProperty::Create(rizomProp, FbxBlobDT, "Scene");
if (sceneProp.IsValid() && properties.count("Scene")) {
auto& data = properties["Scene"].second;
sceneProp.Set(FbxBlob(data.data(), data.size()));
std::cout << " Created: RizomUV->Scene (blob, " << data.size() << " bytes)" << std::endl;
}
FbxProperty uvSetsProp = FbxProperty::Create(rizomProp, FbxStringDT, "UVSets");
if (uvSetsProp.IsValid() && properties.count("UVSets")) {
auto& data = properties["UVSets"].second;
uvSetsProp.Set(FbxString(data.data(), data.size()));
std::cout << " Created: RizomUV->UVSets (string)" << std::endl;
}
FbxProperty uvMapProp = FbxProperty::Create(uvSetsProp, FbxStringDT, "UVMap");
if (uvMapProp.IsValid() && properties.count("UVMap")) {
auto& data = properties["UVMap"].second;
uvMapProp.Set(FbxString(data.data(), data.size()));
std::cout << " Created: RizomUV->UVSets->UVMap (string)" << std::endl;
}
FbxProperty rootGroupProp = FbxProperty::Create(uvMapProp, FbxBlobDT, "RootGroup");
if (rootGroupProp.IsValid() && properties.count("RootGroup")) {
auto& data = properties["RootGroup"].second;
rootGroupProp.Set(FbxBlob(data.data(), data.size()));
std::cout << " Created: RizomUV->UVSets->UVMap->RootGroup (blob, " << data.size() << " bytes)" << std::endl;
}
std::cout << " SUCCESS: Document property hierarchy created." << std::endl;
}
// ============================================================================
// Inject Geometry
// ============================================================================
void InjectGeometryRizomData(FbxScene* scene, std::map<std::string, GeometryData>& geometryData) {
std::cout << "\n=== Injecting RizomUV data into Geometries ===" << std::endl;
int nodeCount = scene->GetNodeCount();
for (int i = 0; i < nodeCount; i++) {
FbxNode* node = scene->GetNode(i);
FbxNodeAttribute* attr = node->GetNodeAttribute();
if (attr && attr->GetAttributeType() == FbxNodeAttribute::eMesh) {
FbxMesh* mesh = node->GetMesh();
std::string nodeName = node->GetName();
std::string meshName = mesh->GetName();
std::cout << "\nChecking node: " << nodeName << " (mesh: " << meshName << ")" << std::endl;
std::string lookupName;
GeometryData* geoData = nullptr;
if (geometryData.count(nodeName)) {
lookupName = nodeName;
geoData = &geometryData[nodeName];
std::cout << " OK Found data for NODE name: " << nodeName << std::endl;
}
else if (geometryData.count(meshName)) {
lookupName = meshName;
geoData = &geometryData[meshName];
std::cout << " OK Found data for MESH name: " << meshName << std::endl;
}
else {
std::cout << " -- No data found for '" << nodeName << "' or '" << meshName << "'" << std::endl;
continue;
}
std::cout << "Processing geometry: " << lookupName << std::endl;
// === 1: RizomUV Properties ===
if (geoData->properties.count("RizomUV")) {
FbxProperty rizomProp = FbxProperty::Create(mesh, FbxIntDT, "RizomUV");
if (rizomProp.IsValid()) {
auto& data = geoData->properties["RizomUV"].second;
rizomProp.Set(*(reinterpret_cast<int*>(data.data())));
std::cout << " Created: RizomUV (int)" << std::endl;
}
}
if (geoData->properties.count("RizomUVUVSets")) {
FbxProperty uvSetsProp = FbxProperty::Create(mesh, FbxStringDT, "RizomUVUVSets");
if (uvSetsProp.IsValid()) {
auto& data = geoData->properties["RizomUVUVSets"].second;
uvSetsProp.Set(FbxString(data.data(), data.size()));
std::cout << " Created: RizomUVUVSets (string)" << std::endl;
}
}
// === 2: Island Group IDs ===
if (geoData->hasIslandData && !geoData->islandGroupIDs.empty()) {
FbxLayer* layer = mesh->GetLayer(0);
if (!layer) {
mesh->CreateLayer();
layer = mesh->GetLayer(0);
}
std::string userDataName = geoData->userDataName.empty() ?
"RizomUVUVMapIslandGroupIDs" :
geoData->userDataName;
std::cout << " Creating UserData: '" << userDataName << "'" << std::endl;
FbxArray<FbxDataType> dataTypes;
dataTypes.Add(FbxIntDT);
FbxArray<const char*> dataNames;
dataNames.Add("IslandGroupID");
FbxLayerElementUserData* userData = FbxLayerElementUserData::Create(
mesh,
userDataName.c_str(),
0,
dataTypes,
dataNames
);
if (userData) {
userData->SetMappingMode(FbxLayerElement::eByPolygon);
userData->SetReferenceMode(FbxLayerElement::eDirect);
userData->ResizeAllDirectArrays(geoData->islandGroupIDs.size());
bool getStatus = false;
FbxLayerElementArrayTemplate<void*>* voidArray = userData->GetDirectArrayVoid(0, &getStatus);
if (getStatus && voidArray) {
int* dataPtr = (int*)voidArray->GetLocked(FbxLayerElementArray::eWriteLock);
if (dataPtr) {
for (size_t j = 0; j < geoData->islandGroupIDs.size(); j++) {
dataPtr[j] = geoData->islandGroupIDs[j];
}
voidArray->Release((void**)&dataPtr);
std::cout << " SAVED " << geoData->islandGroupIDs.size() << " Island Group IDs" << std::endl;
}
}
layer->SetUserData(userData);
}
}
}
}
std::cout << "\n SUCCESS: All geometry data injected." << std::endl;
}
int main(int argc, char** argv) {
if (argc < 4) {
std::cout << "Usage: program.exe <target.fbx> <data.dat> <output.fbx>\n";
return 1;
}
const char* targetFBX = argv[1];
const char* dataFile = argv[2];
const char* outputFBX = argv[3];
FbxManager* manager = FbxManager::Create();
FbxIOSettings* ios = FbxIOSettings::Create(manager, IOSROOT);
manager->SetIOSettings(ios);
FbxImporter* importer = FbxImporter::Create(manager, "");
if (!importer->Initialize(targetFBX, -1, manager->GetIOSettings())) {
std::cerr << "Could not open the target FBX file.\n";
return 1;
}
FbxScene* scene = FbxScene::Create(manager, "Scene");
importer->Import(scene);
importer->Destroy();
std::map<std::string, std::pair<std::string, std::vector<char>>> documentProperties;
std::map<std::string, GeometryData> geometryData;
LoadAllDataFromFile(dataFile, documentProperties, geometryData);
InjectDocumentRizomData(scene, documentProperties);
InjectGeometryRizomData(scene, geometryData);
std::cout << "\n=== CONVERTING: ASCII -> BINARY ===" << std::endl;
std::cout << "Saving to: " << outputFBX << std::endl;
FbxExporter* exporter = FbxExporter::Create(manager, "");
int fileFormat = manager->GetIOPluginRegistry()->FindWriterIDByDescription("FBX binary (*.fbx)");
if (fileFormat == -1) {
std::cerr << "ERROR: FBX binary format not found! Falling back to ASCII." << std::endl;
fileFormat = manager->GetIOPluginRegistry()->FindWriterIDByDescription("FBX ascii (*.fbx)");
}
if (!exporter->Initialize(outputFBX, fileFormat, manager->GetIOSettings())) {
std::cerr << "Error during exporter initialization.\n";
return 1;
}
if (exporter->Export(scene)) {
std::cout << "EXPORT OK: Binary FBX created successfully" << std::endl;
}
else {
std::cerr << "ERROR: Export failed!" << std::endl;
}
exporter->Destroy();
manager->Destroy();
std::cout << "\n============================================" << std::endl;
std::cout << "SUCCESS! Complete injection + conversion finished." << std::endl;
std::cout << "Output: BINARY FBX (ready for Blender/RizomUV)" << std::endl;
std::cout << "============================================" << std::endl;
return 0;
}