ModelImporter.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. return false;
  43. }
  44. bool ModelImporter::ImportAnimation(const String& filename, const String& name, float startTime, float endTime)
  45. {
  46. SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
  47. //importer->SetVerboseLog(true);
  48. importer->SetScale(scale_);
  49. importer->SetExportAnimations(true);
  50. importer->SetStartTime(startTime);
  51. importer->SetEndTime(endTime);
  52. if (importer->Load(filename))
  53. {
  54. importer->ExportModel(asset_->GetCachePath(), name, true);
  55. const Vector<OpenAssetImporter::AnimationInfo>& infos = importer->GetAnimationInfos();
  56. for (unsigned i = 0; i < infos.Size(); i++)
  57. {
  58. const OpenAssetImporter::AnimationInfo& info = infos.At(i);
  59. String pathName, fileName, extension;
  60. SplitPath(info.cacheFilename_, pathName, fileName, extension);
  61. ResourceCache* cache = GetSubsystem<ResourceCache>();
  62. AnimationController* controller = importNode_->GetComponent<AnimationController>();
  63. if (controller)
  64. {
  65. SharedPtr<Animation> animation = cache->GetTempResource<Animation>(fileName + extension);
  66. if (animation)
  67. controller->AddAnimationResource(animation);
  68. }
  69. LOGINFOF("Import Info: %s : %s", info.name_.CString(), fileName.CString());
  70. }
  71. return true;
  72. }
  73. return false;
  74. }
  75. bool ModelImporter::ImportAnimations()
  76. {
  77. if (!animationInfo_.Size())
  78. {
  79. if (!ImportAnimation(asset_->GetPath(), "RootAnim"))
  80. return false;
  81. }
  82. // embedded animations
  83. for (unsigned i = 0; i < animationInfo_.Size(); i++)
  84. {
  85. const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
  86. if (!ImportAnimation(asset_->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  87. return false;
  88. }
  89. // add @ animations
  90. FileSystem* fs = GetSubsystem<FileSystem>();
  91. String pathName, fileName, ext;
  92. SplitPath(asset_->GetPath(), pathName, fileName, ext);
  93. Vector<String> results;
  94. fs->ScanDir(results, pathName, ext, SCAN_FILES, false);
  95. for (unsigned i = 0; i < results.Size(); i++)
  96. {
  97. const String& result = results[i];
  98. if (result.Contains("@"))
  99. {
  100. Vector<String> components = GetFileName(result).Split('@');
  101. if (components.Size() == 2 && components[1].Length() && components[0] == fileName)
  102. {
  103. String animationName = components[1];
  104. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  105. Asset* asset = db->GetAssetByPath(pathName + result);
  106. assert(asset);
  107. assert(asset->GetImporter()->GetType() == ModelImporter::GetTypeStatic());
  108. ModelImporter* importer = (ModelImporter*) asset->GetImporter();
  109. if (!importer->animationInfo_.Size())
  110. {
  111. if (!ImportAnimation(asset->GetPath(), animationName))
  112. return false;
  113. }
  114. else
  115. {
  116. // embedded animations
  117. for (unsigned i = 0; i < importer->animationInfo_.Size(); i++)
  118. {
  119. const SharedPtr<AnimationImportInfo>& info = importer->animationInfo_[i];
  120. if (!ImportAnimation(asset->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  121. return false;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. return true;
  128. }
  129. bool ModelImporter::Import(const String& guid)
  130. {
  131. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  132. Asset* asset = db->GetAssetByGUID(guid);
  133. if (!asset)
  134. return false;
  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. }