patch [#23781] Dynamically Load Actions

from Mitchell Stokes (moguri)
This commit is contained in:
Campbell Barton 2010-10-09 10:45:25 +00:00
parent f7fb4e70b3
commit bd00aa9727
3 changed files with 42 additions and 2 deletions

@ -40,6 +40,7 @@
#include "KX_KetsjiEngine.h"
#include "KX_IPhysicsController.h"
#include "BL_Material.h"
#include "BL_ActionActuator.h"
#include "KX_BlenderMaterial.h"
#include "KX_PolygonMaterial.h"
@ -960,7 +961,7 @@ bool KX_BlenderSceneConverter::LinkBlendFile(BlendHandle *bpy_openlib, const cha
static char err_local[255];
/* only scene and mesh supported right now */
if(idcode!=ID_SCE && idcode!=ID_ME) {
if(idcode!=ID_SCE && idcode!=ID_ME &&idcode!=ID_AC) {
snprintf(err_local, sizeof(err_local), "invalid ID type given \"%s\"\n", group);
return false;
}
@ -1018,6 +1019,16 @@ bool KX_BlenderSceneConverter::LinkBlendFile(BlendHandle *bpy_openlib, const cha
kx_scene->GetLogicManager()->RegisterMeshName(meshobj->GetName(),meshobj);
}
}
else if(idcode==ID_AC) {
/* Convert all actions */
ID *action;
KX_Scene *kx_scene= m_currentScene;
for(action= (ID *)main_newlib->action.first; action; action= (ID *)action->next) {
printf("ActionName: %s\n", action->name);
kx_scene->GetLogicManager()->RegisterActionName(action->name+2, action);
}
}
else if(idcode==ID_SCE) {
/* Merge all new linked in scene into the existing one */
ID *scene;
@ -1092,6 +1103,23 @@ bool KX_BlenderSceneConverter::FreeBlendFile(struct Main *maggie)
}
}
}
/* Now unregister actions */
{
GEN_Map<STR_HashedString,void*> &mapStringToActions = scene->GetLogicManager()->GetActionMap();
for(int i=0; i<mapStringToActions.size(); i++)
{
ID *action= (ID*) *mapStringToActions.at(i);
if(IS_TAGGED(action))
{
STR_HashedString an = action->name+2;
mapStringToActions.remove(an);
i--;
}
}
}
//scene->FreeTagged(); /* removed tagged objects and meshes*/
CListValue *obj_lists[] = {scene->GetObjectList(), scene->GetInactiveList(), NULL};
@ -1128,6 +1156,17 @@ bool KX_BlenderSceneConverter::FreeBlendFile(struct Main *maggie)
break;
}
}
/* make sure action actuators are not referencing tagged actions */
for (int act_idx=0; act_idx<gameobj->GetActuators().size(); act_idx++)
{
if (gameobj->GetActuators()[act_idx]->IsType(SCA_IActuator::KX_ACT_ACTION))
{
BL_ActionActuator *act = (BL_ActionActuator*)gameobj->GetActuators()[act_idx];
if(IS_TAGGED(act->GetAction()))
act->SetAction(NULL);
}
}
}
}
}

@ -127,6 +127,7 @@ public:
void RegisterMeshName(const STR_String& meshname,void* mesh);
void UnregisterMeshName(const STR_String& meshname,void* mesh);
GEN_Map<STR_HashedString,void*>& GetMeshMap() { return m_mapStringToMeshes; };
GEN_Map<STR_HashedString,void*>& GetActionMap() { return m_mapStringToActions; };
void RegisterActionName(const STR_String& actname,void* action);

@ -179,7 +179,7 @@ General functions
:arg blend: The path to the blend file (or the name to use for the library if data is supplied)
:type blend: string
:arg type: The datablock type (currently only "Scene" and "Mesh" are supported)
:arg type: The datablock type (currently only "Action", "Mesh" and "Scene" are supported)
:type type: string
:arg data: Binary data from a blend file (optional)
:type data: bytes