ModelImporter.cpp 10 KB

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