ModelImporter.cpp 8.8 KB

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