From 37ead11cf97d503dad9be54ffedb62f0221d517b Mon Sep 17 00:00:00 2001 From: Simon Walton Date: Mon, 7 May 2018 18:18:16 -0700 Subject: [PATCH] Modify FBX reader to allow for multiple empty node Some baked files can have spurious empty nodes in them; this was causing chunks of the FBX file to be missed. Also more tweaks for FBX to JSON conversion. https://highfidelity.manuscript.com/f/cases/14329/ --- libraries/fbx/src/FBXReader_Node.cpp | 5 +--- libraries/fbx/src/FBXToJSON.cpp | 35 ++++++++++++++++++++++++---- libraries/fbx/src/FBXToJSON.h | 4 +++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/libraries/fbx/src/FBXReader_Node.cpp b/libraries/fbx/src/FBXReader_Node.cpp index c4454421b6..9375dc7b56 100644 --- a/libraries/fbx/src/FBXReader_Node.cpp +++ b/libraries/fbx/src/FBXReader_Node.cpp @@ -214,10 +214,7 @@ FBXNode parseBinaryFBXNode(QDataStream& in, int& position, bool has64BitPosition while (endOffset > position) { FBXNode child = parseBinaryFBXNode(in, position, has64BitPositions); - if (child.name.isNull()) { - return node; - - } else { + if (!child.name.isNull()) { node.children.append(child); } } diff --git a/libraries/fbx/src/FBXToJSON.cpp b/libraries/fbx/src/FBXToJSON.cpp index 40178c7120..2994de0f02 100644 --- a/libraries/fbx/src/FBXToJSON.cpp +++ b/libraries/fbx/src/FBXToJSON.cpp @@ -2,7 +2,7 @@ // FBXToJSON.cpp // libraries/fbx/src // -// Created by Simon Walton on 5/4/2013. +// Created by Simon Walton on 5/4/2018. // Copyright 2018 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. @@ -12,6 +12,8 @@ #include "FBXToJSON.h" #include "FBX.h" +using std::string; + template inline FBXToJSON& FBXToJSON::operator<<(QVector& arrayProp) { *this << "["; @@ -31,10 +33,12 @@ inline FBXToJSON& FBXToJSON::operator<<(QVector& arrayProp) { } FBXToJSON& FBXToJSON::operator<<(const FBXNode& fbxNode) { - using std::string; - string nodeName(fbxNode.name); - if (nodeName.empty()) nodeName = "nodename"; + if (nodeName.empty()) { + nodeName = "node"; + } else { + nodeName = stringEscape(nodeName); + } *this << string(_indentLevel * 4, ' ') << '"' << nodeName << "\": {\n"; @@ -55,7 +59,7 @@ FBXToJSON& FBXToJSON::operator<<(const FBXNode& fbxNode) { case QMetaType::QString: case QMetaType::QByteArray: - *this << '"' << prop.toString().toStdString() << '"'; + *this << '"' << stringEscape(prop.toByteArray().toStdString()) << '"'; break; default: @@ -87,3 +91,24 @@ FBXToJSON& FBXToJSON::operator<<(const FBXNode& fbxNode) { --_indentLevel; return *this; } + +string FBXToJSON::stringEscape(const string& in) { + string out; + out.reserve(in.length()); + + for (unsigned char inChar: in) { + if (inChar == '"') { + out.append(R"(\")"); + } + else if (inChar == '\\') { + out.append(R"(\\)"); + } + else if (inChar < 0x20 || inChar == 0x7f) { + char h[5]; + sprintf(h, "\\x%02x", inChar); + out.append(h); + } + else out.append(1, inChar); + } + return out; +} diff --git a/libraries/fbx/src/FBXToJSON.h b/libraries/fbx/src/FBXToJSON.h index 0961144e9f..85feac6bff 100644 --- a/libraries/fbx/src/FBXToJSON.h +++ b/libraries/fbx/src/FBXToJSON.h @@ -2,7 +2,7 @@ // FBXToJSON.h // libraries/fbx/src // -// Created by Simon Walton on 5/4/2013. +// Created by Simon Walton on 5/4/2018. // Copyright 2018 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. @@ -13,6 +13,7 @@ #define hifi_FBXToJSON_h #include +#include // Forward declarations. class FBXNode; @@ -24,6 +25,7 @@ public: private: template FBXToJSON& operator<<(QVector& arrayProp); + static std::string stringEscape(const std::string& in); int _indentLevel { 0 }; };