ModelImporter.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include <Atomic/Core/ProcessUtils.h>
  2. #include <Atomic/IO/Log.h>
  3. #include <Atomic/IO/File.h>
  4. #include <Atomic/IO/FileSystem.h>
  5. #include <Atomic/Scene/Node.h>
  6. #include <Atomic/Atomic3D/AnimationController.h>
  7. #include <Atomic/Atomic3D/Animation.h>
  8. #include <Atomic/Resource/ResourceCache.h>
  9. #include "../Import/OpenAssetImporter.h"
  10. #include "Asset.h"
  11. #include "AssetDatabase.h"
  12. #include "ModelImporter.h"
  13. namespace ToolCore
  14. {
  15. /// Node + Model (static or animated)
  16. ModelImporter::ModelImporter(Context* context, Asset *asset) : AssetImporter(context),
  17. asset_(asset)
  18. {
  19. SetDefaults();
  20. }
  21. ModelImporter::~ModelImporter()
  22. {
  23. }
  24. void ModelImporter::SetDefaults()
  25. {
  26. AssetImporter::SetDefaults();
  27. scale_ = 1.0f;
  28. importAnimations_ = false;
  29. animationInfo_.Clear();
  30. }
  31. bool ModelImporter::ImportModel()
  32. {
  33. SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
  34. //importer->SetVerboseLog(true);
  35. importer->SetScale(scale_);
  36. importer->SetExportAnimations(false);
  37. importer->SetImportNode(importNode_);
  38. if (importer->Load(asset_->GetPath()))
  39. {
  40. importer->ExportModel(asset_->GetCachePath());
  41. return true;
  42. }
  43. return false;
  44. }
  45. bool ModelImporter::ImportAnimation(const String& filename, const String& name, float startTime, float endTime)
  46. {
  47. SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
  48. //importer->SetVerboseLog(true);
  49. importer->SetScale(scale_);
  50. importer->SetExportAnimations(true);
  51. importer->SetStartTime(startTime);
  52. importer->SetEndTime(endTime);
  53. if (importer->Load(filename))
  54. {
  55. importer->ExportModel(asset_->GetCachePath(), name, true);
  56. const Vector<OpenAssetImporter::AnimationInfo>& infos = importer->GetAnimationInfos();
  57. for (unsigned i = 0; i < infos.Size(); i++)
  58. {
  59. const OpenAssetImporter::AnimationInfo& info = infos.At(i);
  60. String pathName, fileName, extension;
  61. SplitPath(info.cacheFilename_, pathName, fileName, extension);
  62. ResourceCache* cache = GetSubsystem<ResourceCache>();
  63. AnimationController* controller = importNode_->GetComponent<AnimationController>();
  64. if (controller)
  65. {
  66. SharedPtr<Animation> animation = cache->GetTempResource<Animation>(fileName + extension);
  67. if (animation)
  68. controller->AddAnimationResource(animation);
  69. }
  70. LOGINFOF("Import Info: %s : %s", info.name_.CString(), fileName.CString());
  71. }
  72. return true;
  73. }
  74. return false;
  75. }
  76. bool ModelImporter::ImportAnimations()
  77. {
  78. if (!animationInfo_.Size())
  79. {
  80. if (!ImportAnimation(asset_->GetPath(), "RootAnim"))
  81. return false;
  82. }
  83. // embedded animations
  84. for (unsigned i = 0; i < animationInfo_.Size(); i++)
  85. {
  86. const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
  87. if (!ImportAnimation(asset_->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  88. return false;
  89. }
  90. // add @ animations
  91. FileSystem* fs = GetSubsystem<FileSystem>();
  92. String pathName, fileName, ext;
  93. SplitPath(asset_->GetPath(), pathName, fileName, ext);
  94. Vector<String> results;
  95. fs->ScanDir(results, pathName, ext, SCAN_FILES, false);
  96. for (unsigned i = 0; i < results.Size(); i++)
  97. {
  98. const String& result = results[i];
  99. if (result.Contains("@"))
  100. {
  101. Vector<String> components = GetFileName(result).Split('@');
  102. if (components.Size() == 2 && components[1].Length() && components[0] == fileName)
  103. {
  104. String animationName = components[1];
  105. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  106. Asset* asset = db->GetAssetByPath(pathName + result);
  107. assert(asset);
  108. assert(asset->GetImporter()->GetType() == ModelImporter::GetTypeStatic());
  109. ModelImporter* importer = (ModelImporter*) asset->GetImporter();
  110. if (!importer->animationInfo_.Size())
  111. {
  112. if (!ImportAnimation(asset->GetPath(), animationName))
  113. return false;
  114. }
  115. else
  116. {
  117. // embedded animations
  118. for (unsigned i = 0; i < importer->animationInfo_.Size(); i++)
  119. {
  120. const SharedPtr<AnimationImportInfo>& info = importer->animationInfo_[i];
  121. if (!ImportAnimation(asset->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  122. return false;
  123. }
  124. }
  125. }
  126. }
  127. }
  128. return true;
  129. }
  130. bool ModelImporter::Import(const String& guid)
  131. {
  132. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  133. Asset* asset = db->GetAssetByGUID(guid);
  134. if (!asset)
  135. return false;
  136. String modelAssetFilename = asset->GetPath();
  137. importNode_ = new Node(context_);
  138. // skip external animations, they will be brought in when importing their
  139. // corresponding model
  140. if (!modelAssetFilename.Contains("@"))
  141. {
  142. ImportModel();
  143. if (importAnimations_)
  144. {
  145. ImportAnimations();
  146. }
  147. }
  148. File outFile(context_);
  149. if (!outFile.Open(asset_->GetCachePath(), FILE_WRITE))
  150. ErrorExit("Could not open output file " + asset_->GetCachePath());
  151. importNode_->SaveXML(outFile);
  152. importNode_ = 0;
  153. return true;
  154. }
  155. bool ModelImporter::LoadSettingsInternal()
  156. {
  157. if (!AssetImporter::LoadSettingsInternal())
  158. return false;
  159. JSONValue import = jsonRoot_.GetChild("ModelImporter", JSON_OBJECT);
  160. SetDefaults();
  161. if (import.HasMember("scale"))
  162. scale_ = import.GetFloat("scale");
  163. if (import.HasMember("importAnimations"))
  164. importAnimations_ = import.GetBool("importAnimations");
  165. if (import.HasMember("animInfo"))
  166. {
  167. JSONValue animInfo = import.GetChild("animInfo");
  168. for (unsigned i = 0; i < animInfo.GetSize(); i++)
  169. {
  170. JSONValue anim = animInfo.GetChild(i);
  171. SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
  172. info->name_ = anim.GetString("name");
  173. info->SetStartTime(anim.GetFloat("startTime"));
  174. info->SetEndTime(anim.GetFloat("endTime"));
  175. animationInfo_.Push(info);
  176. }
  177. }
  178. return true;
  179. }
  180. bool ModelImporter::SaveSettingsInternal()
  181. {
  182. if (!AssetImporter::SaveSettingsInternal())
  183. return false;
  184. JSONValue save = jsonRoot_.CreateChild("ModelImporter");
  185. save.SetFloat("scale", scale_);
  186. save.SetBool("importAnimations", importAnimations_);
  187. JSONValue animInfo = save.CreateChild("animInfo", JSON_ARRAY);
  188. for (unsigned i = 0; i < animationInfo_.Size(); i++)
  189. {
  190. const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
  191. JSONValue jinfo = animInfo.CreateChild();
  192. jinfo.SetString("name", info->GetName());
  193. jinfo.SetFloat("startTime", info->GetStartTime());
  194. jinfo.SetFloat("endTime", info->GetEndTime());
  195. }
  196. return true;
  197. }
  198. }