ModelImporter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/Core/ProcessUtils.h>
  8. #include <Atomic/IO/Log.h>
  9. #include <Atomic/IO/File.h>
  10. #include <Atomic/IO/FileSystem.h>
  11. #include <Atomic/Scene/Node.h>
  12. #include <Atomic/Atomic3D/AnimatedModel.h>
  13. #include <Atomic/Atomic3D/Animation.h>
  14. #include <Atomic/Atomic3D/AnimationController.h>
  15. #include <Atomic/Atomic3D/StaticModel.h>
  16. #include <Atomic/Atomic3D/Model.h>
  17. #include <Atomic/Resource/ResourceCache.h>
  18. #include <Atomic/Resource/XMLFile.h>
  19. #include "../Import/OpenAssetImporter.h"
  20. #include "Asset.h"
  21. #include "AssetDatabase.h"
  22. #include "ModelImporter.h"
  23. namespace ToolCore
  24. {
  25. /// Node + Model (static or animated)
  26. ModelImporter::ModelImporter(Context* context, Asset *asset) : AssetImporter(context, asset)
  27. {
  28. SetDefaults();
  29. }
  30. ModelImporter::~ModelImporter()
  31. {
  32. }
  33. void ModelImporter::SetDefaults()
  34. {
  35. AssetImporter::SetDefaults();
  36. scale_ = 1.0;
  37. importAnimations_ = false;
  38. animationInfo_.Clear();
  39. }
  40. bool ModelImporter::ImportModel()
  41. {
  42. LOGDEBUGF("Importing Model: %s", asset_->GetPath().CString());
  43. SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
  44. importer->SetVerboseLog(true);
  45. importer->SetScale(scale_);
  46. importer->SetExportAnimations(false);
  47. importer->SetImportNode(importNode_);
  48. if (importer->Load(asset_->GetPath()))
  49. {
  50. importer->ExportModel(asset_->GetCachePath());
  51. return true;
  52. }
  53. else
  54. {
  55. asset_->PostImportError(importer->GetErrorMessage());
  56. }
  57. return false;
  58. }
  59. bool ModelImporter::ImportAnimation(const String& filename, const String& name, float startTime, float endTime)
  60. {
  61. SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
  62. //importer->SetVerboseLog(true);
  63. importer->SetScale(scale_);
  64. importer->SetExportAnimations(true);
  65. importer->SetStartTime(startTime);
  66. importer->SetEndTime(endTime);
  67. if (importer->Load(filename))
  68. {
  69. importer->ExportModel(asset_->GetCachePath(), name, true);
  70. const Vector<OpenAssetImporter::AnimationInfo>& infos = importer->GetAnimationInfos();
  71. for (unsigned i = 0; i < infos.Size(); i++)
  72. {
  73. const OpenAssetImporter::AnimationInfo& info = infos.At(i);
  74. String pathName, fileName, extension;
  75. SplitPath(info.cacheFilename_, pathName, fileName, extension);
  76. ResourceCache* cache = GetSubsystem<ResourceCache>();
  77. AnimatedModel* animatedModel = importNode_->GetComponent<AnimatedModel>();
  78. AnimationController* controller = importNode_->GetComponent<AnimationController>();
  79. if (animatedModel && controller)
  80. {
  81. SharedPtr<Animation> animation = cache->GetTempResource<Animation>(fileName + extension);
  82. if (animation)
  83. controller->AddAnimationResource(animation);
  84. }
  85. LOGINFOF("Import Info: %s : %s", info.name_.CString(), fileName.CString());
  86. }
  87. return true;
  88. }
  89. return false;
  90. }
  91. bool ModelImporter::ImportAnimations()
  92. {
  93. if (!animationInfo_.Size())
  94. {
  95. if (!ImportAnimation(asset_->GetPath(), "RootAnim"))
  96. return false;
  97. }
  98. // embedded animations
  99. for (unsigned i = 0; i < animationInfo_.Size(); i++)
  100. {
  101. const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
  102. if (!ImportAnimation(asset_->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  103. return false;
  104. }
  105. // add @ animations
  106. FileSystem* fs = GetSubsystem<FileSystem>();
  107. String pathName, fileName, ext;
  108. SplitPath(asset_->GetPath(), pathName, fileName, ext);
  109. Vector<String> results;
  110. fs->ScanDir(results, pathName, ext, SCAN_FILES, false);
  111. for (unsigned i = 0; i < results.Size(); i++)
  112. {
  113. const String& result = results[i];
  114. if (result.Contains("@"))
  115. {
  116. Vector<String> components = GetFileName(result).Split('@');
  117. if (components.Size() == 2 && components[1].Length() && components[0] == fileName)
  118. {
  119. String animationName = components[1];
  120. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  121. Asset* asset = db->GetAssetByPath(pathName + result);
  122. assert(asset);
  123. assert(asset->GetImporter()->GetType() == ModelImporter::GetTypeStatic());
  124. ModelImporter* importer = (ModelImporter*) asset->GetImporter();
  125. if (!importer->animationInfo_.Size())
  126. {
  127. if (!ImportAnimation(asset->GetPath(), animationName))
  128. return false;
  129. }
  130. else
  131. {
  132. // embedded animations
  133. for (unsigned i = 0; i < importer->animationInfo_.Size(); i++)
  134. {
  135. const SharedPtr<AnimationImportInfo>& info = importer->animationInfo_[i];
  136. if (!ImportAnimation(asset->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  137. return false;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. return true;
  144. }
  145. bool ModelImporter::Import()
  146. {
  147. String ext = asset_->GetExtension();
  148. String modelAssetFilename = asset_->GetPath();
  149. importNode_ = new Node(context_);
  150. if (ext == ".mdl")
  151. {
  152. FileSystem* fs = GetSubsystem<FileSystem>();
  153. ResourceCache* cache = GetSubsystem<ResourceCache>();
  154. // mdl files are native file format that doesn't need to be converted
  155. // doesn't allow scale, animations legacy primarily for ToonTown
  156. if (!fs->Copy(asset_->GetPath(), asset_->GetCachePath() + ".mdl"))
  157. {
  158. importNode_= 0;
  159. return false;
  160. }
  161. Model* mdl = cache->GetResource<Model>( asset_->GetCachePath() + ".mdl");
  162. if (!mdl)
  163. {
  164. importNode_= 0;
  165. return false;
  166. }
  167. // Force a reload, though file watchers will catch this delayed and load again
  168. cache->ReloadResource(mdl);
  169. importNode_->CreateComponent<StaticModel>()->SetModel(mdl);
  170. }
  171. else
  172. {
  173. // skip external animations, they will be brought in when importing their
  174. // corresponding model
  175. if (!modelAssetFilename.Contains("@"))
  176. {
  177. ImportModel();
  178. if (importAnimations_)
  179. {
  180. ImportAnimations();
  181. }
  182. }
  183. }
  184. File outFile(context_);
  185. if (!outFile.Open(asset_->GetCachePath(), FILE_WRITE))
  186. ErrorExit("Could not open output file " + asset_->GetCachePath());
  187. importNode_->SaveXML(outFile);
  188. importNode_ = 0;
  189. return true;
  190. }
  191. unsigned ModelImporter::GetAnimationCount()
  192. {
  193. return animationInfo_.Size();
  194. }
  195. void ModelImporter::SetAnimationCount(unsigned count)
  196. {
  197. if (animationInfo_.Size() >= count)
  198. {
  199. animationInfo_.Resize(count);
  200. }
  201. else
  202. {
  203. for (unsigned i = animationInfo_.Size(); i < count; i++)
  204. {
  205. SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
  206. animationInfo_.Push(info);
  207. }
  208. }
  209. }
  210. void ModelImporter::GetAnimations(PODVector<Animation*>& animations)
  211. {
  212. animations.Clear();
  213. SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
  214. SharedPtr<XMLFile> xml(new XMLFile(context_));
  215. if (!xml->Load(*file))
  216. return;
  217. SharedPtr<Node> node(new Node(context_));
  218. node->LoadXML(xml->GetRoot());
  219. AnimationController* controller = node->GetComponent<AnimationController>();
  220. if (!controller)
  221. return;
  222. const Vector<SharedPtr<Animation>>& animresources = controller->GetAnimationResources();
  223. for (unsigned i = 0; i < animresources.Size(); i++)
  224. {
  225. if (animresources[i].NotNull())
  226. {
  227. animations.Push(animresources[i]);
  228. }
  229. }
  230. }
  231. bool ModelImporter::LoadSettingsInternal(JSONValue& jsonRoot)
  232. {
  233. if (!AssetImporter::LoadSettingsInternal(jsonRoot))
  234. return false;
  235. JSONValue import = jsonRoot.Get("ModelImporter");
  236. SetDefaults();
  237. if (import.Get("scale").IsNumber())
  238. scale_ = import.Get("scale").GetDouble();
  239. if (import.Get("importAnimations").IsBool())
  240. importAnimations_ = import.Get("importAnimations").GetBool();
  241. if (import.Get("animInfo").IsArray())
  242. {
  243. JSONArray animInfo = import.Get("animInfo").GetArray();
  244. for (unsigned i = 0; i < animInfo.Size(); i++)
  245. {
  246. JSONValue anim = animInfo[i];
  247. SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
  248. info->name_ = anim.Get("name").GetString();
  249. info->SetStartTime(anim.Get("startTime").GetFloat());
  250. info->SetEndTime(anim.Get("endTime").GetFloat());
  251. animationInfo_.Push(info);
  252. }
  253. }
  254. return true;
  255. }
  256. bool ModelImporter::SaveSettingsInternal(JSONValue& jsonRoot)
  257. {
  258. if (!AssetImporter::SaveSettingsInternal(jsonRoot))
  259. return false;
  260. JSONValue save;
  261. save.Set("scale", scale_);
  262. save.Set("importAnimations", importAnimations_);
  263. JSONArray animInfo;
  264. for (unsigned i = 0; i < animationInfo_.Size(); i++)
  265. {
  266. const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
  267. JSONValue jinfo;
  268. jinfo.Set("name", info->GetName());
  269. jinfo.Set("startTime", info->GetStartTime());
  270. jinfo.Set("endTime", info->GetEndTime());
  271. animInfo.Push(jinfo);
  272. }
  273. save.Set("animInfo", animInfo);
  274. jsonRoot.Set("ModelImporter", save);
  275. return true;
  276. }
  277. Resource* ModelImporter::GetResource(const String& typeName)
  278. {
  279. ResourceCache* cache = GetSubsystem<ResourceCache>();
  280. Model* model = cache->GetResource<Model>(asset_->GetCachePath() + ".mdl");
  281. return model;
  282. }
  283. Node* ModelImporter::InstantiateNode(Node* parent, const String& name)
  284. {
  285. SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
  286. SharedPtr<XMLFile> xml(new XMLFile(context_));
  287. if (!xml->Load(*file))
  288. return 0;
  289. Node* node = parent->CreateChild(name);
  290. node->LoadXML(xml->GetRoot());
  291. node->SetName(asset_->GetName());
  292. return node;
  293. }
  294. }