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