ImportWrapper for asset application folder. It just uses the filename and path.
The extension testing is ugly but works. If there is an easier way I would appreciate an answer.
If you use 3ds you will need to add it. I dont have models from that software.
Using is simple.
ImportWrapper.Instance->Start();
ImportWrapper.Instance->Stop();
Happy not dragging.
bool ThreadFuncA(Thread &thread)
{
Str appAssetPath = SkipEnd(App.exe(), App.name()) + "Assets/";
UID start; //Zero
ImportWrapper.Instance()->GetAssets(appAssetPath, start);
return false; // stop the thread after importing assets
}
class ImportWrapper
{
Edit.EditorInterface m_EI;
Memc<Edit.Elm> m_elms; // list of project elements
Memc<Edit.Project> Projects; // list of projects
static ImportWrapper* m_instance = NULL;
// Get assets from path
void GetAssets(C Str& path, UID& parentid);
public:
ImportWrapper();
~ImportWrapper();
static ImportWrapper* Instance();
void Start();
void Stop();
bool SearchProjectElementList(Str name);
bool SearchProjectBySource(Str source);
bool ConnectEditor();
Image LoadImage(Str path);
bool LoadModel(Str path);
UID ImportEditorMaterial(Str path, Str materialName);
UID ImportEditorMesh(Str path, Str meshName, UID& parentid);
};
ImportWrapper::ImportWrapper()
{
}
ImportWrapper::~ImportWrapper()
{
}
ImportWrapper * ImportWrapper::Instance()
{
if(m_instance == NULL)
m_instance = new ImportWrapper();
return m_instance;
}
void ImportWrapper::Start()
{
bool conresult = ConnectEditor();
if(conresult)
{
Thread thread_a(ThreadFuncA);
thread_a.wait();
thread_a.del();
}
}
void ImportWrapper::Stop()
{
m_EI.disconnect();
delete m_instance;
m_instance = null;
}
bool ImportWrapper::ConnectEditor()
{
Str message;
if(!m_EI.connect(message))
Exit(message);
return true;
}
// gets assets recursively
void ImportWrapper::GetAssets(C Str& path, UID& parentid)
{
UID dirid;
for(FileFind ff(path); ff()
![wink wink](images/smilies/wink.gif)
// get files in path
{
if(ff.type == FSTD_DIR)
{
if(!SearchProjectElementList(ff.name))
{
dirid = m_EI.newElm(Edit.ELM_FOLDER, ff.name);
}
//directory recurse
GetAssets(ff.pathName()+"/", dirid);
}
else if(ff.type == FSTD_FILE)
{
// not very clean but works
Str searchtest = path + ff.name;
debugstring = path+ff.name;
//ff.
if(!SearchProjectBySource(searchtest))
{
Str extension = ".fbx";
Str skip = SkipEnd(ff.name, extension);
if(skip.length() < ff.name.length())
ImportEditorMesh(path+ff.name, ff.name(), parentid);
extension = ".obj";
skip = SkipEnd(ff.name, extension);
if(skip.length() < ff.name.length())
ImportEditorMesh(path+ff.name, ff.name, parentid);
extension = ".dae";
skip = SkipEnd(ff.name, extension);
if(skip.length() < ff.name.length())
ImportEditorMesh(path+ff.name, ff.name, parentid);
}
}
}
}
Image ImportWrapper::LoadImage(Str path)
{
Image loadimagefromfile;
Image loadimage;
Str extension = ".png";
Str skip = SkipEnd(path, extension);
if(skip.length() < path.length())
{
//debugstring = "Load Png";
// load png
if(!loadimagefromfile.ImportPNG(path))
Exit("Error Importer - Load png");
}
extension = ".jpg";
skip = SkipEnd(path, extension);
if(skip.length() < path.length())
{
// load jpg
if(!loadimagefromfile.ImportJPG(path))
Exit("Error Importer - Load jpg");
}
extension = ".tga";
skip = SkipEnd(path, extension);
if(skip.length() < path.length())
{
// load tga
if(!loadimagefromfile.ImportTGA(path))
Exit("Error Importer - Load tga");
}
extension = ".tif";
skip = SkipEnd(path, extension);
if(skip.length() < path.length())
{
// load tif
if(!loadimagefromfile.ImportTIF(path))
Exit("Error Importer - Load tif");
}
extension = ".bmp";
skip = SkipEnd(path, extension);
if(skip.length() < path.length())
{
// load bmp
if(!loadimagefromfile.ImportBMP(path))
Exit("Error Importer - Load bmp");
}
loadimage.mustCreate2D(512, 512, IMAGE_R8G8B8A8_SRGB, 1);
if(loadimage.lock()) // in order to edit the texture we must first lock it
{
FREPD(y, loadimage.h()) // iterate through all y's
FREPD(x, loadimage.w()) // iterate through all x's
{
loadimage.color(x, y, loadimagefromfile.color(x, y) );
}
loadimage.unlock(); // unlock
}
return loadimage;
}
bool ImportWrapper::LoadModel(Str path)
{
Str extension = ".obj";
Str skip = SkipEnd(path, extension);
//bool result = ImportOBJ(path, &mesh, mats, partindex);
if(skip.length() < path.length())
{
//debugstring = "Load Wavefront Model";
return ImportOBJ(path, &mesh, mats, partindex);
}
extension = ".fbx";
skip = SkipEnd(path, extension);
if(skip.length() < path.length())
{
return Import(path, &mesh, skeleton, animations, mats, partindex);
}
extension = ".dae";
skip = SkipEnd(path, extension);
if(skip.length() < path.length())
{
return Import(path, &mesh, skeleton, animations, mats, partindex);
}
return false;
}
bool ImportWrapper::SearchProjectElementList(Str name)
{
m_EI.getElms(m_elms); // get a list of project elements
FREPA(m_elms)
{
if(m_elms[i].full_name == name)
{
return true;
}
}
return false;
}
bool ImportWrapper::SearchProjectBySource(Str source)
{
m_EI.getElms(m_elms); // get a list of project elements
FREPA(m_elms)
{
if(m_elms[i].src_file == source)
{
return true;
}
}
return false;
}
UID ImportWrapper::ImportEditorMaterial(Str path, Str materialName)
{
UID matid = m_EI.newElm(Edit.ELM_MTRL, materialName);
Edit.Material editmaterial;
FileParams fp;
fp = path;
Mems<FileParams>fpars;
fpars.add(fp);
editmaterial.color_map = fpars;
//Fix-add normal and other params if found
m_EI.setMaterial(matid, editmaterial , true, true);
return matid;
}
UID ImportWrapper::ImportEditorMesh(Str path, Str meshname, UID& parentid)
{
//debugstring = path;
UID meshid = m_EI.newElm(Edit.ELM_OBJ, meshname, parentid);
Mesh importmesh;
CMemPtr <UID> meshidptr;
meshidptr.point(meshid);
m_EI.setMesh(meshid, importmesh);
EE.Edit.IDParam<Str> test;
test.id = meshid;
test.value = path;
CMemPtr<EE.Edit.IDParam<Str>> testmemptr;
testmemptr.point(test);
m_EI.setElmSrcFile(testmemptr);
m_EI.reloadElms(meshidptr, true);
return meshid;
}