ModelImporter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Core/ProcessUtils.h>
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/IO/File.h>
  25. #include <Atomic/IO/FileSystem.h>
  26. #include <Atomic/Scene/Node.h>
  27. #include <Atomic/Graphics/AnimatedModel.h>
  28. #include <Atomic/Graphics/Animation.h>
  29. #include <Atomic/Graphics/AnimationController.h>
  30. #include <Atomic/Graphics/StaticModel.h>
  31. #include <Atomic/Graphics/Model.h>
  32. #include <Atomic/Resource/ResourceCache.h>
  33. #include <Atomic/Resource/XMLFile.h>
  34. #include "../Import/OpenAssetImporter.h"
  35. #include "Asset.h"
  36. #include "AssetDatabase.h"
  37. #include "ModelImporter.h"
  38. namespace ToolCore
  39. {
  40. /// Node + Model (static or animated)
  41. ModelImporter::ModelImporter(Context* context, Asset *asset) : AssetImporter(context, asset)
  42. {
  43. SetDefaults();
  44. }
  45. ModelImporter::~ModelImporter()
  46. {
  47. }
  48. void ModelImporter::SetDefaults()
  49. {
  50. AssetImporter::SetDefaults();
  51. SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
  52. scale_ = 1.0;
  53. importAnimations_ = false;
  54. importMaterials_ = importer->GetImportMaterialsDefault();
  55. animationInfo_.Clear();
  56. }
  57. bool ModelImporter::ImportModel()
  58. {
  59. ATOMIC_LOGDEBUGF("Importing Model: %s", asset_->GetPath().CString());
  60. SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
  61. importer->SetVerboseLog(true);
  62. importer->SetScale(scale_);
  63. importer->SetExportAnimations(false);
  64. importer->SetImportNode(importNode_);
  65. importer->SetImportMaterials(importMaterials_);
  66. if (importer->Load(asset_->GetPath()))
  67. {
  68. importer->ExportModel(asset_->GetCachePath());
  69. return true;
  70. }
  71. else
  72. {
  73. asset_->PostImportError(importer->GetErrorMessage());
  74. }
  75. return false;
  76. }
  77. /*void ModelImporter::SetImportMaterials(bool importMat)
  78. {
  79. LOGDEBUGF("Importing Materials of: %s", asset_->GetPath().CString());
  80. SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
  81. importer->SetImportMaterials(importMat);
  82. }*/
  83. bool ModelImporter::ImportAnimation(const String& filename, const String& name, float startTime, float endTime)
  84. {
  85. SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
  86. //importer->SetVerboseLog(true);
  87. importer->SetScale(scale_);
  88. importer->SetExportAnimations(true);
  89. importer->SetStartTime(startTime);
  90. importer->SetEndTime(endTime);
  91. if (importer->Load(filename))
  92. {
  93. importer->ExportModel(asset_->GetCachePath(), name, true);
  94. const Vector<OpenAssetImporter::AnimationInfo>& infos = importer->GetAnimationInfos();
  95. for (unsigned i = 0; i < infos.Size(); i++)
  96. {
  97. const OpenAssetImporter::AnimationInfo& info = infos.At(i);
  98. String pathName, fileName, extension;
  99. SplitPath(info.cacheFilename_, pathName, fileName, extension);
  100. ResourceCache* cache = GetSubsystem<ResourceCache>();
  101. AnimatedModel* animatedModel = importNode_->GetComponent<AnimatedModel>();
  102. AnimationController* controller = importNode_->GetComponent<AnimationController>();
  103. if (animatedModel && controller)
  104. {
  105. SharedPtr<Animation> animation = cache->GetTempResource<Animation>(fileName + extension);
  106. if (animation)
  107. controller->AddAnimationResource(animation);
  108. }
  109. ATOMIC_LOGINFOF("Import Info: %s : %s", info.name_.CString(), fileName.CString());
  110. }
  111. return true;
  112. }
  113. return false;
  114. }
  115. bool ModelImporter::ImportAnimations()
  116. {
  117. if (!animationInfo_.Size())
  118. {
  119. if (!ImportAnimation(asset_->GetPath(), "RootAnim"))
  120. return false;
  121. }
  122. // embedded animations
  123. for (unsigned i = 0; i < animationInfo_.Size(); i++)
  124. {
  125. const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
  126. if (!ImportAnimation(asset_->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  127. return false;
  128. }
  129. // add @ animations
  130. FileSystem* fs = GetSubsystem<FileSystem>();
  131. String pathName, fileName, ext;
  132. SplitPath(asset_->GetPath(), pathName, fileName, ext);
  133. Vector<String> results;
  134. fs->ScanDir(results, pathName, ext, SCAN_FILES, false);
  135. for (unsigned i = 0; i < results.Size(); i++)
  136. {
  137. const String& result = results[i];
  138. if (result.Contains("@"))
  139. {
  140. Vector<String> components = GetFileName(result).Split('@');
  141. if (components.Size() == 2 && components[1].Length() && components[0] == fileName)
  142. {
  143. String animationName = components[1];
  144. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  145. Asset* asset = db->GetAssetByPath(pathName + result);
  146. assert(asset);
  147. assert(asset->GetImporter()->GetType() == ModelImporter::GetTypeStatic());
  148. ModelImporter* importer = (ModelImporter*) asset->GetImporter();
  149. if (!importer->animationInfo_.Size())
  150. {
  151. if (!ImportAnimation(asset->GetPath(), animationName))
  152. return false;
  153. }
  154. else
  155. {
  156. // embedded animations
  157. for (unsigned i = 0; i < importer->animationInfo_.Size(); i++)
  158. {
  159. const SharedPtr<AnimationImportInfo>& info = importer->animationInfo_[i];
  160. if (!ImportAnimation(asset->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  161. return false;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. return true;
  168. }
  169. bool ModelImporter::Import()
  170. {
  171. String ext = asset_->GetExtension();
  172. String modelAssetFilename = asset_->GetPath();
  173. importNode_ = new Node(context_);
  174. if (ext == ".mdl")
  175. {
  176. FileSystem* fs = GetSubsystem<FileSystem>();
  177. ResourceCache* cache = GetSubsystem<ResourceCache>();
  178. // mdl files are native file format that doesn't need to be converted
  179. // doesn't allow scale, animations legacy primarily for ToonTown
  180. if (!fs->Copy(asset_->GetPath(), asset_->GetCachePath() + ".mdl"))
  181. {
  182. importNode_= 0;
  183. return false;
  184. }
  185. Model* mdl = cache->GetResource<Model>( asset_->GetCachePath() + ".mdl");
  186. if (!mdl)
  187. {
  188. importNode_= 0;
  189. return false;
  190. }
  191. // Force a reload, though file watchers will catch this delayed and load again
  192. cache->ReloadResource(mdl);
  193. importNode_->CreateComponent<StaticModel>()->SetModel(mdl);
  194. }
  195. else
  196. {
  197. // skip external animations, they will be brought in when importing their
  198. // corresponding model
  199. if (!modelAssetFilename.Contains("@"))
  200. {
  201. ImportModel();
  202. if (importAnimations_)
  203. {
  204. ImportAnimations();
  205. }
  206. SetImportMaterials(importMaterials_);
  207. }
  208. }
  209. File outFile(context_);
  210. if (!outFile.Open(asset_->GetCachePath(), FILE_WRITE))
  211. ErrorExit("Could not open output file " + asset_->GetCachePath());
  212. importNode_->SaveXML(outFile);
  213. importNode_ = 0;
  214. return true;
  215. }
  216. unsigned ModelImporter::GetAnimationCount()
  217. {
  218. return animationInfo_.Size();
  219. }
  220. void ModelImporter::SetAnimationCount(unsigned count)
  221. {
  222. if (animationInfo_.Size() >= count)
  223. {
  224. animationInfo_.Resize(count);
  225. }
  226. else
  227. {
  228. for (unsigned i = animationInfo_.Size(); i < count; i++)
  229. {
  230. SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
  231. animationInfo_.Push(info);
  232. }
  233. }
  234. }
  235. void ModelImporter::GetAnimations(PODVector<Animation*>& animations)
  236. {
  237. animations.Clear();
  238. SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
  239. SharedPtr<XMLFile> xml(new XMLFile(context_));
  240. if (!xml->Load(*file))
  241. return;
  242. SharedPtr<Node> node(new Node(context_));
  243. node->LoadXML(xml->GetRoot());
  244. AnimationController* controller = node->GetComponent<AnimationController>();
  245. if (!controller)
  246. return;
  247. const Vector<SharedPtr<Animation>>& animresources = controller->GetAnimationResources();
  248. for (unsigned i = 0; i < animresources.Size(); i++)
  249. {
  250. if (animresources[i].NotNull())
  251. {
  252. animations.Push(animresources[i]);
  253. }
  254. }
  255. }
  256. bool ModelImporter::LoadSettingsInternal(JSONValue& jsonRoot)
  257. {
  258. if (!AssetImporter::LoadSettingsInternal(jsonRoot))
  259. return false;
  260. JSONValue import = jsonRoot.Get("ModelImporter");
  261. SetDefaults();
  262. if (import.Get("scale").IsNumber())
  263. scale_ = import.Get("scale").GetDouble();
  264. if (import.Get("importAnimations").IsBool())
  265. importAnimations_ = import.Get("importAnimations").GetBool();
  266. if (import.Get("importMaterials").IsBool())
  267. {
  268. importMaterials_ = import.Get("importMaterials").GetBool();
  269. }
  270. else
  271. {
  272. }
  273. if (import.Get("animInfo").IsArray())
  274. {
  275. JSONArray animInfo = import.Get("animInfo").GetArray();
  276. for (unsigned i = 0; i < animInfo.Size(); i++)
  277. {
  278. JSONValue anim = animInfo[i];
  279. SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
  280. info->name_ = anim.Get("name").GetString();
  281. info->SetStartTime(anim.Get("startTime").GetFloat());
  282. info->SetEndTime(anim.Get("endTime").GetFloat());
  283. animationInfo_.Push(info);
  284. }
  285. }
  286. return true;
  287. }
  288. bool ModelImporter::SaveSettingsInternal(JSONValue& jsonRoot)
  289. {
  290. if (!AssetImporter::SaveSettingsInternal(jsonRoot))
  291. return false;
  292. JSONValue save;
  293. save.Set("scale", scale_);
  294. save.Set("importAnimations", importAnimations_);
  295. save.Set("importMaterials", importMaterials_);
  296. JSONArray animInfo;
  297. for (unsigned i = 0; i < animationInfo_.Size(); i++)
  298. {
  299. const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
  300. JSONValue jinfo;
  301. jinfo.Set("name", info->GetName());
  302. jinfo.Set("startTime", info->GetStartTime());
  303. jinfo.Set("endTime", info->GetEndTime());
  304. animInfo.Push(jinfo);
  305. }
  306. save.Set("animInfo", animInfo);
  307. jsonRoot.Set("ModelImporter", save);
  308. return true;
  309. }
  310. Resource* ModelImporter::GetResource(const String& typeName)
  311. {
  312. ResourceCache* cache = GetSubsystem<ResourceCache>();
  313. Model* model = cache->GetResource<Model>(asset_->GetCachePath() + ".mdl");
  314. return model;
  315. }
  316. Node* ModelImporter::InstantiateNode(Node* parent, const String& name)
  317. {
  318. SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
  319. SharedPtr<XMLFile> xml(new XMLFile(context_));
  320. if (!xml->Load(*file))
  321. return 0;
  322. Node* node = parent->CreateChild(name);
  323. node->LoadXML(xml->GetRoot());
  324. node->SetName(asset_->GetName());
  325. return node;
  326. }
  327. }