BGE: removing remaining reference to sce->world->gravity/physics/... (it's all in...
[blender.git] / source / gameengine / Converter / KX_BlenderSceneConverter.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #ifdef WIN32
30         #pragma warning (disable:4786) // suppress stl-MSVC debug info warning
31 #endif
32
33 #include "KX_Scene.h"
34 #include "KX_GameObject.h"
35 #include "KX_BlenderSceneConverter.h"
36 #include "KX_IpoConvert.h"
37 #include "RAS_MeshObject.h"
38 #include "KX_PhysicsEngineEnums.h"
39 #include "PHY_IPhysicsEnvironment.h"
40 #include "KX_KetsjiEngine.h"
41 #include "KX_IPhysicsController.h"
42 #include "BL_Material.h"
43 #include "SYS_System.h"
44
45 #include "DummyPhysicsEnvironment.h"
46
47 #include "KX_ConvertPhysicsObject.h"
48
49 #ifdef USE_BULLET
50 #include "CcdPhysicsEnvironment.h"
51 #endif
52
53 #include "KX_BlenderSceneConverter.h"
54 #include "KX_BlenderScalarInterpolator.h"
55 #include "BL_BlenderDataConversion.h"
56 #include "BlenderWorldInfo.h"
57 #include "KX_Scene.h"
58
59 /* This little block needed for linking to Blender... */
60 #ifdef WIN32
61 #include "BLI_winstuff.h"
62 #endif
63
64 /* This list includes only data type definitions */
65 #include "DNA_scene_types.h"
66 #include "DNA_world_types.h"
67 #include "BKE_main.h"
68
69 #include "BLI_arithb.h"
70
71 extern "C"
72 {
73 #include "DNA_object_types.h"
74 #include "DNA_curve_types.h"
75 #include "BLI_blenlib.h"
76 #include "MEM_guardedalloc.h"
77 //XXX #include "BSE_editipo.h"
78 //XXX #include "BSE_editipo_types.h"
79 #include "DNA_ipo_types.h"
80 #include "BKE_global.h"
81 #include "BKE_ipo.h" // eval_icu
82 #include "DNA_space_types.h"
83 }
84
85
86 KX_BlenderSceneConverter::KX_BlenderSceneConverter(
87                                                         struct Main* maggie,
88                                                         class KX_KetsjiEngine* engine
89                                                         )
90                                                         : m_maggie(maggie),
91                                                         m_ketsjiEngine(engine),
92                                                         m_alwaysUseExpandFraming(false),
93                                                         m_usemat(false),
94                                                         m_useglslmat(false)
95 {
96         m_newfilename = "";
97 }
98
99
100 KX_BlenderSceneConverter::~KX_BlenderSceneConverter()
101 {
102         // clears meshes, and hashmaps from blender to gameengine data
103         int i;
104         // delete sumoshapes
105         
106
107         int numAdtLists = m_map_blender_to_gameAdtList.size();
108         for (i=0; i<numAdtLists; i++) {
109                 BL_InterpolatorList *adtList= *m_map_blender_to_gameAdtList.at(i);
110
111                 delete (adtList);
112         }
113
114         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator itw = m_worldinfos.begin();
115         while (itw != m_worldinfos.end()) {
116                 delete (*itw).second;
117                 itw++;
118         }
119
120         vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator itp = m_polymaterials.begin();
121         while (itp != m_polymaterials.end()) {
122                 delete (*itp).second;
123                 itp++;
124         }
125
126         // delete after RAS_IPolyMaterial
127         vector<pair<KX_Scene*,BL_Material *> >::iterator itmat = m_materials.begin();
128         while (itmat != m_materials.end()) {
129                 delete (*itmat).second;
130                 itmat++;
131         }       
132
133
134         vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator itm = m_meshobjects.begin();
135         while (itm != m_meshobjects.end()) {
136                 delete (*itm).second;
137                 itm++;
138         }
139
140 #ifdef USE_BULLET
141         KX_ClearBulletSharedShapes();
142 #endif
143
144 }
145
146
147
148 void KX_BlenderSceneConverter::SetNewFileName(const STR_String& filename)
149 {
150         m_newfilename = filename;
151 }
152
153
154
155 bool KX_BlenderSceneConverter::TryAndLoadNewFile()
156 {
157         bool result = false;
158
159         // find the file
160 /*      if ()
161         {
162                 result = true;
163         }
164         // if not, clear the newfilename
165         else
166         {
167                 m_newfilename = "";     
168         }
169 */
170         return result;
171 }
172
173 Scene *KX_BlenderSceneConverter::GetBlenderSceneForName(const STR_String& name)
174 {
175         Scene *sce;
176
177         /**
178          * Find the specified scene by name, or the first
179          * scene if nothing matches (shouldn't happen).
180          */
181
182         for (sce= (Scene*) m_maggie->scene.first; sce; sce= (Scene*) sce->id.next)
183                 if (name == (sce->id.name+2))
184                         return sce;
185
186         return (Scene*)m_maggie->scene.first;
187
188 }
189 #include "KX_PythonInit.h"
190
191 #ifdef USE_BULLET
192
193 #include "LinearMath/btIDebugDraw.h"
194
195
196 struct  BlenderDebugDraw : public btIDebugDraw
197 {
198         BlenderDebugDraw () :
199                 m_debugMode(0) 
200         {
201         }
202         
203         int m_debugMode;
204
205         virtual void    drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
206         {
207                 if (m_debugMode >0)
208                 {
209                         MT_Vector3 kxfrom(from[0],from[1],from[2]);
210                         MT_Vector3 kxto(to[0],to[1],to[2]);
211                         MT_Vector3 kxcolor(color[0],color[1],color[2]);
212
213                         KX_RasterizerDrawDebugLine(kxfrom,kxto,kxcolor);
214                 }
215         }
216         
217         virtual void    reportErrorWarning(const char* warningString)
218         {
219
220         }
221
222         virtual void    drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,float distance,int lifeTime,const btVector3& color)
223         {
224                 //not yet
225         }
226
227         virtual void    setDebugMode(int debugMode)
228         {
229                 m_debugMode = debugMode;
230         }
231         virtual int             getDebugMode() const
232         {
233                 return m_debugMode;
234         }
235         ///todo: find out if Blender can do this
236         virtual void    draw3dText(const btVector3& location,const char* textString)
237         {
238
239         }
240                 
241 };
242
243 #endif
244
245 void KX_BlenderSceneConverter::ConvertScene(class KX_Scene* destinationscene,
246                                                                                         PyObject* dictobj,
247                                                                                         class RAS_IRenderTools* rendertools,
248                                                                                         class RAS_ICanvas* canvas)
249 {
250         //find out which physics engine
251         Scene *blenderscene = destinationscene->GetBlenderScene();
252
253         e_PhysicsEngine physics_engine = UseBullet;
254         bool useDbvtCulling = false;
255         // hook for registration function during conversion.
256         m_currentScene = destinationscene;
257         destinationscene->SetSceneConverter(this);
258         SG_SetActiveStage(SG_STAGE_CONVERTER);
259
260         if (blenderscene)
261         {
262         
263                 switch (blenderscene->gm.physicsEngine)
264                 {
265                 case WOPHY_BULLET:
266                         {
267                                 physics_engine = UseBullet;
268                                 useDbvtCulling = (blenderscene->gm.mode & WO_DBVT_CULLING) != 0;
269                                 break;
270                         }
271                                                         
272                         case WOPHY_ODE:
273                         {
274                                 physics_engine = UseODE;
275                                 break;
276                         }
277                         case WOPHY_DYNAMO:
278                         {
279                                 physics_engine = UseDynamo;
280                                 break;
281                         }
282                         case WOPHY_SUMO:
283                         {
284                                 physics_engine = UseSumo; 
285                                 break;
286                         }
287                         case WOPHY_NONE:
288                         {
289                                 physics_engine = UseNone;
290                         }
291                 }
292         }
293
294         switch (physics_engine)
295         {
296 #ifdef USE_BULLET
297                 case UseBullet:
298                         {
299                                 CcdPhysicsEnvironment* ccdPhysEnv = new CcdPhysicsEnvironment(useDbvtCulling);
300                                 ccdPhysEnv->setDebugDrawer(new BlenderDebugDraw());
301                                 ccdPhysEnv->setDeactivationLinearTreshold(0.8f); // default, can be overridden by Python
302                                 ccdPhysEnv->setDeactivationAngularTreshold(1.0f); // default, can be overridden by Python
303
304                                 SYS_SystemHandle syshandle = SYS_GetSystem(); /*unused*/
305                                 int visualizePhysics = SYS_GetCommandLineInt(syshandle,"show_physics",0);
306                                 if (visualizePhysics)
307                                         ccdPhysEnv->setDebugMode(btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb|btIDebugDraw::DBG_DrawContactPoints|btIDebugDraw::DBG_DrawText|btIDebugDraw::DBG_DrawConstraintLimits|btIDebugDraw::DBG_DrawConstraints);
308                 
309                                 //todo: get a button in blender ?
310                                 //disable / enable debug drawing (contact points, aabb's etc)   
311                                 //ccdPhysEnv->setDebugMode(1);
312                                 destinationscene->SetPhysicsEnvironment(ccdPhysEnv);
313                                 break;
314                         }
315 #endif  
316                 case UseDynamo:
317                 {
318                 }
319                 
320                 default:
321                 case UseNone:
322                         physics_engine = UseNone;
323                         destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
324                         break;
325         }
326
327         BL_ConvertBlenderObjects(m_maggie,
328                 destinationscene,
329                 m_ketsjiEngine,
330                 physics_engine,
331                 dictobj,
332                 rendertools,
333                 canvas,
334                 this,
335                 m_alwaysUseExpandFraming
336                 );
337
338         //These lookup are not needed during game
339         m_map_blender_to_gameactuator.clear();
340         m_map_blender_to_gamecontroller.clear();
341         m_map_blender_to_gameobject.clear();
342
343         //Clearing this lookup table has the effect of disabling the cache of meshes
344         //between scenes, even if they are shared in the blend file.
345         //This cache mecanism is buggy so I leave it disable and the memory leak
346         //that would result from this is fixed in RemoveScene()
347         m_map_mesh_to_gamemesh.clear();
348         //Don't clear this lookup, it is needed for the baking physics into ipo animation
349         //To avoid it's infinite grows, object will be unregister when they are deleted 
350         //see KX_Scene::NewRemoveObject
351         //m_map_gameobject_to_blender.clear();
352 }
353
354 // This function removes all entities stored in the converter for that scene
355 // It should be used instead of direct delete scene
356 // Note that there was some provision for sharing entities (meshes...) between
357 // scenes but that is now disabled so all scene will have their own copy
358 // and we can delete them here. If the sharing is reactivated, change this code too..
359 // (see KX_BlenderSceneConverter::ConvertScene)
360 void KX_BlenderSceneConverter::RemoveScene(KX_Scene *scene)
361 {
362         int i, size;
363         // delete the scene first as it will stop the use of entities
364         delete scene;
365         // delete the entities of this scene
366         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator worldit;
367         size = m_worldinfos.size();
368         for (i=0, worldit=m_worldinfos.begin(); i<size; ) {
369                 if ((*worldit).first == scene) {
370                         delete (*worldit).second;
371                         *worldit = m_worldinfos.back();
372                         m_worldinfos.pop_back();
373                         size--;
374                 } else {
375                         i++;
376                         worldit++;
377                 }
378         }
379
380         vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator polymit;
381         size = m_polymaterials.size();
382         for (i=0, polymit=m_polymaterials.begin(); i<size; ) {
383                 if ((*polymit).first == scene) {
384                         delete (*polymit).second;
385                         *polymit = m_polymaterials.back();
386                         m_polymaterials.pop_back();
387                         size--;
388                 } else {
389                         i++;
390                         polymit++;
391                 }
392         }
393
394         vector<pair<KX_Scene*,BL_Material*> >::iterator matit;
395         size = m_materials.size();
396         for (i=0, matit=m_materials.begin(); i<size; ) {
397                 if ((*matit).first == scene) {
398                         delete (*matit).second;
399                         *matit = m_materials.back();
400                         m_materials.pop_back();
401                         size--;
402                 } else {
403                         i++;
404                         matit++;
405                 }
406         }
407
408         vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator meshit;
409         size = m_meshobjects.size();
410         for (i=0, meshit=m_meshobjects.begin(); i<size; ) {
411                 if ((*meshit).first == scene) {
412                         delete (*meshit).second;
413                         *meshit = m_meshobjects.back();
414                         m_meshobjects.pop_back();
415                         size--;
416                 } else {
417                         i++;
418                         meshit++;
419                 }
420         }
421 }
422
423 // use blender materials
424 void KX_BlenderSceneConverter::SetMaterials(bool val)
425 {
426         m_usemat = val;
427         m_useglslmat = false;
428 }
429
430 void KX_BlenderSceneConverter::SetGLSLMaterials(bool val)
431 {
432         m_usemat = val;
433         m_useglslmat = val;
434 }
435
436 bool KX_BlenderSceneConverter::GetMaterials()
437 {
438         return m_usemat;
439 }
440
441 bool KX_BlenderSceneConverter::GetGLSLMaterials()
442 {
443         return m_useglslmat;
444 }
445
446 void KX_BlenderSceneConverter::RegisterBlenderMaterial(BL_Material *mat)
447 {
448         m_materials.push_back(pair<KX_Scene*,BL_Material *>(m_currentScene,mat));
449 }
450
451
452
453 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
454         bool to_what)
455 {
456         m_alwaysUseExpandFraming= to_what;
457 }
458
459         
460
461 void KX_BlenderSceneConverter::RegisterGameObject(
462                                                                         KX_GameObject *gameobject, 
463                                                                         struct Object *for_blenderobject) 
464 {
465         m_map_gameobject_to_blender.insert(CHashedPtr(gameobject),for_blenderobject);
466         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
467 }
468
469 void KX_BlenderSceneConverter::UnregisterGameObject(
470                                                                         KX_GameObject *gameobject) 
471 {
472         CHashedPtr gptr(gameobject);
473         struct Object **bobp= m_map_gameobject_to_blender[gptr];
474         if (bobp) {
475                 CHashedPtr bptr(*bobp);
476                 KX_GameObject **gobp= m_map_blender_to_gameobject[bptr];
477                 if (gobp && *gobp == gameobject)
478                         // also maintain m_map_blender_to_gameobject if the gameobject
479                         // being removed is matching the blender object
480                         m_map_blender_to_gameobject.remove(bptr);
481                 m_map_gameobject_to_blender.remove(gptr);
482         }
483 }
484
485
486 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
487                                                                         struct Object *for_blenderobject) 
488 {
489         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
490         
491         return obp?*obp:NULL;
492 }
493
494
495
496 struct Object *KX_BlenderSceneConverter::FindBlenderObject(
497                                                                         KX_GameObject *for_gameobject) 
498 {
499         struct Object **obp= m_map_gameobject_to_blender[CHashedPtr(for_gameobject)];
500         
501         return obp?*obp:NULL;
502 }
503
504         
505
506 void KX_BlenderSceneConverter::RegisterGameMesh(
507                                                                         RAS_MeshObject *gamemesh,
508                                                                         struct Mesh *for_blendermesh)
509 {
510         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
511         m_meshobjects.push_back(pair<KX_Scene*,RAS_MeshObject*>(m_currentScene,gamemesh));
512 }
513
514
515
516 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
517                                                                         struct Mesh *for_blendermesh/*,
518                                                                         unsigned int onlayer*/)
519 {
520         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
521         
522         if (meshp/* && onlayer==(*meshp)->GetLightLayer()*/) {
523                 return *meshp;
524         } else {
525                 return NULL;
526         }
527 }
528
529         
530
531
532         
533
534 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
535 {
536         m_polymaterials.push_back(pair<KX_Scene*,RAS_IPolyMaterial*>(m_currentScene,polymat));
537 }
538
539
540
541 void KX_BlenderSceneConverter::RegisterInterpolatorList(
542                                                                         BL_InterpolatorList *adtList,
543                                                                         struct AnimData *for_adt)
544 {
545         m_map_blender_to_gameAdtList.insert(CHashedPtr(for_adt), adtList);
546 }
547
548
549
550 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
551                                                                         struct AnimData *for_adt)
552 {
553         BL_InterpolatorList **listp = m_map_blender_to_gameAdtList[CHashedPtr(for_adt)];
554                 
555         return listp?*listp:NULL;
556 }
557
558
559
560 void KX_BlenderSceneConverter::RegisterGameActuator(
561                                                                         SCA_IActuator *act,
562                                                                         struct bActuator *for_actuator)
563 {
564         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
565 }
566
567
568
569 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
570                                                                         struct bActuator *for_actuator)
571 {
572         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
573         
574         return actp?*actp:NULL;
575 }
576
577
578
579 void KX_BlenderSceneConverter::RegisterGameController(
580                                                                         SCA_IController *cont,
581                                                                         struct bController *for_controller)
582 {
583         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
584 }
585
586
587
588 SCA_IController *KX_BlenderSceneConverter::FindGameController(
589                                                                         struct bController *for_controller)
590 {
591         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
592         
593         return contp?*contp:NULL;
594 }
595
596
597
598 void KX_BlenderSceneConverter::RegisterWorldInfo(
599                                                                         KX_WorldInfo *worldinfo)
600 {
601         m_worldinfos.push_back(pair<KX_Scene*,KX_WorldInfo*>(m_currentScene,worldinfo));
602 }
603
604 /*
605  * When deleting an IPO curve from Python, check if the IPO is being
606  * edited and if so clear the pointer to the old curve.
607  */
608 void KX_BlenderSceneConverter::localDel_ipoCurve ( IpoCurve * icu )
609 {
610 #if 0 //XXX
611         if (!G.sipo)
612                 return;
613
614         int i;
615         EditIpo *ei= (EditIpo *)G.sipo->editipo;
616         if (!ei) return;
617
618         for(i=0; i<G.sipo->totipo; i++, ei++) {
619                 if ( ei->icu == icu ) {
620                         ei->flag &= ~(IPO_SELECT | IPO_EDIT);
621                         ei->icu= 0;
622                         return;
623                 }
624         }
625 #endif
626 }
627
628 //quick hack
629 extern "C"
630 {
631         Ipo *add_ipo( char *name, int idcode );
632         //XXX char *getIpoCurveName( IpoCurve * icu );
633         //XXX struct IpoCurve *verify_ipocurve(struct ID *, short, char *, char *, char *, int);
634         //XXX void testhandles_ipocurve(struct IpoCurve *icu);
635         void insert_vert_icu(struct IpoCurve *, float, float, short);
636         float eval_icu(struct IpoCurve *icu, float ipotime);
637         //void Mat3ToEul(float tmat[][3], float *eul);
638         void Mat3ToCompatibleEul(float mat[][3], float *eul, float *oldrot);
639 }
640
641 IpoCurve* findIpoCurve(IpoCurve* first, const char* searchName)
642 {
643         IpoCurve* icu1;
644         for( icu1 = first; icu1; icu1 = icu1->next ) 
645         {
646                 /*XXX char* curveName = getIpoCurveName( icu1 );
647                 if( !strcmp( curveName, searchName) )
648                 {
649                         return icu1;
650                 }*/
651         }
652         return 0;
653 }
654
655 // this is not longer necesary //rcruiz
656 /*Ipo* KX_BlenderSceneConverter::findIpoForName(char* objName)
657 {
658         Ipo* ipo_iter = (Ipo*)m_maggie->ipo.first;
659
660         while( ipo_iter )
661         {
662                 if( strcmp( objName, ipo_iter->id.name + 2 ) == 0 ) 
663                 {
664                         return ipo_iter;
665                 }
666                 ipo_iter = (Ipo*)ipo_iter->id.next;
667         }
668         return 0;
669 }
670 */
671
672 void    KX_BlenderSceneConverter::ResetPhysicsObjectsAnimationIpo(bool clearIpo)
673 {
674
675         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
676         int numScenes = scenes->size();
677         int i;
678         for (i=0;i<numScenes;i++)
679         {
680                 KX_Scene* scene = scenes->at(i);
681                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
682                 CListValue* parentList = scene->GetRootParentList();
683                 int numObjects = parentList->GetCount();
684                 int g;
685                 for (g=0;g<numObjects;g++)
686                 {
687                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
688                         if (gameObj->IsDynamic())
689                         {
690                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
691                                 
692                                 Object* blenderObject = FindBlenderObject(gameObj);
693                                 if (blenderObject)
694                                 {
695                                         //erase existing ipo's
696                                         Ipo* ipo = blenderObject->ipo;//findIpoForName(blenderObject->id.name+2);
697                                         if (ipo)
698                                         {       //clear the curve data
699                                                 if (clearIpo){//rcruiz
700                                                         IpoCurve *icu1;
701                                                                                                                 
702                                                         int numCurves = 0;
703                                                         for( icu1 = (IpoCurve*)ipo->curve.first; icu1;  ) {
704                                                         
705                                                                 IpoCurve* tmpicu = icu1;
706                                                                 
707                                                                 /*int i;
708                                                                 BezTriple *bezt;
709                                                                 for( bezt = tmpicu->bezt, i = 0;        i < tmpicu->totvert; i++, bezt++){
710                                                                         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]);
711                                                                 }*/
712                                                                 
713                                                                 icu1 = icu1->next;
714                                                                 numCurves++;
715                         
716                                                                 BLI_remlink( &( blenderObject->ipo->curve ), tmpicu );
717                                                                 if( tmpicu->bezt )
718                                                                         MEM_freeN( tmpicu->bezt );
719                                                                 MEM_freeN( tmpicu );
720                                                                 localDel_ipoCurve( tmpicu );
721                                                         }
722                                                 }
723                                         } else
724                                         {       ipo = NULL; // XXX add_ipo(blenderObject->id.name+2, ID_OB);
725                                                 blenderObject->ipo = ipo;
726
727                                         }
728                                 
729                                         
730
731                                         
732
733                                 }
734                         }
735
736                 }
737                 
738         
739         }
740
741
742
743 }
744
745 void    KX_BlenderSceneConverter::resetNoneDynamicObjectToIpo(){
746         
747         if (addInitFromFrame){          
748                 KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
749                 int numScenes = scenes->size();
750                 if (numScenes>=0){
751                         KX_Scene* scene = scenes->at(0);
752                         CListValue* parentList = scene->GetRootParentList();
753                         for (int ix=0;ix<parentList->GetCount();ix++){
754                                 KX_GameObject* gameobj = (KX_GameObject*)parentList->GetValue(ix);
755                                 if (!gameobj->IsDynamic()){
756                                         Object* blenderobject = FindBlenderObject(gameobj);
757                                         if (!blenderobject)
758                                                 continue;
759                                         if (blenderobject->type==OB_ARMATURE)
760                                                 continue;
761                                         float eu[3];
762                                         Mat4ToEul(blenderobject->obmat,eu);                                     
763                                         MT_Point3 pos = MT_Point3(
764                                                 blenderobject->obmat[3][0],
765                                                 blenderobject->obmat[3][1],
766                                                 blenderobject->obmat[3][2]
767                                         );
768                                         MT_Vector3 eulxyz = MT_Vector3(
769                                                 eu[0],
770                                                 eu[1],
771                                                 eu[2]
772                                         );
773                                         MT_Vector3 scale = MT_Vector3(
774                                                 blenderobject->size[0],
775                                                 blenderobject->size[1],
776                                                 blenderobject->size[2]
777                                         );
778                                         gameobj->NodeSetLocalPosition(pos);
779                                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
780                                         gameobj->NodeSetLocalScale(scale);
781                                         gameobj->NodeUpdateGS(0);
782                                 }
783                         }
784                 }
785         }
786 }
787
788
789         ///this generates ipo curves for position, rotation, allowing to use game physics in animation
790 void    KX_BlenderSceneConverter::WritePhysicsObjectToAnimationIpo(int frameNumber)
791 {
792
793         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
794         int numScenes = scenes->size();
795         int i;
796         for (i=0;i<numScenes;i++)
797         {
798                 KX_Scene* scene = scenes->at(i);
799                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
800                 CListValue* parentList = scene->GetRootParentList();
801                 int numObjects = parentList->GetCount();
802                 int g;
803                 for (g=0;g<numObjects;g++)
804                 {
805                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
806                         if (gameObj->IsDynamic())
807                         {
808                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
809                                 
810                                 Object* blenderObject = FindBlenderObject(gameObj);
811                                 if (blenderObject && blenderObject->ipo)
812                                 {
813                                         const MT_Point3& position = gameObj->NodeGetWorldPosition();
814                                         //const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
815                                         const MT_Matrix3x3& orn = gameObj->NodeGetWorldOrientation();
816                                         
817                                         float eulerAngles[3];   
818                                         float eulerAnglesOld[3] = {0.0f, 0.0f, 0.0f};                                           
819                                         float tmat[3][3];
820                                         
821                                         // XXX animato
822 #if 0
823                                         Ipo* ipo = blenderObject->ipo;
824
825                                         //create the curves, if not existing, set linear if new
826
827                                         IpoCurve *icu_lx = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
828                                         if (!icu_lx) {
829                                                 icu_lx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_X, 1);
830                                                 if(icu_lx) icu_lx->ipo = IPO_LIN;
831                                         }
832                                         IpoCurve *icu_ly = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
833                                         if (!icu_ly) {
834                                                 icu_ly = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Y, 1);
835                                                 if(icu_ly) icu_ly->ipo = IPO_LIN;
836                                         }
837                                         IpoCurve *icu_lz = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
838                                         if (!icu_lz) {
839                                                 icu_lz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Z, 1);
840                                                 if(icu_lz) icu_lz->ipo = IPO_LIN;
841                                         }
842                                         IpoCurve *icu_rx = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
843                                         if (!icu_rx) {
844                                                 icu_rx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_X, 1);
845                                                 if(icu_rx) icu_rx->ipo = IPO_LIN;
846                                         }
847                                         IpoCurve *icu_ry = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
848                                         if (!icu_ry) {
849                                                 icu_ry = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Y, 1);
850                                                 if(icu_ry) icu_ry->ipo = IPO_LIN;
851                                         }
852                                         IpoCurve *icu_rz = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
853                                         if (!icu_rz) {
854                                                 icu_rz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Z, 1);
855                                                 if(icu_rz) icu_rz->ipo = IPO_LIN;
856                                         }
857                                         
858                                         if(icu_rx) eulerAnglesOld[0]= eval_icu( icu_rx, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
859                                         if(icu_ry) eulerAnglesOld[1]= eval_icu( icu_ry, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
860                                         if(icu_rz) eulerAnglesOld[2]= eval_icu( icu_rz, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
861                                         
862                                         // orn.getValue((float *)tmat); // uses the wrong ordering, cant use this
863                                         for (int r=0;r<3;r++)
864                                                 for (int c=0;c<3;c++)
865                                                         tmat[r][c] = orn[c][r];
866                                         
867                                         // Mat3ToEul(tmat, eulerAngles); // better to use Mat3ToCompatibleEul
868                                         Mat3ToCompatibleEul(tmat, eulerAngles, eulerAnglesOld);
869                                         
870                                         //eval_icu
871                                         for(int x = 0; x < 3; x++)
872                                                 eulerAngles[x] *= (float) ((180 / 3.14159265f) / 10.0);
873                                         
874                                         //fill the curves with data
875                                         if (icu_lx) insert_vert_icu(icu_lx, frameNumber, position.x(), 1);
876                                         if (icu_ly) insert_vert_icu(icu_ly, frameNumber, position.y(), 1);
877                                         if (icu_lz) insert_vert_icu(icu_lz, frameNumber, position.z(), 1);
878                                         if (icu_rx) insert_vert_icu(icu_rx, frameNumber, eulerAngles[0], 1);
879                                         if (icu_ry) insert_vert_icu(icu_ry, frameNumber, eulerAngles[1], 1);
880                                         if (icu_rz) insert_vert_icu(icu_rz, frameNumber, eulerAngles[2], 1);
881                                         
882                                         // Handles are corrected at the end, testhandles_ipocurve isnt needed yet
883 #endif
884                                 }
885                         }
886                 }
887         }
888 }
889
890
891 void    KX_BlenderSceneConverter::TestHandlesPhysicsObjectToAnimationIpo()
892 {
893
894         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
895         int numScenes = scenes->size();
896         int i;
897         for (i=0;i<numScenes;i++)
898         {
899                 KX_Scene* scene = scenes->at(i);
900                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
901                 CListValue* parentList = scene->GetRootParentList();
902                 int numObjects = parentList->GetCount();
903                 int g;
904                 for (g=0;g<numObjects;g++)
905                 {
906                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
907                         if (gameObj->IsDynamic())
908                         {
909                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
910                                 
911                                 Object* blenderObject = FindBlenderObject(gameObj);
912                                 if (blenderObject && blenderObject->ipo)
913                                 {
914                                         // XXX animato
915 #if 0
916                                         Ipo* ipo = blenderObject->ipo;
917                                         
918                                         //create the curves, if not existing
919                                         //testhandles_ipocurve checks for NULL
920                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocX"));
921                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocY"));
922                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ"));
923                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotX"));
924                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotY"));
925                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ"));
926 #endif
927                                 }
928                         }
929
930                 }
931                 
932         
933         }
934
935
936
937 }