| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- #include <Atomic/Core/ProcessUtils.h>
- #include <Atomic/IO/Log.h>
- #include <Atomic/IO/File.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Scene/Node.h>
- #include <Atomic/Atomic3D/AnimationController.h>
- #include <Atomic/Atomic3D/Animation.h>
- #include <Atomic/Atomic3D/Model.h>
- #include <Atomic/Resource/ResourceCache.h>
- #include "../Import/OpenAssetImporter.h"
- #include "Asset.h"
- #include "AssetDatabase.h"
- #include "ModelImporter.h"
- namespace ToolCore
- {
- /// Node + Model (static or animated)
- ModelImporter::ModelImporter(Context* context, Asset *asset) : AssetImporter(context, asset)
- {
- SetDefaults();
- }
- ModelImporter::~ModelImporter()
- {
- }
- void ModelImporter::SetDefaults()
- {
- AssetImporter::SetDefaults();
- scale_ = 1.0f;
- importAnimations_ = false;
- animationInfo_.Clear();
- }
- bool ModelImporter::ImportModel()
- {
- SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
- //importer->SetVerboseLog(true);
- importer->SetScale(scale_);
- importer->SetExportAnimations(false);
- importer->SetImportNode(importNode_);
- if (importer->Load(asset_->GetPath()))
- {
- importer->ExportModel(asset_->GetCachePath());
- return true;
- }
- else
- {
- asset_->PostImportError(importer->GetErrorMessage());
- }
- return false;
- }
- bool ModelImporter::ImportAnimation(const String& filename, const String& name, float startTime, float endTime)
- {
- SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
- //importer->SetVerboseLog(true);
- importer->SetScale(scale_);
- importer->SetExportAnimations(true);
- importer->SetStartTime(startTime);
- importer->SetEndTime(endTime);
- if (importer->Load(filename))
- {
- importer->ExportModel(asset_->GetCachePath(), name, true);
- const Vector<OpenAssetImporter::AnimationInfo>& infos = importer->GetAnimationInfos();
- for (unsigned i = 0; i < infos.Size(); i++)
- {
- const OpenAssetImporter::AnimationInfo& info = infos.At(i);
- String pathName, fileName, extension;
- SplitPath(info.cacheFilename_, pathName, fileName, extension);
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- AnimationController* controller = importNode_->GetComponent<AnimationController>();
- if (controller)
- {
- SharedPtr<Animation> animation = cache->GetTempResource<Animation>(fileName + extension);
- if (animation)
- controller->AddAnimationResource(animation);
- }
- LOGINFOF("Import Info: %s : %s", info.name_.CString(), fileName.CString());
- }
- return true;
- }
- return false;
- }
- bool ModelImporter::ImportAnimations()
- {
- if (!animationInfo_.Size())
- {
- if (!ImportAnimation(asset_->GetPath(), "RootAnim"))
- return false;
- }
- // embedded animations
- for (unsigned i = 0; i < animationInfo_.Size(); i++)
- {
- const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
- if (!ImportAnimation(asset_->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
- return false;
- }
- // add @ animations
- FileSystem* fs = GetSubsystem<FileSystem>();
- String pathName, fileName, ext;
- SplitPath(asset_->GetPath(), pathName, fileName, ext);
- Vector<String> results;
- fs->ScanDir(results, pathName, ext, SCAN_FILES, false);
- for (unsigned i = 0; i < results.Size(); i++)
- {
- const String& result = results[i];
- if (result.Contains("@"))
- {
- Vector<String> components = GetFileName(result).Split('@');
- if (components.Size() == 2 && components[1].Length() && components[0] == fileName)
- {
- String animationName = components[1];
- AssetDatabase* db = GetSubsystem<AssetDatabase>();
- Asset* asset = db->GetAssetByPath(pathName + result);
- assert(asset);
- assert(asset->GetImporter()->GetType() == ModelImporter::GetTypeStatic());
- ModelImporter* importer = (ModelImporter*) asset->GetImporter();
- if (!importer->animationInfo_.Size())
- {
- if (!ImportAnimation(asset->GetPath(), animationName))
- return false;
- }
- else
- {
- // embedded animations
- for (unsigned i = 0; i < importer->animationInfo_.Size(); i++)
- {
- const SharedPtr<AnimationImportInfo>& info = importer->animationInfo_[i];
- if (!ImportAnimation(asset->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
- return false;
- }
- }
- }
- }
- }
- return true;
- }
- bool ModelImporter::Import()
- {
- String modelAssetFilename = asset_->GetPath();
- importNode_ = new Node(context_);
- // skip external animations, they will be brought in when importing their
- // corresponding model
- if (!modelAssetFilename.Contains("@"))
- {
- ImportModel();
- if (importAnimations_)
- {
- ImportAnimations();
- }
- }
- File outFile(context_);
- if (!outFile.Open(asset_->GetCachePath(), FILE_WRITE))
- ErrorExit("Could not open output file " + asset_->GetCachePath());
- importNode_->SaveXML(outFile);
- importNode_ = 0;
- return true;
- }
- unsigned ModelImporter::GetAnimationCount()
- {
- return animationInfo_.Size();
- }
- void ModelImporter::SetAnimationCount(unsigned count)
- {
- if (animationInfo_.Size() >= count)
- {
- animationInfo_.Resize(count);
- }
- else
- {
- for (unsigned i = animationInfo_.Size(); i < count; i++)
- {
- SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
- animationInfo_.Push(info);
- }
- }
- }
- bool ModelImporter::LoadSettingsInternal()
- {
- if (!AssetImporter::LoadSettingsInternal())
- return false;
- JSONValue import = jsonRoot_.GetChild("ModelImporter", JSON_OBJECT);
- SetDefaults();
- if (import.HasMember("scale"))
- scale_ = import.GetFloat("scale");
- if (import.HasMember("importAnimations"))
- importAnimations_ = import.GetBool("importAnimations");
- if (import.HasMember("animInfo"))
- {
- JSONValue animInfo = import.GetChild("animInfo");
- for (unsigned i = 0; i < animInfo.GetSize(); i++)
- {
- JSONValue anim = animInfo.GetChild(i);
- SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
- info->name_ = anim.GetString("name");
- info->SetStartTime(anim.GetFloat("startTime"));
- info->SetEndTime(anim.GetFloat("endTime"));
- animationInfo_.Push(info);
- }
- }
- return true;
- }
- bool ModelImporter::SaveSettingsInternal()
- {
- if (!AssetImporter::SaveSettingsInternal())
- return false;
- JSONValue save = jsonRoot_.CreateChild("ModelImporter");
- save.SetFloat("scale", scale_);
- save.SetBool("importAnimations", importAnimations_);
- JSONValue animInfo = save.CreateChild("animInfo", JSON_ARRAY);
- for (unsigned i = 0; i < animationInfo_.Size(); i++)
- {
- const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
- JSONValue jinfo = animInfo.CreateChild();
- jinfo.SetString("name", info->GetName());
- jinfo.SetFloat("startTime", info->GetStartTime());
- jinfo.SetFloat("endTime", info->GetEndTime());
- }
- return true;
- }
- Resource* ModelImporter::GetResource()
- {
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- Model* model = cache->GetResource<Model>(asset_->GetCachePath() + ".mdl");
- return model;
- }
- }
|