ModelImporter.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/Atomic3D/Model.h>
  9. #include <Atomic/Resource/ResourceCache.h>
  10. #include "../Import/OpenAssetImporter.h"
  11. #include "Asset.h"
  12. #include "AssetDatabase.h"
  13. #include "ModelImporter.h"
  14. namespace ToolCore
  15. {
  16. /// Node + Model (static or animated)
  17. ModelImporter::ModelImporter(Context* context, Asset *asset) : AssetImporter(context, 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. else
  44. {
  45. asset_->PostImportError(importer->GetErrorMessage());
  46. }
  47. return false;
  48. }
  49. bool ModelImporter::ImportAnimation(const String& filename, const String& name, float startTime, float endTime)
  50. {
  51. SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
  52. //importer->SetVerboseLog(true);
  53. importer->SetScale(scale_);
  54. importer->SetExportAnimations(true);
  55. importer->SetStartTime(startTime);
  56. importer->SetEndTime(endTime);
  57. if (importer->Load(filename))
  58. {
  59. importer->ExportModel(asset_->GetCachePath(), name, true);
  60. const Vector<OpenAssetImporter::AnimationInfo>& infos = importer->GetAnimationInfos();
  61. for (unsigned i = 0; i < infos.Size(); i++)
  62. {
  63. const OpenAssetImporter::AnimationInfo& info = infos.At(i);
  64. String pathName, fileName, extension;
  65. SplitPath(info.cacheFilename_, pathName, fileName, extension);
  66. ResourceCache* cache = GetSubsystem<ResourceCache>();
  67. AnimationController* controller = importNode_->GetComponent<AnimationController>();
  68. if (controller)
  69. {
  70. SharedPtr<Animation> animation = cache->GetTempResource<Animation>(fileName + extension);
  71. if (animation)
  72. controller->AddAnimationResource(animation);
  73. }
  74. LOGINFOF("Import Info: %s : %s", info.name_.CString(), fileName.CString());
  75. }
  76. return true;
  77. }
  78. return false;
  79. }
  80. bool ModelImporter::ImportAnimations()
  81. {
  82. if (!animationInfo_.Size())
  83. {
  84. if (!ImportAnimation(asset_->GetPath(), "RootAnim"))
  85. return false;
  86. }
  87. // embedded animations
  88. for (unsigned i = 0; i < animationInfo_.Size(); i++)
  89. {
  90. const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
  91. if (!ImportAnimation(asset_->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  92. return false;
  93. }
  94. // add @ animations
  95. FileSystem* fs = GetSubsystem<FileSystem>();
  96. String pathName, fileName, ext;
  97. SplitPath(asset_->GetPath(), pathName, fileName, ext);
  98. Vector<String> results;
  99. fs->ScanDir(results, pathName, ext, SCAN_FILES, false);
  100. for (unsigned i = 0; i < results.Size(); i++)
  101. {
  102. const String& result = results[i];
  103. if (result.Contains("@"))
  104. {
  105. Vector<String> components = GetFileName(result).Split('@');
  106. if (components.Size() == 2 && components[1].Length() && components[0] == fileName)
  107. {
  108. String animationName = components[1];
  109. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  110. Asset* asset = db->GetAssetByPath(pathName + result);
  111. assert(asset);
  112. assert(asset->GetImporter()->GetType() == ModelImporter::GetTypeStatic());
  113. ModelImporter* importer = (ModelImporter*) asset->GetImporter();
  114. if (!importer->animationInfo_.Size())
  115. {
  116. if (!ImportAnimation(asset->GetPath(), animationName))
  117. return false;
  118. }
  119. else
  120. {
  121. // embedded animations
  122. for (unsigned i = 0; i < importer->animationInfo_.Size(); i++)
  123. {
  124. const SharedPtr<AnimationImportInfo>& info = importer->animationInfo_[i];
  125. if (!ImportAnimation(asset->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  126. return false;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. return true;
  133. }
  134. bool ModelImporter::Import()
  135. {
  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. unsigned ModelImporter::GetAnimationCount()
  156. {
  157. return animationInfo_.Size();
  158. }
  159. void ModelImporter::SetAnimationCount(unsigned count)
  160. {
  161. if (animationInfo_.Size() >= count)
  162. {
  163. animationInfo_.Resize(count);
  164. }
  165. else
  166. {
  167. for (unsigned i = animationInfo_.Size(); i < count; i++)
  168. {
  169. SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
  170. animationInfo_.Push(info);
  171. }
  172. }
  173. }
  174. bool ModelImporter::LoadSettingsInternal()
  175. {
  176. if (!AssetImporter::LoadSettingsInternal())
  177. return false;
  178. JSONValue import = jsonRoot_.GetChild("ModelImporter", JSON_OBJECT);
  179. SetDefaults();
  180. if (import.HasMember("scale"))
  181. scale_ = import.GetFloat("scale");
  182. if (import.HasMember("importAnimations"))
  183. importAnimations_ = import.GetBool("importAnimations");
  184. if (import.HasMember("animInfo"))
  185. {
  186. JSONValue animInfo = import.GetChild("animInfo");
  187. for (unsigned i = 0; i < animInfo.GetSize(); i++)
  188. {
  189. JSONValue anim = animInfo.GetChild(i);
  190. SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
  191. info->name_ = anim.GetString("name");
  192. info->SetStartTime(anim.GetFloat("startTime"));
  193. info->SetEndTime(anim.GetFloat("endTime"));
  194. animationInfo_.Push(info);
  195. }
  196. }
  197. return true;
  198. }
  199. bool ModelImporter::SaveSettingsInternal()
  200. {
  201. if (!AssetImporter::SaveSettingsInternal())
  202. return false;
  203. JSONValue save = jsonRoot_.CreateChild("ModelImporter");
  204. save.SetFloat("scale", scale_);
  205. save.SetBool("importAnimations", importAnimations_);
  206. JSONValue animInfo = save.CreateChild("animInfo", JSON_ARRAY);
  207. for (unsigned i = 0; i < animationInfo_.Size(); i++)
  208. {
  209. const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
  210. JSONValue jinfo = animInfo.CreateChild();
  211. jinfo.SetString("name", info->GetName());
  212. jinfo.SetFloat("startTime", info->GetStartTime());
  213. jinfo.SetFloat("endTime", info->GetEndTime());
  214. }
  215. return true;
  216. }
  217. Resource* ModelImporter::GetResource()
  218. {
  219. ResourceCache* cache = GetSubsystem<ResourceCache>();
  220. Model* model = cache->GetResource<Model>(asset_->GetCachePath() + ".mdl");
  221. return model;
  222. }
  223. }