Some slight refactoring of the BGE's LibLoad code to make things a bit cleaner (no...
[blender.git] / source / gameengine / Converter / KX_BlenderSceneConverter.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/Converter/KX_BlenderSceneConverter.cpp
29  *  \ingroup bgeconv
30  */
31
32
33 #if defined(WIN32) && !defined(FREE_WINDOWS)
34 #pragma warning (disable:4786) // suppress stl-MSVC debug info warning
35 #endif
36
37 #include "KX_Scene.h"
38 #include "KX_GameObject.h"
39 #include "KX_BlenderSceneConverter.h"
40 #include "KX_IpoConvert.h"
41 #include "RAS_MeshObject.h"
42 #include "KX_PhysicsEngineEnums.h"
43 #include "PHY_IPhysicsEnvironment.h"
44 #include "KX_KetsjiEngine.h"
45 #include "KX_IPhysicsController.h"
46 #include "BL_Material.h"
47 #include "BL_ActionActuator.h"
48 #include "KX_BlenderMaterial.h"
49 #include "KX_PolygonMaterial.h"
50
51
52 #include "BL_System.h"
53
54 #include "DummyPhysicsEnvironment.h"
55
56 #include "KX_ConvertPhysicsObject.h"
57
58 #ifdef USE_BULLET
59 #include "CcdPhysicsEnvironment.h"
60 #endif
61
62 #include "KX_BlenderSceneConverter.h"
63 #include "KX_BlenderScalarInterpolator.h"
64 #include "BL_BlenderDataConversion.h"
65 #include "BlenderWorldInfo.h"
66 #include "KX_Scene.h"
67
68 /* This little block needed for linking to Blender... */
69 #ifdef WIN32
70 #include "BLI_winstuff.h"
71 #endif
72
73 /* This list includes only data type definitions */
74 #include "DNA_scene_types.h"
75 #include "DNA_world_types.h"
76 #include "BKE_main.h"
77
78 #include "BLI_math.h"
79
80 extern "C"
81 {
82 #include "DNA_object_types.h"
83 #include "DNA_curve_types.h"
84 #include "DNA_mesh_types.h"
85 #include "DNA_material_types.h"
86 #include "BLI_blenlib.h"
87 #include "MEM_guardedalloc.h"
88 #include "BKE_global.h"
89 #include "BKE_animsys.h"
90 #include "BKE_library.h"
91 #include "BKE_material.h" // BKE_material_copy
92 #include "BKE_mesh.h" // BKE_mesh_copy
93 #include "DNA_space_types.h"
94 #include "DNA_anim_types.h"
95 #include "RNA_define.h"
96 #include "../../blender/editors/include/ED_keyframing.h"
97 }
98
99 /* Only for dynamic loading and merging */
100 #include "RAS_BucketManager.h" // XXX cant stay
101 #include "KX_BlenderSceneConverter.h"
102 #include "BL_BlenderDataConversion.h"
103 #include "KX_MeshProxy.h"
104 #include "RAS_MeshObject.h"
105 extern "C" {
106         #include "BKE_context.h"
107         #include "BLO_readfile.h"
108         #include "BKE_idcode.h"
109         #include "BKE_report.h"
110         #include "DNA_space_types.h"
111         #include "DNA_windowmanager_types.h" /* report api */
112         #include "../../blender/blenlib/BLI_linklist.h"
113 }
114
115 KX_BlenderSceneConverter::KX_BlenderSceneConverter(
116                                                         struct Main* maggie,
117                                                         class KX_KetsjiEngine* engine
118                                                         )
119                                                         : m_maggie(maggie),
120                                                         /*m_maggie_dyn(NULL),*/
121                                                         m_ketsjiEngine(engine),
122                                                         m_alwaysUseExpandFraming(false),
123                                                         m_usemat(false),
124                                                         m_useglslmat(false)
125 {
126         tag_main(maggie, 0); /* avoid re-tagging later on */
127         m_newfilename = "";
128 }
129
130
131 KX_BlenderSceneConverter::~KX_BlenderSceneConverter()
132 {
133         // clears meshes, and hashmaps from blender to gameengine data
134         int i;
135         // delete sumoshapes
136         
137
138         int numAdtLists = m_map_blender_to_gameAdtList.size();
139         for (i=0; i<numAdtLists; i++) {
140                 BL_InterpolatorList *adtList= *m_map_blender_to_gameAdtList.at(i);
141
142                 delete (adtList);
143         }
144
145         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator itw = m_worldinfos.begin();
146         while (itw != m_worldinfos.end()) {
147                 delete (*itw).second;
148                 itw++;
149         }
150
151         vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator itp = m_polymaterials.begin();
152         while (itp != m_polymaterials.end()) {
153                 delete (*itp).second;
154                 itp++;
155         }
156
157         // delete after RAS_IPolyMaterial
158         vector<pair<KX_Scene*,BL_Material *> >::iterator itmat = m_materials.begin();
159         while (itmat != m_materials.end()) {
160                 delete (*itmat).second;
161                 itmat++;
162         }       
163
164
165         vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator itm = m_meshobjects.begin();
166         while (itm != m_meshobjects.end()) {
167                 delete (*itm).second;
168                 itm++;
169         }
170
171 #ifdef USE_BULLET
172         KX_ClearBulletSharedShapes();
173 #endif
174
175         /* free any data that was dynamically loaded */
176         for (vector<Main*>::iterator it=m_DynamicMaggie.begin(); !(it==m_DynamicMaggie.end()); it++) {
177                 Main *main= *it;
178                 free_main(main);
179         }
180
181         m_DynamicMaggie.clear();
182 }
183
184 void KX_BlenderSceneConverter::SetNewFileName(const STR_String& filename)
185 {
186         m_newfilename = filename;
187 }
188
189
190
191 bool KX_BlenderSceneConverter::TryAndLoadNewFile()
192 {
193         bool result = false;
194
195         // find the file
196 /*      if ()
197         {
198                 result = true;
199         }
200         // if not, clear the newfilename
201         else
202         {
203                 m_newfilename = "";     
204         }
205 */
206         return result;
207 }
208
209 Scene *KX_BlenderSceneConverter::GetBlenderSceneForName(const STR_String& name)
210 {
211         Scene *sce;
212
213         /**
214          * Find the specified scene by name, or the first
215          * scene if nothing matches (shouldn't happen).
216          */
217         if ((sce= (Scene *)BLI_findstring(&m_maggie->scene, name.ReadPtr(), offsetof(ID, name) + 2)))
218                 return sce;
219
220         for (vector<Main*>::iterator it=m_DynamicMaggie.begin(); !(it==m_DynamicMaggie.end()); it++) {
221                 Main *main= *it;
222
223                 if ((sce= (Scene *)BLI_findstring(&main->scene, name.ReadPtr(), offsetof(ID, name) + 2)))
224                         return sce;
225         }
226
227         return (Scene*)m_maggie->scene.first;
228
229 }
230 #include "KX_PythonInit.h"
231
232 #ifdef USE_BULLET
233
234 #include "LinearMath/btIDebugDraw.h"
235
236
237 struct  BlenderDebugDraw : public btIDebugDraw
238 {
239         BlenderDebugDraw () :
240                 m_debugMode(0) 
241         {
242         }
243         
244         int m_debugMode;
245
246         virtual void    drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
247         {
248                 if (m_debugMode >0)
249                 {
250                         MT_Vector3 kxfrom(from[0],from[1],from[2]);
251                         MT_Vector3 kxto(to[0],to[1],to[2]);
252                         MT_Vector3 kxcolor(color[0],color[1],color[2]);
253
254                         KX_RasterizerDrawDebugLine(kxfrom,kxto,kxcolor);
255                 }
256         }
257         
258         virtual void    reportErrorWarning(const char* warningString)
259         {
260
261         }
262
263         virtual void    drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,float distance,int lifeTime,const btVector3& color)
264         {
265                 //not yet
266         }
267
268         virtual void    setDebugMode(int debugMode)
269         {
270                 m_debugMode = debugMode;
271         }
272         virtual int             getDebugMode() const
273         {
274                 return m_debugMode;
275         }
276         ///todo: find out if Blender can do this
277         virtual void    draw3dText(const btVector3& location,const char* textString)
278         {
279
280         }
281                 
282 };
283
284 #endif
285
286 void KX_BlenderSceneConverter::ConvertScene(class KX_Scene* destinationscene,
287                                                                                         class RAS_IRenderTools* rendertools,
288                                                                                         class RAS_ICanvas* canvas)
289 {
290         //find out which physics engine
291         Scene *blenderscene = destinationscene->GetBlenderScene();
292
293         e_PhysicsEngine physics_engine = UseBullet;
294         bool useDbvtCulling = false;
295         // hook for registration function during conversion.
296         m_currentScene = destinationscene;
297         destinationscene->SetSceneConverter(this);
298         SG_SetActiveStage(SG_STAGE_CONVERTER);
299
300         if (blenderscene)
301         {
302         
303                 switch (blenderscene->gm.physicsEngine)
304                 {
305                 case WOPHY_BULLET:
306                         {
307                                 physics_engine = UseBullet;
308                                 useDbvtCulling = (blenderscene->gm.mode & WO_DBVT_CULLING) != 0;
309                                 break;
310                         }
311                         default:
312                         case WOPHY_NONE:
313                         {
314                                 physics_engine = UseNone;
315                                 break;
316                         }
317                 }
318         }
319
320         switch (physics_engine)
321         {
322 #ifdef USE_BULLET
323                 case UseBullet:
324                         {
325                                 CcdPhysicsEnvironment* ccdPhysEnv = new CcdPhysicsEnvironment(useDbvtCulling);
326                                 ccdPhysEnv->setDebugDrawer(new BlenderDebugDraw());
327                                 ccdPhysEnv->setDeactivationLinearTreshold(blenderscene->gm.lineardeactthreshold);
328                                 ccdPhysEnv->setDeactivationAngularTreshold(blenderscene->gm.angulardeactthreshold);
329                                 ccdPhysEnv->setDeactivationTime(blenderscene->gm.deactivationtime);
330
331                                 SYS_SystemHandle syshandle = SYS_GetSystem(); /*unused*/
332                                 int visualizePhysics = SYS_GetCommandLineInt(syshandle,"show_physics",0);
333                                 if (visualizePhysics)
334                                         ccdPhysEnv->setDebugMode(btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb|btIDebugDraw::DBG_DrawContactPoints|btIDebugDraw::DBG_DrawText|btIDebugDraw::DBG_DrawConstraintLimits|btIDebugDraw::DBG_DrawConstraints);
335                 
336                                 //todo: get a button in blender ?
337                                 //disable / enable debug drawing (contact points, aabb's etc)   
338                                 //ccdPhysEnv->setDebugMode(1);
339                                 destinationscene->SetPhysicsEnvironment(ccdPhysEnv);
340                                 break;
341                         }
342 #endif
343                 default:
344                 case UseNone:
345                         physics_engine = UseNone;
346                         destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
347                         break;
348         }
349
350         BL_ConvertBlenderObjects(m_maggie,
351                 destinationscene,
352                 m_ketsjiEngine,
353                 physics_engine,
354                 rendertools,
355                 canvas,
356                 this,
357                 m_alwaysUseExpandFraming
358                 );
359
360         //These lookup are not needed during game
361         m_map_blender_to_gameactuator.clear();
362         m_map_blender_to_gamecontroller.clear();
363         m_map_blender_to_gameobject.clear();
364
365         //Clearing this lookup table has the effect of disabling the cache of meshes
366         //between scenes, even if they are shared in the blend file.
367         //This cache mecanism is buggy so I leave it disable and the memory leak
368         //that would result from this is fixed in RemoveScene()
369         m_map_mesh_to_gamemesh.clear();
370
371 #ifndef USE_BULLET
372         /* quiet compiler warning */
373         (void)useDbvtCulling;
374 #endif
375
376 }
377
378 // This function removes all entities stored in the converter for that scene
379 // It should be used instead of direct delete scene
380 // Note that there was some provision for sharing entities (meshes...) between
381 // scenes but that is now disabled so all scene will have their own copy
382 // and we can delete them here. If the sharing is reactivated, change this code too..
383 // (see KX_BlenderSceneConverter::ConvertScene)
384 void KX_BlenderSceneConverter::RemoveScene(KX_Scene *scene)
385 {
386         int i, size;
387         // delete the scene first as it will stop the use of entities
388         delete scene;
389         // delete the entities of this scene
390         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator worldit;
391         size = m_worldinfos.size();
392         for (i=0, worldit=m_worldinfos.begin(); i<size; ) {
393                 if ((*worldit).first == scene) {
394                         delete (*worldit).second;
395                         *worldit = m_worldinfos.back();
396                         m_worldinfos.pop_back();
397                         size--;
398                 } else {
399                         i++;
400                         worldit++;
401                 }
402         }
403
404         vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator polymit;
405         size = m_polymaterials.size();
406         for (i=0, polymit=m_polymaterials.begin(); i<size; ) {
407                 if ((*polymit).first == scene) {
408                         delete (*polymit).second;
409                         *polymit = m_polymaterials.back();
410                         m_polymaterials.pop_back();
411                         size--;
412                 } else {
413                         i++;
414                         polymit++;
415                 }
416         }
417
418         vector<pair<KX_Scene*,BL_Material*> >::iterator matit;
419         size = m_materials.size();
420         for (i=0, matit=m_materials.begin(); i<size; ) {
421                 if ((*matit).first == scene) {
422                         delete (*matit).second;
423                         *matit = m_materials.back();
424                         m_materials.pop_back();
425                         size--;
426                 } else {
427                         i++;
428                         matit++;
429                 }
430         }
431
432         vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator meshit;
433         size = m_meshobjects.size();
434         for (i=0, meshit=m_meshobjects.begin(); i<size; ) {
435                 if ((*meshit).first == scene) {
436                         delete (*meshit).second;
437                         *meshit = m_meshobjects.back();
438                         m_meshobjects.pop_back();
439                         size--;
440                 } else {
441                         i++;
442                         meshit++;
443                 }
444         }
445 }
446
447 // use blender materials
448 void KX_BlenderSceneConverter::SetMaterials(bool val)
449 {
450         m_usemat = val;
451         m_useglslmat = false;
452 }
453
454 void KX_BlenderSceneConverter::SetGLSLMaterials(bool val)
455 {
456         m_usemat = val;
457         m_useglslmat = val;
458 }
459
460 bool KX_BlenderSceneConverter::GetMaterials()
461 {
462         return m_usemat;
463 }
464
465 bool KX_BlenderSceneConverter::GetGLSLMaterials()
466 {
467         return m_useglslmat;
468 }
469
470 void KX_BlenderSceneConverter::RegisterBlenderMaterial(BL_Material *mat)
471 {
472         m_materials.push_back(pair<KX_Scene*,BL_Material *>(m_currentScene,mat));
473 }
474
475
476
477 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
478         bool to_what)
479 {
480         m_alwaysUseExpandFraming= to_what;
481 }
482
483         
484
485 void KX_BlenderSceneConverter::RegisterGameObject(
486                                                                         KX_GameObject *gameobject, 
487                                                                         struct Object *for_blenderobject) 
488 {
489         /* only maintained while converting, freed during game runtime */
490         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
491 }
492
493 /* only need to run this during conversion since
494  * m_map_blender_to_gameobject is freed after conversion */
495 void KX_BlenderSceneConverter::UnregisterGameObject(
496                                                                         KX_GameObject *gameobject) 
497 {
498         struct Object *bobp= gameobject->GetBlenderObject();
499         if (bobp) {
500                 CHashedPtr bptr(bobp);
501                 KX_GameObject **gobp= m_map_blender_to_gameobject[bptr];
502                 if (gobp && *gobp == gameobject)
503                 {
504                         // also maintain m_map_blender_to_gameobject if the gameobject
505                         // being removed is matching the blender object
506                         m_map_blender_to_gameobject.remove(bptr);
507                 }
508         }
509 }
510
511 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
512                                                                         struct Object *for_blenderobject) 
513 {
514         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
515         
516         return obp?*obp:NULL;
517 }
518
519 void KX_BlenderSceneConverter::RegisterGameMesh(
520                                                                         RAS_MeshObject *gamemesh,
521                                                                         struct Mesh *for_blendermesh)
522 {
523         if (for_blendermesh) { /* dynamically loaded meshes we don't want to keep lookups for */
524                 m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
525         }
526         m_meshobjects.push_back(pair<KX_Scene*,RAS_MeshObject*>(m_currentScene,gamemesh));
527 }
528
529
530
531 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
532                                                                         struct Mesh *for_blendermesh/*,
533                                                                         unsigned int onlayer*/)
534 {
535         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
536         
537         if (meshp/* && onlayer==(*meshp)->GetLightLayer()*/) {
538                 return *meshp;
539         } else {
540                 return NULL;
541         }
542 }
543
544         
545
546
547         
548
549 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
550 {
551         m_polymaterials.push_back(pair<KX_Scene*,RAS_IPolyMaterial*>(m_currentScene,polymat));
552 }
553
554
555
556 void KX_BlenderSceneConverter::RegisterInterpolatorList(
557                                                                         BL_InterpolatorList *actList,
558                                                                         struct bAction *for_act)
559 {
560         m_map_blender_to_gameAdtList.insert(CHashedPtr(for_act), actList);
561 }
562
563
564
565 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
566                                                                         struct bAction *for_act)
567 {
568         BL_InterpolatorList **listp = m_map_blender_to_gameAdtList[CHashedPtr(for_act)];
569                 
570         return listp?*listp:NULL;
571 }
572
573
574
575 void KX_BlenderSceneConverter::RegisterGameActuator(
576                                                                         SCA_IActuator *act,
577                                                                         struct bActuator *for_actuator)
578 {
579         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
580 }
581
582
583
584 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
585                                                                         struct bActuator *for_actuator)
586 {
587         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
588         
589         return actp?*actp:NULL;
590 }
591
592
593
594 void KX_BlenderSceneConverter::RegisterGameController(
595                                                                         SCA_IController *cont,
596                                                                         struct bController *for_controller)
597 {
598         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
599 }
600
601
602
603 SCA_IController *KX_BlenderSceneConverter::FindGameController(
604                                                                         struct bController *for_controller)
605 {
606         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
607         
608         return contp?*contp:NULL;
609 }
610
611
612
613 void KX_BlenderSceneConverter::RegisterWorldInfo(
614                                                                         KX_WorldInfo *worldinfo)
615 {
616         m_worldinfos.push_back(pair<KX_Scene*,KX_WorldInfo*>(m_currentScene,worldinfo));
617 }
618
619 void    KX_BlenderSceneConverter::ResetPhysicsObjectsAnimationIpo(bool clearIpo)
620 {
621
622         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
623         int numScenes = scenes->size();
624         int i;
625         for (i=0;i<numScenes;i++)
626         {
627                 KX_Scene* scene = scenes->at(i);
628                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
629                 CListValue* parentList = scene->GetRootParentList();
630                 int numObjects = parentList->GetCount();
631                 int g;
632                 for (g=0;g<numObjects;g++)
633                 {
634                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
635                         if (gameObj->IsDynamic())
636                         {
637                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
638                                 
639                                 Object* blenderObject = gameObj->GetBlenderObject();
640                                 if (blenderObject)
641                                 {
642 #if 0
643                                         //erase existing ipo's
644                                         Ipo* ipo = blenderObject->ipo;//findIpoForName(blenderObject->id.name+2);
645                                         if (ipo)
646                                         {       //clear the curve data
647                                                 if (clearIpo) {//rcruiz
648                                                         IpoCurve *icu1;
649                                                                                                                 
650                                                         int numCurves = 0;
651                                                         for ( icu1 = (IpoCurve*)ipo->curve.first; icu1;  ) {
652                                                         
653                                                                 IpoCurve* tmpicu = icu1;
654                                                                 
655                                                                 /*int i;
656                                                                 BezTriple *bezt;
657                                                                 for ( bezt = tmpicu->bezt, i = 0;       i < tmpicu->totvert; i++, bezt++) {
658                                                                         printf("(%f,%f,%f),(%f,%f,%f),(%f,%f,%f)\n",bezt->vec[0][0],bezt->vec[0][1],bezt->vec[0][2],bezt->vec[1][0],bezt->vec[1][1],bezt->vec[1][2],bezt->vec[2][0],bezt->vec[2][1],bezt->vec[2][2]);
659                                                                 }*/
660                                                                 
661                                                                 icu1 = icu1->next;
662                                                                 numCurves++;
663                         
664                                                                 BLI_remlink( &( blenderObject->ipo->curve ), tmpicu );
665                                                                 if ( tmpicu->bezt )
666                                                                         MEM_freeN( tmpicu->bezt );
667                                                                 MEM_freeN( tmpicu );
668                                                                 localDel_ipoCurve( tmpicu );
669                                                         }
670                                                 }
671                                         } else
672                                         {       ipo = NULL; // XXX add_ipo(blenderObject->id.name+2, ID_OB);
673                                                 blenderObject->ipo = ipo;
674
675                                         }
676 #endif
677                                 }
678                         }
679
680                 }
681                 
682         
683         }
684
685
686
687 }
688
689 void    KX_BlenderSceneConverter::resetNoneDynamicObjectToIpo()
690 {
691         if (addInitFromFrame) {         
692                 KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
693                 int numScenes = scenes->size();
694                 if (numScenes>=0) {
695                         KX_Scene* scene = scenes->at(0);
696                         CListValue* parentList = scene->GetRootParentList();
697                         for (int ix=0;ix<parentList->GetCount();ix++) {
698                                 KX_GameObject* gameobj = (KX_GameObject*)parentList->GetValue(ix);
699                                 if (!gameobj->IsDynamic()) {
700                                         Object* blenderobject = gameobj->GetBlenderObject();
701                                         if (!blenderobject)
702                                                 continue;
703                                         if (blenderobject->type==OB_ARMATURE)
704                                                 continue;
705                                         float eu[3];
706                                         mat4_to_eul(eu,blenderobject->obmat);                                   
707                                         MT_Point3 pos = MT_Point3(
708                                                 blenderobject->obmat[3][0],
709                                                 blenderobject->obmat[3][1],
710                                                 blenderobject->obmat[3][2]
711                                         );
712                                         MT_Vector3 eulxyz = MT_Vector3(
713                                                 eu[0],
714                                                 eu[1],
715                                                 eu[2]
716                                         );
717                                         MT_Vector3 scale = MT_Vector3(
718                                                 blenderobject->size[0],
719                                                 blenderobject->size[1],
720                                                 blenderobject->size[2]
721                                         );
722                                         gameobj->NodeSetLocalPosition(pos);
723                                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
724                                         gameobj->NodeSetLocalScale(scale);
725                                         gameobj->NodeUpdateGS(0);
726                                 }
727                         }
728                 }
729         }
730 }
731
732
733         ///this generates ipo curves for position, rotation, allowing to use game physics in animation
734 void    KX_BlenderSceneConverter::WritePhysicsObjectToAnimationIpo(int frameNumber)
735 {
736
737         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
738         int numScenes = scenes->size();
739         int i;
740         for (i=0;i<numScenes;i++)
741         {
742                 KX_Scene* scene = scenes->at(i);
743                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
744                 CListValue* parentList = scene->GetObjectList();
745                 int numObjects = parentList->GetCount();
746                 int g;
747                 for (g=0;g<numObjects;g++)
748                 {
749                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
750                         Object* blenderObject = gameObj->GetBlenderObject();
751                         if (blenderObject && blenderObject->parent==NULL && gameObj->IsDynamic())
752                         {
753                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
754
755                                 if (blenderObject->adt==NULL)
756                                         BKE_id_add_animdata(&blenderObject->id);
757
758                                 if (blenderObject->adt)
759                                 {
760                                         const MT_Point3& position = gameObj->NodeGetWorldPosition();
761                                         //const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
762                                         const MT_Matrix3x3& orn = gameObj->NodeGetWorldOrientation();
763
764                                         position.getValue(blenderObject->loc);
765
766                                         float tmat[3][3];
767                                         for (int r=0;r<3;r++)
768                                                 for (int c=0;c<3;c++)
769                                                         tmat[r][c] = (float)orn[c][r];
770
771                                         mat3_to_compatible_eul(blenderObject->rot, blenderObject->rot, tmat);
772
773                                         insert_keyframe(NULL, &blenderObject->id, NULL, NULL, "location", -1, (float)frameNumber, INSERTKEY_FAST);
774                                         insert_keyframe(NULL, &blenderObject->id, NULL, NULL, "rotation_euler", -1, (float)frameNumber, INSERTKEY_FAST);
775
776 #if 0
777                                         const MT_Point3& position = gameObj->NodeGetWorldPosition();
778                                         //const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
779                                         const MT_Matrix3x3& orn = gameObj->NodeGetWorldOrientation();
780                                         
781                                         float eulerAngles[3];   
782                                         float eulerAnglesOld[3] = {0.0f, 0.0f, 0.0f};                                           
783                                         float tmat[3][3];
784                                         
785                                         // XXX animato
786                                         Ipo* ipo = blenderObject->ipo;
787
788                                         //create the curves, if not existing, set linear if new
789
790                                         IpoCurve *icu_lx = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
791                                         if (!icu_lx) {
792                                                 icu_lx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_X, 1);
793                                                 if (icu_lx) icu_lx->ipo = IPO_LIN;
794                                         }
795                                         IpoCurve *icu_ly = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
796                                         if (!icu_ly) {
797                                                 icu_ly = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Y, 1);
798                                                 if (icu_ly) icu_ly->ipo = IPO_LIN;
799                                         }
800                                         IpoCurve *icu_lz = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
801                                         if (!icu_lz) {
802                                                 icu_lz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Z, 1);
803                                                 if (icu_lz) icu_lz->ipo = IPO_LIN;
804                                         }
805                                         IpoCurve *icu_rx = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
806                                         if (!icu_rx) {
807                                                 icu_rx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_X, 1);
808                                                 if (icu_rx) icu_rx->ipo = IPO_LIN;
809                                         }
810                                         IpoCurve *icu_ry = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
811                                         if (!icu_ry) {
812                                                 icu_ry = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Y, 1);
813                                                 if (icu_ry) icu_ry->ipo = IPO_LIN;
814                                         }
815                                         IpoCurve *icu_rz = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
816                                         if (!icu_rz) {
817                                                 icu_rz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Z, 1);
818                                                 if (icu_rz) icu_rz->ipo = IPO_LIN;
819                                         }
820                                         
821                                         if (icu_rx) eulerAnglesOld[0]= eval_icu( icu_rx, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
822                                         if (icu_ry) eulerAnglesOld[1]= eval_icu( icu_ry, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
823                                         if (icu_rz) eulerAnglesOld[2]= eval_icu( icu_rz, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
824                                         
825                                         // orn.getValue((float *)tmat); // uses the wrong ordering, cant use this
826                                         for (int r=0;r<3;r++)
827                                                 for (int c=0;c<3;c++)
828                                                         tmat[r][c] = orn[c][r];
829                                         
830                                         // mat3_to_eul( eulerAngles,tmat); // better to use Mat3ToCompatibleEul
831                                         mat3_to_compatible_eul( eulerAngles, eulerAnglesOld,tmat);
832                                         
833                                         //eval_icu
834                                         for (int x = 0; x < 3; x++)
835                                                 eulerAngles[x] *= (float) ((180 / 3.14159265f) / 10.0);
836                                         
837                                         //fill the curves with data
838                                         if (icu_lx) insert_vert_icu(icu_lx, frameNumber, position.x(), 1);
839                                         if (icu_ly) insert_vert_icu(icu_ly, frameNumber, position.y(), 1);
840                                         if (icu_lz) insert_vert_icu(icu_lz, frameNumber, position.z(), 1);
841                                         if (icu_rx) insert_vert_icu(icu_rx, frameNumber, eulerAngles[0], 1);
842                                         if (icu_ry) insert_vert_icu(icu_ry, frameNumber, eulerAngles[1], 1);
843                                         if (icu_rz) insert_vert_icu(icu_rz, frameNumber, eulerAngles[2], 1);
844                                         
845                                         // Handles are corrected at the end, testhandles_ipocurve isn't needed yet
846 #endif
847                                 }
848                         }
849                 }
850         }
851 }
852
853
854 void    KX_BlenderSceneConverter::TestHandlesPhysicsObjectToAnimationIpo()
855 {
856
857         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
858         int numScenes = scenes->size();
859         int i;
860         for (i=0;i<numScenes;i++)
861         {
862                 KX_Scene* scene = scenes->at(i);
863                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
864                 CListValue* parentList = scene->GetRootParentList();
865                 int numObjects = parentList->GetCount();
866                 int g;
867                 for (g=0;g<numObjects;g++)
868                 {
869                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
870                         if (gameObj->IsDynamic())
871                         {
872                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
873                                 
874 #if 0
875                                 Object* blenderObject = gameObj->GetBlenderObject();
876                                 if (blenderObject && blenderObject->ipo)
877                                 {
878                                         // XXX animato
879                                         Ipo* ipo = blenderObject->ipo;
880                                         
881                                         //create the curves, if not existing
882                                         //testhandles_ipocurve checks for NULL
883                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocX"));
884                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocY"));
885                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ"));
886                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotX"));
887                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotY"));
888                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ"));
889                                 }
890 #endif
891                         }
892                 }
893         }
894 }
895
896 #ifdef WITH_PYTHON
897 PyObject *KX_BlenderSceneConverter::GetPyNamespace()
898 {
899         return m_ketsjiEngine->GetPyNamespace();
900 }
901 #endif
902
903 vector<Main*> &KX_BlenderSceneConverter::GetMainDynamic()
904 {
905         return m_DynamicMaggie;
906 }
907
908 Main* KX_BlenderSceneConverter::GetMainDynamicPath(const char *path)
909 {
910         for (vector<Main*>::iterator it=m_DynamicMaggie.begin(); !(it==m_DynamicMaggie.end()); it++)
911                 if (BLI_path_cmp((*it)->name, path) == 0)
912                         return *it;
913         
914         return NULL;
915 }
916
917 bool KX_BlenderSceneConverter::LinkBlendFileMemory(void *data, int length, const char *path, char *group, KX_Scene *scene_merge, char **err_str, short options)
918 {
919         BlendHandle *bpy_openlib = BLO_blendhandle_from_memory(data, length);
920
921         // Error checking is done in LinkBlendFile
922         return LinkBlendFile(bpy_openlib, path, group, scene_merge, err_str, options);
923 }
924
925 bool KX_BlenderSceneConverter::LinkBlendFilePath(const char *path, char *group, KX_Scene *scene_merge, char **err_str, short options)
926 {
927         BlendHandle *bpy_openlib = BLO_blendhandle_from_file((char *)path, NULL);
928
929         // Error checking is done in LinkBlendFile
930         return LinkBlendFile(bpy_openlib, path, group, scene_merge, err_str, options);
931 }
932
933 static void load_datablocks(Main *main_newlib, BlendHandle *bpy_openlib, const char *path, int idcode)
934 {
935         Main *main_tmp= NULL; /* created only for linking, then freed */
936         LinkNode *names = NULL;
937         short flag= 0; /* don't need any special options */
938
939         /* here appending/linking starts */
940         main_tmp = BLO_library_append_begin(main_newlib, &bpy_openlib, (char *)path);
941
942         int totnames_dummy;
943         names = BLO_blendhandle_get_datablock_names( bpy_openlib, idcode, &totnames_dummy);
944         
945         int i=0;
946         LinkNode *n= names;
947         while(n) {
948                 BLO_library_append_named_part(main_tmp, &bpy_openlib, (char *)n->link, idcode);
949                 n= (LinkNode *)n->next;
950                 i++;
951         }
952         BLI_linklist_free(names, free); /* free linklist *and* each node's data */
953         
954         BLO_library_append_end(NULL, main_tmp, &bpy_openlib, idcode, flag);
955 }
956
957 bool KX_BlenderSceneConverter::LinkBlendFile(BlendHandle *bpy_openlib, const char *path, char *group, KX_Scene *scene_merge, char **err_str, short options)
958 {
959         Main *main_newlib; /* stored as a dynamic 'main' until we free it */
960         int idcode= BKE_idcode_from_name(group);
961         ReportList reports;
962         static char err_local[255];
963         
964         /* only scene and mesh supported right now */
965         if (idcode!=ID_SCE && idcode!=ID_ME &&idcode!=ID_AC) {
966                 snprintf(err_local, sizeof(err_local), "invalid ID type given \"%s\"\n", group);
967                 *err_str= err_local;
968                 BLO_blendhandle_close(bpy_openlib);
969                 return false;
970         }
971         
972         if (GetMainDynamicPath(path)) {
973                 snprintf(err_local, sizeof(err_local), "blend file already open \"%s\"\n", path);
974                 *err_str= err_local;
975                 BLO_blendhandle_close(bpy_openlib);
976                 return false;
977         }
978
979         if (bpy_openlib==NULL) {
980                 snprintf(err_local, sizeof(err_local), "could not open blendfile \"%s\"\n", path);
981                 *err_str= err_local;
982                 return false;
983         }
984         
985         main_newlib= (Main *)MEM_callocN( sizeof(Main), "BgeMain");
986         BKE_reports_init(&reports, RPT_STORE);  
987
988         load_datablocks(main_newlib, bpy_openlib, path, idcode);
989
990         /* now do another round of linking for Scenes so all actions are properly loaded */
991         if (idcode==ID_SCE && options & LIB_LOAD_LOAD_ACTIONS) {
992                 load_datablocks(main_newlib, bpy_openlib, path, ID_AC);
993         }
994         
995         BLO_blendhandle_close(bpy_openlib);
996
997         BKE_reports_clear(&reports);
998         /* done linking */      
999         
1000         /* needed for lookups*/
1001         GetMainDynamic().push_back(main_newlib);
1002         strncpy(main_newlib->name, path, sizeof(main_newlib->name));    
1003         
1004         
1005         if (idcode==ID_ME) {
1006                 /* Convert all new meshes into BGE meshes */
1007                 ID* mesh;
1008         
1009                 for (mesh= (ID *)main_newlib->mesh.first; mesh; mesh= (ID *)mesh->next ) {
1010                         if (options & LIB_LOAD_VERBOSE)
1011                                 printf("MeshName: %s\n", mesh->name+2);
1012                         RAS_MeshObject *meshobj = BL_ConvertMesh((Mesh *)mesh, NULL, scene_merge, this);
1013                         scene_merge->GetLogicManager()->RegisterMeshName(meshobj->GetName(),meshobj);
1014                 }
1015         }
1016         else if (idcode==ID_AC) {
1017                 /* Convert all actions */
1018                 ID *action;
1019
1020                 for (action= (ID *)main_newlib->action.first; action; action= (ID *)action->next) {
1021                         if (options & LIB_LOAD_VERBOSE)
1022                                 printf("ActionName: %s\n", action->name+2);
1023                         scene_merge->GetLogicManager()->RegisterActionName(action->name+2, action);
1024                 }
1025         }
1026         else if (idcode==ID_SCE) {              
1027                 /* Merge all new linked in scene into the existing one */
1028                 ID *scene;
1029                 for (scene= (ID *)main_newlib->scene.first; scene; scene= (ID *)scene->next ) {
1030                         if (options & LIB_LOAD_VERBOSE)
1031                                 printf("SceneName: %s\n", scene->name+2);
1032                         
1033                         /* merge into the base  scene */
1034                         KX_Scene* other= m_ketsjiEngine->CreateScene((Scene *)scene);
1035                         scene_merge->MergeScene(other);
1036                         
1037                         // RemoveScene(other); // Don't run this, it frees the entire scene converter data, just delete the scene
1038                         delete other;
1039                 }
1040
1041                 /* Now handle all the actions */
1042                 if (options & LIB_LOAD_LOAD_ACTIONS) {
1043                         ID *action;
1044
1045                         for (action= (ID *)main_newlib->action.first; action; action= (ID *)action->next) {
1046                                 if (options & LIB_LOAD_VERBOSE)
1047                                         printf("ActionName: %s\n", action->name+2);
1048                                 scene_merge->GetLogicManager()->RegisterActionName(action->name+2, action);
1049                         }
1050                 }
1051         }
1052         
1053         return true;
1054 }
1055
1056 /* Note m_map_*** are all ok and don't need to be freed
1057  * most are temp and NewRemoveObject frees m_map_gameobject_to_blender */
1058 bool KX_BlenderSceneConverter::FreeBlendFile(struct Main *maggie)
1059 {
1060         int maggie_index= -1;
1061         int i=0;
1062
1063         if (maggie==NULL)
1064                 return false;
1065         
1066         /* tag all false except the one we remove */
1067         for (vector<Main*>::iterator it=m_DynamicMaggie.begin(); !(it==m_DynamicMaggie.end()); it++) {
1068                 Main *main= *it;
1069                 if (main != maggie) {
1070                         tag_main(main, 0);
1071                 }
1072                 else {
1073                         maggie_index= i;
1074                 }
1075                 i++;
1076         }
1077
1078         /* should never happen but just to be safe */
1079         if (maggie_index == -1)
1080                 return false;
1081
1082         m_DynamicMaggie.erase(m_DynamicMaggie.begin() + maggie_index);
1083         tag_main(maggie, 1);
1084
1085
1086         /* free all tagged objects */
1087         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
1088         int numScenes = scenes->size();
1089
1090
1091         for (int scene_idx=0;scene_idx<numScenes;scene_idx++)
1092         {
1093                 KX_Scene* scene = scenes->at(scene_idx);
1094                 if (IS_TAGGED(scene->GetBlenderScene())) {
1095                         RemoveScene(scene); // XXX - not tested yet
1096                         scene_idx--;
1097                         numScenes--;
1098                 }
1099                 else {
1100                         
1101                         /* in case the mesh might be refered to later */
1102                         {
1103                                 CTR_Map<STR_HashedString,void*> &mapStringToMeshes = scene->GetLogicManager()->GetMeshMap();
1104                                 
1105                                 for (int i=0; i<mapStringToMeshes.size(); i++)
1106                                 {
1107                                         RAS_MeshObject *meshobj= (RAS_MeshObject *) *mapStringToMeshes.at(i);
1108                                         if (meshobj && IS_TAGGED(meshobj->GetMesh()))
1109                                         {       
1110                                                 STR_HashedString mn = meshobj->GetName();
1111                                                 mapStringToMeshes.remove(mn);
1112                                                 m_map_mesh_to_gamemesh.remove(CHashedPtr(meshobj->GetMesh()));
1113                                                 i--;
1114                                         }
1115                                 }
1116                         }
1117
1118                         /* Now unregister actions */
1119                         {
1120                                 CTR_Map<STR_HashedString,void*> &mapStringToActions = scene->GetLogicManager()->GetActionMap();
1121
1122                                 for (int i=0; i<mapStringToActions.size(); i++)
1123                                 {
1124                                         ID *action= (ID*) *mapStringToActions.at(i);
1125
1126                                         if (IS_TAGGED(action))
1127                                         {
1128                                                 STR_HashedString an = action->name+2;
1129                                                 mapStringToActions.remove(an);
1130                                                 i--;
1131                                         }
1132                                 }
1133                         }
1134                         
1135                         //scene->FreeTagged(); /* removed tagged objects and meshes*/
1136                         CListValue *obj_lists[] = {scene->GetObjectList(), scene->GetInactiveList(), NULL};
1137
1138                         for (int ob_ls_idx=0; obj_lists[ob_ls_idx]; ob_ls_idx++)
1139                         {
1140                                 CListValue *obs= obj_lists[ob_ls_idx];
1141                                 RAS_MeshObject* mesh;
1142
1143                                 for (int ob_idx = 0; ob_idx < obs->GetCount(); ob_idx++)
1144                                 {
1145                                         KX_GameObject* gameobj = (KX_GameObject*)obs->GetValue(ob_idx);
1146                                         if (IS_TAGGED(gameobj->GetBlenderObject())) {
1147
1148                                                 int size_before = obs->GetCount();
1149
1150                                                 /* Eventually calls RemoveNodeDestructObject
1151                                                  * frees m_map_gameobject_to_blender from UnregisterGameObject */
1152                                                 scene->RemoveObject(gameobj);
1153
1154                                                 if (size_before != obs->GetCount())
1155                                                         ob_idx--;
1156                                                 else {
1157                                                         printf("ERROR COULD NOT REMOVE \"%s\"\n", gameobj->GetName().ReadPtr());
1158                                                 }
1159                                         }
1160                                         else {
1161                                                 /* free the mesh, we could be referecing a linked one! */
1162                                                 int mesh_index= gameobj->GetMeshCount();
1163                                                 while(mesh_index--) {
1164                                                         mesh= gameobj->GetMesh(mesh_index);
1165                                                         if (IS_TAGGED(mesh->GetMesh())) {
1166                                                                 gameobj->RemoveMeshes(); /* XXX - slack, should only remove meshes that are library items but mostly objects only have 1 mesh */
1167                                                                 break;
1168                                                         }
1169                                                 }
1170
1171                                                 /* make sure action actuators are not referencing tagged actions */
1172                                                 for (unsigned int act_idx=0; act_idx<gameobj->GetActuators().size(); act_idx++)
1173                                                 {
1174                                                         if (gameobj->GetActuators()[act_idx]->IsType(SCA_IActuator::KX_ACT_ACTION))
1175                                                         {
1176                                                                 BL_ActionActuator *act = (BL_ActionActuator*)gameobj->GetActuators()[act_idx];
1177                                                                 if (IS_TAGGED(act->GetAction()))
1178                                                                         act->SetAction(NULL);
1179                                                         }
1180                                                 }
1181                                         }
1182                                 }
1183                         }
1184                 }
1185         }
1186
1187
1188         int size;
1189
1190         // delete the entities of this scene
1191         /* TODO - */
1192 #if 0
1193         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator worldit;
1194         size = m_worldinfos.size();
1195         for (i=0, worldit=m_worldinfos.begin(); i<size; ) {
1196                 if ((*worldit).second) {
1197                         delete (*worldit).second;
1198                         *worldit = m_worldinfos.back();
1199                         m_worldinfos.pop_back();
1200                         size--;
1201                 } else {
1202                         i++;
1203                         worldit++;
1204                 }
1205         }
1206 #endif
1207
1208
1209         /* Worlds don't reference original blender data so we need to make a set from them */
1210         typedef std::set<KX_WorldInfo*> KX_WorldInfoSet;
1211         KX_WorldInfoSet worldset;
1212         for (int scene_idx=0;scene_idx<numScenes;scene_idx++)
1213         {
1214                 KX_Scene* scene = scenes->at(scene_idx);
1215                 if (scene->GetWorldInfo())
1216                         worldset.insert( scene->GetWorldInfo() );
1217         }
1218
1219         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator worldit;
1220         size = m_worldinfos.size();
1221         for (i=0, worldit=m_worldinfos.begin(); i<size; ) {
1222                 if ((*worldit).second && (worldset.count((*worldit).second)) == 0) {
1223                         delete (*worldit).second;
1224                         *worldit = m_worldinfos.back();
1225                         m_worldinfos.pop_back();
1226                         size--;
1227                 } else {
1228                         i++;
1229                         worldit++;
1230                 }
1231         }
1232         worldset.clear();
1233         /* done freeing the worlds */
1234
1235
1236
1237
1238         vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator polymit;
1239         size = m_polymaterials.size();
1240
1241
1242
1243         for (i=0, polymit=m_polymaterials.begin(); i<size; ) {
1244                 RAS_IPolyMaterial *mat= (*polymit).second;
1245                 Material *bmat= NULL;
1246
1247                 /* Why do we need to check for RAS_BLENDERMAT if both are cast to a (PyObject*)? - Campbell */
1248                 if (mat->GetFlag() & RAS_BLENDERMAT) {
1249                         KX_BlenderMaterial *bl_mat = static_cast<KX_BlenderMaterial*>(mat);
1250                         bmat= bl_mat->GetBlenderMaterial();
1251
1252                 } else {
1253                         KX_PolygonMaterial *kx_mat = static_cast<KX_PolygonMaterial*>(mat);
1254                         bmat= kx_mat->GetBlenderMaterial();
1255                 }
1256
1257                 if (IS_TAGGED(bmat)) {
1258                         /* only remove from bucket */
1259                         ((*polymit).first)->GetBucketManager()->RemoveMaterial(mat);
1260                 }
1261
1262                 i++;
1263                 polymit++;
1264         }
1265
1266
1267
1268         for (i=0, polymit=m_polymaterials.begin(); i<size; ) {
1269                 RAS_IPolyMaterial *mat= (*polymit).second;
1270                 Material *bmat= NULL;
1271
1272                 /* Why do we need to check for RAS_BLENDERMAT if both are cast to a (PyObject*)? - Campbell */
1273                 if (mat->GetFlag() & RAS_BLENDERMAT) {
1274                         KX_BlenderMaterial *bl_mat = static_cast<KX_BlenderMaterial*>(mat);
1275                         bmat= bl_mat->GetBlenderMaterial();
1276
1277                 } else {
1278                         KX_PolygonMaterial *kx_mat = static_cast<KX_PolygonMaterial*>(mat);
1279                         bmat= kx_mat->GetBlenderMaterial();
1280                 }
1281
1282                 if (bmat) {
1283                         //printf("FOUND MAT '%s' !!! ", ((ID*)bmat)->name+2);
1284                 }
1285                 else {
1286                         //printf("LOST MAT  !!!");
1287                 }
1288
1289                 if (IS_TAGGED(bmat)) {
1290
1291                         delete (*polymit).second;
1292                         *polymit = m_polymaterials.back();
1293                         m_polymaterials.pop_back();
1294                         size--;
1295                         //printf("tagged !\n");
1296                 } else {
1297                         i++;
1298                         polymit++;
1299                         //printf("(un)tagged !\n");
1300                 }
1301         }
1302
1303         vector<pair<KX_Scene*,BL_Material*> >::iterator matit;
1304         size = m_materials.size();
1305         for (i=0, matit=m_materials.begin(); i<size; ) {
1306                 BL_Material *mat= (*matit).second;
1307                 if (IS_TAGGED(mat->material)) {
1308                         delete (*matit).second;
1309                         *matit = m_materials.back();
1310                         m_materials.pop_back();
1311                         size--;
1312                 } else {
1313                         i++;
1314                         matit++;
1315                 }
1316         }
1317
1318         vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator meshit;
1319         size = m_meshobjects.size();
1320         for (i=0, meshit=m_meshobjects.begin(); i<size; ) {
1321                 RAS_MeshObject *me= (*meshit).second;
1322                 if (IS_TAGGED(me->GetMesh())) {
1323                         delete (*meshit).second;
1324                         *meshit = m_meshobjects.back();
1325                         m_meshobjects.pop_back();
1326                         size--;
1327                 } else {
1328                         i++;
1329                         meshit++;
1330                 }
1331         }
1332
1333         free_main(maggie);
1334
1335         return true;
1336 }
1337
1338 bool KX_BlenderSceneConverter::FreeBlendFile(const char *path)
1339 {
1340         return FreeBlendFile(GetMainDynamicPath(path));
1341 }
1342
1343 bool KX_BlenderSceneConverter::MergeScene(KX_Scene *to, KX_Scene *from)
1344 {
1345
1346         {
1347                 vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator itp = m_worldinfos.begin();
1348                 while (itp != m_worldinfos.end()) {
1349                         if ((*itp).first==from)
1350                                 (*itp).first= to;
1351                         itp++;
1352                 }
1353         }
1354
1355         {
1356                 vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator itp = m_polymaterials.begin();
1357                 while (itp != m_polymaterials.end()) {
1358                         if ((*itp).first==from) {
1359                                 (*itp).first= to;
1360
1361                                 /* also switch internal data */
1362                                 RAS_IPolyMaterial*mat= (*itp).second;
1363                                 mat->Replace_IScene(to);
1364                         }
1365                         itp++;
1366                 }
1367         }
1368
1369         {
1370                 vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator itp = m_meshobjects.begin();
1371                 while (itp != m_meshobjects.end()) {
1372                         if ((*itp).first==from)
1373                                 (*itp).first= to;
1374                         itp++;
1375                 }
1376         }
1377
1378         {
1379                 vector<pair<KX_Scene*,BL_Material*> >::iterator itp = m_materials.begin();
1380                 while (itp != m_materials.end()) {
1381                         if ((*itp).first==from)
1382                                 (*itp).first= to;
1383                         itp++;
1384                 }
1385         }
1386         
1387         return true;
1388 }
1389
1390 /* This function merges a mesh from the current scene into another main
1391  * it does not convert */
1392 RAS_MeshObject *KX_BlenderSceneConverter::ConvertMeshSpecial(KX_Scene* kx_scene, Main *maggie, const char *name)
1393 {
1394         /* Find a mesh in the current main */
1395         ID *me= static_cast<ID *>(BLI_findstring(&m_maggie->mesh, name, offsetof(ID, name) + 2));
1396         
1397         if (me==NULL) {
1398                 printf("Could not be found \"%s\"\n", name);
1399                 return NULL;
1400         }
1401         
1402         /* Watch this!, if its used in the original scene can cause big troubles */
1403         if (me->us > 0) {
1404                 printf("Mesh has a user \"%s\"\n", name);
1405                 me = (ID*)BKE_mesh_copy((Mesh*)me);
1406                 me->us--;
1407         }
1408         BLI_remlink(&m_maggie->mesh, me); /* even if we made the copy it needs to be removed */
1409         BLI_addtail(&maggie->mesh, me);
1410
1411         
1412         /* Must copy the materials this uses else we cant free them */
1413         {
1414                 Mesh *mesh= (Mesh *)me;
1415                 
1416                 /* ensure all materials are tagged */
1417                 for (int i=0; i<mesh->totcol; i++)
1418                         if (mesh->mat[i])
1419                                 mesh->mat[i]->id.flag &= ~LIB_DOIT;
1420                 
1421                 for (int i=0; i<mesh->totcol; i++)
1422                 {
1423                         Material *mat_old= mesh->mat[i];
1424                         
1425                         /* if its tagged its a replaced material */
1426                         if (mat_old && (mat_old->id.flag & LIB_DOIT)==0)
1427                         {
1428                                 Material *mat_old= mesh->mat[i];
1429                                 Material *mat_new= BKE_material_copy( mat_old );
1430                                 
1431                                 mat_new->id.flag |= LIB_DOIT;
1432                                 mat_old->id.us--;
1433                                 
1434                                 BLI_remlink(&m_maggie->mat, mat_new);
1435                                 BLI_addtail(&maggie->mat, mat_new);
1436                                 
1437                                 mesh->mat[i]= mat_new;
1438                                 
1439                                 /* the same material may be used twice */
1440                                 for (int j=i+1; j<mesh->totcol; j++)
1441                                 {
1442                                         if (mesh->mat[j]==mat_old)
1443                                         {
1444                                                 mesh->mat[j]= mat_new;
1445                                                 mat_new->id.us++;
1446                                                 mat_old->id.us--;
1447                                         }
1448                                 }
1449                         }
1450                 }
1451         }
1452         
1453         RAS_MeshObject *meshobj = BL_ConvertMesh((Mesh *)me, NULL, kx_scene, this);
1454         kx_scene->GetLogicManager()->RegisterMeshName(meshobj->GetName(),meshobj);
1455         m_map_mesh_to_gamemesh.clear(); /* This is at runtime so no need to keep this, BL_ConvertMesh adds */
1456         return meshobj;
1457 }