ModelImporter.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/AnimatedModel.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. AnimatedModel* animatedModel = importNode_->GetComponent<AnimatedModel>();
  70. if (animatedModel)
  71. {
  72. Model* model = animatedModel->GetModel();
  73. if (model)
  74. {
  75. SharedPtr<Animation> animation = cache->GetTempResource<Animation>(fileName + extension);
  76. if (animation)
  77. model->AddAnimationResource(animation);
  78. }
  79. }
  80. LOGINFOF("Import Info: %s : %s", info.name_.CString(), fileName.CString());
  81. }
  82. return true;
  83. }
  84. return false;
  85. }
  86. bool ModelImporter::ImportAnimations()
  87. {
  88. if (!animationInfo_.Size())
  89. {
  90. if (!ImportAnimation(asset_->GetPath(), "RootAnim"))
  91. return false;
  92. }
  93. // embedded animations
  94. for (unsigned i = 0; i < animationInfo_.Size(); i++)
  95. {
  96. const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
  97. if (!ImportAnimation(asset_->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  98. return false;
  99. }
  100. // add @ animations
  101. FileSystem* fs = GetSubsystem<FileSystem>();
  102. String pathName, fileName, ext;
  103. SplitPath(asset_->GetPath(), pathName, fileName, ext);
  104. Vector<String> results;
  105. fs->ScanDir(results, pathName, ext, SCAN_FILES, false);
  106. for (unsigned i = 0; i < results.Size(); i++)
  107. {
  108. const String& result = results[i];
  109. if (result.Contains("@"))
  110. {
  111. Vector<String> components = GetFileName(result).Split('@');
  112. if (components.Size() == 2 && components[1].Length() && components[0] == fileName)
  113. {
  114. String animationName = components[1];
  115. AssetDatabase* db = GetSubsystem<AssetDatabase>();
  116. Asset* asset = db->GetAssetByPath(pathName + result);
  117. assert(asset);
  118. assert(asset->GetImporter()->GetType() == ModelImporter::GetTypeStatic());
  119. ModelImporter* importer = (ModelImporter*) asset->GetImporter();
  120. if (!importer->animationInfo_.Size())
  121. {
  122. if (!ImportAnimation(asset->GetPath(), animationName))
  123. return false;
  124. }
  125. else
  126. {
  127. // embedded animations
  128. for (unsigned i = 0; i < importer->animationInfo_.Size(); i++)
  129. {
  130. const SharedPtr<AnimationImportInfo>& info = importer->animationInfo_[i];
  131. if (!ImportAnimation(asset->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
  132. return false;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. return true;
  139. }
  140. bool ModelImporter::Import()
  141. {
  142. String ext = asset_->GetExtension();
  143. String modelAssetFilename = asset_->GetPath();
  144. importNode_ = new Node(context_);
  145. if (ext == ".mdl")
  146. {
  147. FileSystem* fs = GetSubsystem<FileSystem>();
  148. ResourceCache* cache = GetSubsystem<ResourceCache>();
  149. // mdl files are native file format that doesn't need to be converted
  150. // doesn't allow scale, animations legacy primarily for ToonTown
  151. if (!fs->Copy(asset_->GetPath(), asset_->GetCachePath() + ".mdl"))
  152. {
  153. importNode_= 0;
  154. return false;
  155. }
  156. Model* mdl = cache->GetResource<Model>( asset_->GetCachePath() + ".mdl");
  157. if (!mdl)
  158. {
  159. importNode_= 0;
  160. return false;
  161. }
  162. // Force a reload, though file watchers will catch this delayed and load again
  163. cache->ReloadResource(mdl);
  164. importNode_->CreateComponent<StaticModel>()->SetModel(mdl);
  165. }
  166. else
  167. {
  168. // skip external animations, they will be brought in when importing their
  169. // corresponding model
  170. if (!modelAssetFilename.Contains("@"))
  171. {
  172. ImportModel();
  173. if (importAnimations_)
  174. {
  175. ImportAnimations();
  176. }
  177. AnimatedModel* animatedModel = importNode_->GetComponent<AnimatedModel>();
  178. if (animatedModel)
  179. {
  180. Model* model = animatedModel->GetModel();
  181. if (model && model->GetAnimationCount())
  182. {
  183. // resave with animation info
  184. File mdlFile(context_);
  185. if (!mdlFile.Open(asset_->GetCachePath() + ".mdl", FILE_WRITE))
  186. {
  187. ErrorExit("Could not open output file " + asset_->GetCachePath() + ".mdl");
  188. return false;
  189. }
  190. model->Save(mdlFile);
  191. }
  192. }
  193. }
  194. }
  195. File outFile(context_);
  196. if (!outFile.Open(asset_->GetCachePath(), FILE_WRITE))
  197. ErrorExit("Could not open output file " + asset_->GetCachePath());
  198. importNode_->SaveXML(outFile);
  199. importNode_ = 0;
  200. return true;
  201. }
  202. unsigned ModelImporter::GetAnimationCount()
  203. {
  204. return animationInfo_.Size();
  205. }
  206. void ModelImporter::SetAnimationCount(unsigned count)
  207. {
  208. if (animationInfo_.Size() >= count)
  209. {
  210. animationInfo_.Resize(count);
  211. }
  212. else
  213. {
  214. for (unsigned i = animationInfo_.Size(); i < count; i++)
  215. {
  216. SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
  217. animationInfo_.Push(info);
  218. }
  219. }
  220. }
  221. bool ModelImporter::LoadSettingsInternal()
  222. {
  223. if (!AssetImporter::LoadSettingsInternal())
  224. return false;
  225. JSONValue import = jsonRoot_.GetChild("ModelImporter", JSON_OBJECT);
  226. SetDefaults();
  227. if (import.HasMember("scale"))
  228. scale_ = import.GetFloat("scale");
  229. if (import.HasMember("importAnimations"))
  230. importAnimations_ = import.GetBool("importAnimations");
  231. if (import.HasMember("animInfo"))
  232. {
  233. JSONValue animInfo = import.GetChild("animInfo");
  234. for (unsigned i = 0; i < animInfo.GetSize(); i++)
  235. {
  236. JSONValue anim = animInfo.GetChild(i);
  237. SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
  238. info->name_ = anim.GetString("name");
  239. info->SetStartTime(anim.GetFloat("startTime"));
  240. info->SetEndTime(anim.GetFloat("endTime"));
  241. animationInfo_.Push(info);
  242. }
  243. }
  244. return true;
  245. }
  246. bool ModelImporter::SaveSettingsInternal()
  247. {
  248. if (!AssetImporter::SaveSettingsInternal())
  249. return false;
  250. JSONValue save = jsonRoot_.CreateChild("ModelImporter");
  251. save.SetFloat("scale", scale_);
  252. save.SetBool("importAnimations", importAnimations_);
  253. JSONValue animInfo = save.CreateChild("animInfo", JSON_ARRAY);
  254. for (unsigned i = 0; i < animationInfo_.Size(); i++)
  255. {
  256. const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
  257. JSONValue jinfo = animInfo.CreateChild();
  258. jinfo.SetString("name", info->GetName());
  259. jinfo.SetFloat("startTime", info->GetStartTime());
  260. jinfo.SetFloat("endTime", info->GetEndTime());
  261. }
  262. return true;
  263. }
  264. Resource* ModelImporter::GetResource(const String& typeName)
  265. {
  266. ResourceCache* cache = GetSubsystem<ResourceCache>();
  267. Model* model = cache->GetResource<Model>(asset_->GetCachePath() + ".mdl");
  268. return model;
  269. }
  270. }