ModelImporter.cpp 7.5 KB

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