osgDB::ReaderWriter::WriteResult ReaderWriterFBX::writeNode( const osg::Node& node, const std::string& filename, const Options* options) const { try { std::string ext = osgDB::getLowerCaseFileExtension(filename); if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; osg::ref_ptr<Options> localOptions = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; localOptions->getDatabasePathList().push_front(osgDB::getFilePath(filename)); FbxManager* pSdkManager = FbxManager::Create(); if (!pSdkManager) { return WriteResult::ERROR_IN_WRITING_FILE; } CleanUpFbx cleanUpFbx(pSdkManager); pSdkManager->SetIOSettings(FbxIOSettings::Create(pSdkManager, IOSROOT)); bool useFbxRoot = false; if (options) { std::istringstream iss(options->getOptionString()); std::string opt; while (iss >> opt) { if (opt == "Embedded") { pSdkManager->GetIOSettings()->SetBoolProp(EXP_FBX_EMBEDDED, true); } else if (opt == "UseFbxRoot") { useFbxRoot = true; } } } FbxScene* pScene = FbxScene::Create(pSdkManager, ""); pluginfbx::WriterNodeVisitor writerNodeVisitor(pScene, pSdkManager, filename, options, osgDB::getFilePath(node.getName().empty() ? filename : node.getName())); if (useFbxRoot && isBasicRootNode(node)) { // If root node is a simple group, put all elements under the FBX root const osg::Group * osgGroup = node.asGroup(); for (unsigned int child = 0; child < osgGroup->getNumChildren(); ++child) { const_cast<osg::Node *>(osgGroup->getChild(child))->accept(writerNodeVisitor); } } else { // Normal scene const_cast<osg::Node&>(node).accept(writerNodeVisitor); } FbxDocumentInfo* pDocInfo = pScene->GetDocumentInfo(); bool needNewDocInfo = pDocInfo != NULL; if (needNewDocInfo) { pDocInfo = FbxDocumentInfo::Create(pSdkManager, ""); } pDocInfo->LastSaved_ApplicationName.Set(FbxString("OpenSceneGraph")); pDocInfo->LastSaved_ApplicationVersion.Set(FbxString(osgGetVersion())); if (needNewDocInfo) { pScene->SetDocumentInfo(pDocInfo); } FbxExporter* lExporter = FbxExporter::Create(pSdkManager, ""); pScene->GetGlobalSettings().SetAxisSystem(FbxAxisSystem::eOpenGL); // Ensure the directory exists or else the FBX SDK will fail if (!osgDB::makeDirectoryForFile(filename)) { OSG_NOTICE << "Can't create directory for file '" << filename << "'. FBX SDK may fail creating the file." << std::endl; } // The FBX SDK interprets the filename as UTF-8 #ifdef OSG_USE_UTF8_FILENAME const std::string& utf8filename(filename); #else std::string utf8filename(osgDB::convertStringFromCurrentCodePageToUTF8(filename)); #endif if (!lExporter->Initialize(utf8filename.c_str())) { #if FBXSDK_VERSION_MAJOR < 2014 return std::string(lExporter->GetLastErrorString()); #else return std::string(lExporter->GetStatus().GetErrorString()); #endif } if (!lExporter->Export(pScene)) { #if FBXSDK_VERSION_MAJOR < 2014 return std::string(lExporter->GetLastErrorString()); #else return std::string(lExporter->GetStatus().GetErrorString()); #endif } return WriteResult::FILE_SAVED; } catch (const std::string& s) { return s; } catch (const char* s) { return std::string(s); } catch (...) { } return WriteResult::ERROR_IN_WRITING_FILE; }
osgDB::ReaderWriter::WriteResult ReaderWriterFBX::writeNode( const osg::Node& node, const std::string& filename, const Options* options) const { try { std::string ext = osgDB::getLowerCaseFileExtension(filename); if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; osg::ref_ptr<Options> localOptions = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; localOptions->getDatabasePathList().push_front(osgDB::getFilePath(filename)); FbxManager* pSdkManager = FbxManager::Create(); if (!pSdkManager) { return WriteResult::ERROR_IN_WRITING_FILE; } CleanUpFbx cleanUpFbx(pSdkManager); pSdkManager->SetIOSettings(FbxIOSettings::Create(pSdkManager, IOSROOT)); bool useFbxRoot = false; bool ascii(false); std::string exportVersion; if (options) { std::istringstream iss(options->getOptionString()); std::string opt; while (iss >> opt) { if (opt == "Embedded") { pSdkManager->GetIOSettings()->SetBoolProp(EXP_FBX_EMBEDDED, true); } else if (opt == "UseFbxRoot") { useFbxRoot = true; } else if (opt == "FBX-ASCII") { ascii = true; } else if (opt == "FBX-ExportVersion") { iss >> exportVersion; } } } FbxScene* pScene = FbxScene::Create(pSdkManager, ""); if (options) { if (options->getPluginData("FBX-AssetUnitMeter")) { float unit = *static_cast<const float*>(options->getPluginData("FBX-AssetUnitMeter")); FbxSystemUnit kFbxSystemUnit(unit*100); pScene->GetGlobalSettings().SetSystemUnit(kFbxSystemUnit); } } pluginfbx::WriterNodeVisitor writerNodeVisitor(pScene, pSdkManager, filename, options, osgDB::getFilePath(node.getName().empty() ? filename : node.getName())); if (useFbxRoot && isBasicRootNode(node)) { // If root node is a simple group, put all elements under the FBX root const osg::Group * osgGroup = node.asGroup(); for (unsigned int child = 0; child < osgGroup->getNumChildren(); ++child) { const_cast<osg::Node *>(osgGroup->getChild(child))->accept(writerNodeVisitor); } } else { // Normal scene const_cast<osg::Node&>(node).accept(writerNodeVisitor); } FbxDocumentInfo* pDocInfo = pScene->GetDocumentInfo(); bool needNewDocInfo = pDocInfo != NULL; if (needNewDocInfo) { pDocInfo = FbxDocumentInfo::Create(pSdkManager, ""); } pDocInfo->LastSaved_ApplicationName.Set(FbxString("OpenSceneGraph")); pDocInfo->LastSaved_ApplicationVersion.Set(FbxString(osgGetVersion())); if (needNewDocInfo) { pScene->SetDocumentInfo(pDocInfo); } FbxExporter* lExporter = FbxExporter::Create(pSdkManager, ""); pScene->GetGlobalSettings().SetAxisSystem(FbxAxisSystem::eOpenGL); // Ensure the directory exists or else the FBX SDK will fail if (!osgDB::makeDirectoryForFile(filename)) { OSG_NOTICE << "Can't create directory for file '" << filename << "'. FBX SDK may fail creating the file." << std::endl; } // The FBX SDK interprets the filename as UTF-8 #ifdef OSG_USE_UTF8_FILENAME const std::string& utf8filename(filename); #else std::string utf8filename(osgDB::convertStringFromCurrentCodePageToUTF8(filename)); #endif // Output format selection. Here we only handle "recent" FBX, binary or ASCII. // pSdkManager->GetIOPluginRegistry()->GetWriterFormatDescription() / GetReaderFormatCount() gives the following list: //FBX binary (*.fbx) //FBX ascii (*.fbx) //FBX encrypted (*.fbx) //FBX 6.0 binary (*.fbx) //FBX 6.0 ascii (*.fbx) //FBX 6.0 encrypted (*.fbx) //AutoCAD DXF (*.dxf) //Alias OBJ (*.obj) //Collada DAE (*.dae) //Biovision BVH (*.bvh) //Motion Analysis HTR (*.htr) //Motion Analysis TRC (*.trc) //Acclaim ASF (*.asf) int format = ascii ? pSdkManager->GetIOPluginRegistry()->FindWriterIDByDescription("FBX ascii (*.fbx)") : -1; // -1 = Default if (!lExporter->Initialize(utf8filename.c_str(), format)) { #if FBXSDK_VERSION_MAJOR < 2014 return std::string(lExporter->GetLastErrorString()); #else return std::string(lExporter->GetStatus().GetErrorString()); #endif } if (!exportVersion.empty() && !lExporter->SetFileExportVersion(FbxString(exportVersion.c_str()), FbxSceneRenamer::eNone)) { std::stringstream versionsStr; char const * const * versions = lExporter->GetCurrentWritableVersions(); if (versions) for(; *versions; ++versions) versionsStr << " " << *versions; OSG_WARN << "Can't set FBX export version to '" << exportVersion << "'. Using default. Available export versions are:" << versionsStr.str() << std::endl; } if (!lExporter->Export(pScene)) { #if FBXSDK_VERSION_MAJOR < 2014 return std::string(lExporter->GetLastErrorString()); #else return std::string(lExporter->GetStatus().GetErrorString()); #endif } return WriteResult::FILE_SAVED; }