svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r20849:20855
[blender-staging.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 //XXX #include "BSE_editipo.h"
87 //XXX #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 0 //XXX
645         if (!G.sipo)
646                 return;
647
648         int i;
649         EditIpo *ei= (EditIpo *)G.sipo->editipo;
650         if (!ei) return;
651
652         for(i=0; i<G.sipo->totipo; i++, ei++) {
653                 if ( ei->icu == icu ) {
654                         ei->flag &= ~(IPO_SELECT | IPO_EDIT);
655                         ei->icu= 0;
656                         return;
657                 }
658         }
659 #endif
660 }
661
662 //quick hack
663 extern "C"
664 {
665         Ipo *add_ipo( char *name, int idcode );
666         //XXX char *getIpoCurveName( IpoCurve * icu );
667         //XXX struct IpoCurve *verify_ipocurve(struct ID *, short, char *, char *, char *, int);
668         //XXX void testhandles_ipocurve(struct IpoCurve *icu);
669         void insert_vert_icu(struct IpoCurve *, float, float, short);
670         float eval_icu(struct IpoCurve *icu, float ipotime);
671         //void Mat3ToEul(float tmat[][3], float *eul);
672         void Mat3ToCompatibleEul(float mat[][3], float *eul, float *oldrot);
673 }
674
675 IpoCurve* findIpoCurve(IpoCurve* first, const char* searchName)
676 {
677         IpoCurve* icu1;
678         for( icu1 = first; icu1; icu1 = icu1->next ) 
679         {
680                 /*XXX char* curveName = getIpoCurveName( icu1 );
681                 if( !strcmp( curveName, searchName) )
682                 {
683                         return icu1;
684                 }*/
685         }
686         return 0;
687 }
688
689 // this is not longer necesary //rcruiz
690 /*Ipo* KX_BlenderSceneConverter::findIpoForName(char* objName)
691 {
692         Ipo* ipo_iter = (Ipo*)m_maggie->ipo.first;
693
694         while( ipo_iter )
695         {
696                 if( strcmp( objName, ipo_iter->id.name + 2 ) == 0 ) 
697                 {
698                         return ipo_iter;
699                 }
700                 ipo_iter = (Ipo*)ipo_iter->id.next;
701         }
702         return 0;
703 }
704 */
705
706 void    KX_BlenderSceneConverter::ResetPhysicsObjectsAnimationIpo(bool clearIpo)
707 {
708
709         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
710         int numScenes = scenes->size();
711         int i;
712         for (i=0;i<numScenes;i++)
713         {
714                 KX_Scene* scene = scenes->at(i);
715                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
716                 CListValue* parentList = scene->GetRootParentList();
717                 int numObjects = parentList->GetCount();
718                 int g;
719                 for (g=0;g<numObjects;g++)
720                 {
721                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
722                         if (gameObj->IsDynamic())
723                         {
724                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
725                                 
726                                 Object* blenderObject = FindBlenderObject(gameObj);
727                                 if (blenderObject)
728                                 {
729                                         //erase existing ipo's
730                                         Ipo* ipo = blenderObject->ipo;//findIpoForName(blenderObject->id.name+2);
731                                         if (ipo)
732                                         {       //clear the curve data
733                                                 if (clearIpo){//rcruiz
734                                                         IpoCurve *icu1;
735                                                                                                                 
736                                                         int numCurves = 0;
737                                                         for( icu1 = (IpoCurve*)ipo->curve.first; icu1;  ) {
738                                                         
739                                                                 IpoCurve* tmpicu = icu1;
740                                                                 
741                                                                 /*int i;
742                                                                 BezTriple *bezt;
743                                                                 for( bezt = tmpicu->bezt, i = 0;        i < tmpicu->totvert; i++, bezt++){
744                                                                         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]);
745                                                                 }*/
746                                                                 
747                                                                 icu1 = icu1->next;
748                                                                 numCurves++;
749                         
750                                                                 BLI_remlink( &( blenderObject->ipo->curve ), tmpicu );
751                                                                 if( tmpicu->bezt )
752                                                                         MEM_freeN( tmpicu->bezt );
753                                                                 MEM_freeN( tmpicu );
754                                                                 localDel_ipoCurve( tmpicu );
755                                                         }
756                                                 }
757                                         } else
758                                         {       ipo = NULL; // XXX add_ipo(blenderObject->id.name+2, ID_OB);
759                                                 blenderObject->ipo = ipo;
760
761                                         }
762                                 
763                                         
764
765                                         
766
767                                 }
768                         }
769
770                 }
771                 
772         
773         }
774
775
776
777 }
778
779 void    KX_BlenderSceneConverter::resetNoneDynamicObjectToIpo(){
780         
781         if (addInitFromFrame){          
782                 KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
783                 int numScenes = scenes->size();
784                 if (numScenes>=0){
785                         KX_Scene* scene = scenes->at(0);
786                         CListValue* parentList = scene->GetRootParentList();
787                         for (int ix=0;ix<parentList->GetCount();ix++){
788                                 KX_GameObject* gameobj = (KX_GameObject*)parentList->GetValue(ix);
789                                 if (!gameobj->IsDynamic()){
790                                         Object* blenderobject = FindBlenderObject(gameobj);
791                                         if (!blenderobject)
792                                                 continue;
793                                         if (blenderobject->type==OB_ARMATURE)
794                                                 continue;
795                                         float eu[3];
796                                         Mat4ToEul(blenderobject->obmat,eu);                                     
797                                         MT_Point3 pos = MT_Point3(
798                                                 blenderobject->obmat[3][0],
799                                                 blenderobject->obmat[3][1],
800                                                 blenderobject->obmat[3][2]
801                                         );
802                                         MT_Vector3 eulxyz = MT_Vector3(
803                                                 eu[0],
804                                                 eu[1],
805                                                 eu[2]
806                                         );
807                                         MT_Vector3 scale = MT_Vector3(
808                                                 blenderobject->size[0],
809                                                 blenderobject->size[1],
810                                                 blenderobject->size[2]
811                                         );
812                                         gameobj->NodeSetLocalPosition(pos);
813                                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
814                                         gameobj->NodeSetLocalScale(scale);
815                                         gameobj->NodeUpdateGS(0);
816                                 }
817                         }
818                 }
819         }
820 }
821
822
823         ///this generates ipo curves for position, rotation, allowing to use game physics in animation
824 void    KX_BlenderSceneConverter::WritePhysicsObjectToAnimationIpo(int frameNumber)
825 {
826
827         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
828         int numScenes = scenes->size();
829         int i;
830         for (i=0;i<numScenes;i++)
831         {
832                 KX_Scene* scene = scenes->at(i);
833                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
834                 CListValue* parentList = scene->GetRootParentList();
835                 int numObjects = parentList->GetCount();
836                 int g;
837                 for (g=0;g<numObjects;g++)
838                 {
839                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
840                         if (gameObj->IsDynamic())
841                         {
842                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
843                                 
844                                 Object* blenderObject = FindBlenderObject(gameObj);
845                                 if (blenderObject && blenderObject->ipo)
846                                 {
847                                         const MT_Point3& position = gameObj->NodeGetWorldPosition();
848                                         //const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
849                                         const MT_Matrix3x3& orn = gameObj->NodeGetWorldOrientation();
850                                         
851                                         float eulerAngles[3];   
852                                         float eulerAnglesOld[3] = {0.0f, 0.0f, 0.0f};                                           
853                                         float tmat[3][3];
854                                         
855                                         // XXX animato
856 #if 0
857                                         Ipo* ipo = blenderObject->ipo;
858
859                                         //create the curves, if not existing, set linear if new
860
861                                         IpoCurve *icu_lx = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
862                                         if (!icu_lx) {
863                                                 icu_lx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_X, 1);
864                                                 if(icu_lx) icu_lx->ipo = IPO_LIN;
865                                         }
866                                         IpoCurve *icu_ly = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
867                                         if (!icu_ly) {
868                                                 icu_ly = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Y, 1);
869                                                 if(icu_ly) icu_ly->ipo = IPO_LIN;
870                                         }
871                                         IpoCurve *icu_lz = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
872                                         if (!icu_lz) {
873                                                 icu_lz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Z, 1);
874                                                 if(icu_lz) icu_lz->ipo = IPO_LIN;
875                                         }
876                                         IpoCurve *icu_rx = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
877                                         if (!icu_rx) {
878                                                 icu_rx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_X, 1);
879                                                 if(icu_rx) icu_rx->ipo = IPO_LIN;
880                                         }
881                                         IpoCurve *icu_ry = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
882                                         if (!icu_ry) {
883                                                 icu_ry = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Y, 1);
884                                                 if(icu_ry) icu_ry->ipo = IPO_LIN;
885                                         }
886                                         IpoCurve *icu_rz = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
887                                         if (!icu_rz) {
888                                                 icu_rz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Z, 1);
889                                                 if(icu_rz) icu_rz->ipo = IPO_LIN;
890                                         }
891                                         
892                                         if(icu_rx) eulerAnglesOld[0]= eval_icu( icu_rx, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
893                                         if(icu_ry) eulerAnglesOld[1]= eval_icu( icu_ry, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
894                                         if(icu_rz) eulerAnglesOld[2]= eval_icu( icu_rz, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
895                                         
896                                         // orn.getValue((float *)tmat); // uses the wrong ordering, cant use this
897                                         for (int r=0;r<3;r++)
898                                                 for (int c=0;c<3;c++)
899                                                         tmat[r][c] = orn[c][r];
900                                         
901                                         // Mat3ToEul(tmat, eulerAngles); // better to use Mat3ToCompatibleEul
902                                         Mat3ToCompatibleEul(tmat, eulerAngles, eulerAnglesOld);
903                                         
904                                         //eval_icu
905                                         for(int x = 0; x < 3; x++)
906                                                 eulerAngles[x] *= (float) ((180 / 3.14159265f) / 10.0);
907                                         
908                                         //fill the curves with data
909                                         if (icu_lx) insert_vert_icu(icu_lx, frameNumber, position.x(), 1);
910                                         if (icu_ly) insert_vert_icu(icu_ly, frameNumber, position.y(), 1);
911                                         if (icu_lz) insert_vert_icu(icu_lz, frameNumber, position.z(), 1);
912                                         if (icu_rx) insert_vert_icu(icu_rx, frameNumber, eulerAngles[0], 1);
913                                         if (icu_ry) insert_vert_icu(icu_ry, frameNumber, eulerAngles[1], 1);
914                                         if (icu_rz) insert_vert_icu(icu_rz, frameNumber, eulerAngles[2], 1);
915                                         
916                                         // Handles are corrected at the end, testhandles_ipocurve isnt needed yet
917 #endif
918                                 }
919                         }
920                 }
921         }
922 }
923
924
925 void    KX_BlenderSceneConverter::TestHandlesPhysicsObjectToAnimationIpo()
926 {
927
928         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
929         int numScenes = scenes->size();
930         int i;
931         for (i=0;i<numScenes;i++)
932         {
933                 KX_Scene* scene = scenes->at(i);
934                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
935                 CListValue* parentList = scene->GetRootParentList();
936                 int numObjects = parentList->GetCount();
937                 int g;
938                 for (g=0;g<numObjects;g++)
939                 {
940                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
941                         if (gameObj->IsDynamic())
942                         {
943                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
944                                 
945                                 Object* blenderObject = FindBlenderObject(gameObj);
946                                 if (blenderObject && blenderObject->ipo)
947                                 {
948                                         // XXX animato
949 #if 0
950                                         Ipo* ipo = blenderObject->ipo;
951                                         
952                                         //create the curves, if not existing
953                                         //testhandles_ipocurve checks for NULL
954                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocX"));
955                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocY"));
956                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ"));
957                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotX"));
958                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotY"));
959                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ"));
960 #endif
961                                 }
962                         }
963
964                 }
965                 
966         
967         }
968
969
970
971 }