ModelImporter.cpp 11 KB

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