- rather then passing the python namespace dictionary to the controller function...
[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 #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                                                                                         class RAS_IRenderTools* rendertools,
247                                                                                         class RAS_ICanvas* canvas)
248 {
249         //find out which physics engine
250         Scene *blenderscene = destinationscene->GetBlenderScene();
251
252         e_PhysicsEngine physics_engine = UseBullet;
253         bool useDbvtCulling = false;
254         // hook for registration function during conversion.
255         m_currentScene = destinationscene;
256         destinationscene->SetSceneConverter(this);
257         SG_SetActiveStage(SG_STAGE_CONVERTER);
258
259         if (blenderscene)
260         {
261         
262                 switch (blenderscene->gm.physicsEngine)
263                 {
264                 case WOPHY_BULLET:
265                         {
266                                 physics_engine = UseBullet;
267                                 useDbvtCulling = (blenderscene->gm.mode & WO_DBVT_CULLING) != 0;
268                                 break;
269                         }
270                                                         
271                         case WOPHY_ODE:
272                         {
273                                 physics_engine = UseODE;
274                                 break;
275                         }
276                         case WOPHY_DYNAMO:
277                         {
278                                 physics_engine = UseDynamo;
279                                 break;
280                         }
281                         case WOPHY_SUMO:
282                         {
283                                 physics_engine = UseSumo; 
284                                 break;
285                         }
286                         case WOPHY_NONE:
287                         {
288                                 physics_engine = UseNone;
289                         }
290                 }
291         }
292
293         switch (physics_engine)
294         {
295 #ifdef USE_BULLET
296                 case UseBullet:
297                         {
298                                 CcdPhysicsEnvironment* ccdPhysEnv = new CcdPhysicsEnvironment(useDbvtCulling);
299                                 ccdPhysEnv->setDebugDrawer(new BlenderDebugDraw());
300                                 ccdPhysEnv->setDeactivationLinearTreshold(0.8f); // default, can be overridden by Python
301                                 ccdPhysEnv->setDeactivationAngularTreshold(1.0f); // default, can be overridden by Python
302
303                                 SYS_SystemHandle syshandle = SYS_GetSystem(); /*unused*/
304                                 int visualizePhysics = SYS_GetCommandLineInt(syshandle,"show_physics",0);
305                                 if (visualizePhysics)
306                                         ccdPhysEnv->setDebugMode(btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb|btIDebugDraw::DBG_DrawContactPoints|btIDebugDraw::DBG_DrawText|btIDebugDraw::DBG_DrawConstraintLimits|btIDebugDraw::DBG_DrawConstraints);
307                 
308                                 //todo: get a button in blender ?
309                                 //disable / enable debug drawing (contact points, aabb's etc)   
310                                 //ccdPhysEnv->setDebugMode(1);
311                                 destinationscene->SetPhysicsEnvironment(ccdPhysEnv);
312                                 break;
313                         }
314 #endif  
315                 case UseDynamo:
316                 {
317                 }
318                 
319                 default:
320                 case UseNone:
321                         physics_engine = UseNone;
322                         destinationscene ->SetPhysicsEnvironment(new DummyPhysicsEnvironment());
323                         break;
324         }
325
326         BL_ConvertBlenderObjects(m_maggie,
327                 destinationscene,
328                 m_ketsjiEngine,
329                 physics_engine,
330                 rendertools,
331                 canvas,
332                 this,
333                 m_alwaysUseExpandFraming
334                 );
335
336         //These lookup are not needed during game
337         m_map_blender_to_gameactuator.clear();
338         m_map_blender_to_gamecontroller.clear();
339         m_map_blender_to_gameobject.clear();
340
341         //Clearing this lookup table has the effect of disabling the cache of meshes
342         //between scenes, even if they are shared in the blend file.
343         //This cache mecanism is buggy so I leave it disable and the memory leak
344         //that would result from this is fixed in RemoveScene()
345         m_map_mesh_to_gamemesh.clear();
346 }
347
348 // This function removes all entities stored in the converter for that scene
349 // It should be used instead of direct delete scene
350 // Note that there was some provision for sharing entities (meshes...) between
351 // scenes but that is now disabled so all scene will have their own copy
352 // and we can delete them here. If the sharing is reactivated, change this code too..
353 // (see KX_BlenderSceneConverter::ConvertScene)
354 void KX_BlenderSceneConverter::RemoveScene(KX_Scene *scene)
355 {
356         int i, size;
357         // delete the scene first as it will stop the use of entities
358         delete scene;
359         // delete the entities of this scene
360         vector<pair<KX_Scene*,KX_WorldInfo*> >::iterator worldit;
361         size = m_worldinfos.size();
362         for (i=0, worldit=m_worldinfos.begin(); i<size; ) {
363                 if ((*worldit).first == scene) {
364                         delete (*worldit).second;
365                         *worldit = m_worldinfos.back();
366                         m_worldinfos.pop_back();
367                         size--;
368                 } else {
369                         i++;
370                         worldit++;
371                 }
372         }
373
374         vector<pair<KX_Scene*,RAS_IPolyMaterial*> >::iterator polymit;
375         size = m_polymaterials.size();
376         for (i=0, polymit=m_polymaterials.begin(); i<size; ) {
377                 if ((*polymit).first == scene) {
378                         delete (*polymit).second;
379                         *polymit = m_polymaterials.back();
380                         m_polymaterials.pop_back();
381                         size--;
382                 } else {
383                         i++;
384                         polymit++;
385                 }
386         }
387
388         vector<pair<KX_Scene*,BL_Material*> >::iterator matit;
389         size = m_materials.size();
390         for (i=0, matit=m_materials.begin(); i<size; ) {
391                 if ((*matit).first == scene) {
392                         delete (*matit).second;
393                         *matit = m_materials.back();
394                         m_materials.pop_back();
395                         size--;
396                 } else {
397                         i++;
398                         matit++;
399                 }
400         }
401
402         vector<pair<KX_Scene*,RAS_MeshObject*> >::iterator meshit;
403         size = m_meshobjects.size();
404         for (i=0, meshit=m_meshobjects.begin(); i<size; ) {
405                 if ((*meshit).first == scene) {
406                         delete (*meshit).second;
407                         *meshit = m_meshobjects.back();
408                         m_meshobjects.pop_back();
409                         size--;
410                 } else {
411                         i++;
412                         meshit++;
413                 }
414         }
415 }
416
417 // use blender materials
418 void KX_BlenderSceneConverter::SetMaterials(bool val)
419 {
420         m_usemat = val;
421         m_useglslmat = false;
422 }
423
424 void KX_BlenderSceneConverter::SetGLSLMaterials(bool val)
425 {
426         m_usemat = val;
427         m_useglslmat = val;
428 }
429
430 bool KX_BlenderSceneConverter::GetMaterials()
431 {
432         return m_usemat;
433 }
434
435 bool KX_BlenderSceneConverter::GetGLSLMaterials()
436 {
437         return m_useglslmat;
438 }
439
440 void KX_BlenderSceneConverter::RegisterBlenderMaterial(BL_Material *mat)
441 {
442         m_materials.push_back(pair<KX_Scene*,BL_Material *>(m_currentScene,mat));
443 }
444
445
446
447 void KX_BlenderSceneConverter::SetAlwaysUseExpandFraming(
448         bool to_what)
449 {
450         m_alwaysUseExpandFraming= to_what;
451 }
452
453         
454
455 void KX_BlenderSceneConverter::RegisterGameObject(
456                                                                         KX_GameObject *gameobject, 
457                                                                         struct Object *for_blenderobject) 
458 {
459         /* only maintained while converting, freed during game runtime */
460         m_map_blender_to_gameobject.insert(CHashedPtr(for_blenderobject),gameobject);
461 }
462
463 /* only need to run this during conversion since
464  * m_map_blender_to_gameobject is freed after conversion */
465 void KX_BlenderSceneConverter::UnregisterGameObject(
466                                                                         KX_GameObject *gameobject) 
467 {
468         struct Object *bobp= gameobject->GetBlenderObject();
469         if (bobp) {
470                 CHashedPtr bptr(bobp);
471                 KX_GameObject **gobp= m_map_blender_to_gameobject[bptr];
472                 if (gobp && *gobp == gameobject)
473                 {
474                         // also maintain m_map_blender_to_gameobject if the gameobject
475                         // being removed is matching the blender object
476                         m_map_blender_to_gameobject.remove(bptr);
477                 }
478         }
479 }
480
481 KX_GameObject *KX_BlenderSceneConverter::FindGameObject(
482                                                                         struct Object *for_blenderobject) 
483 {
484         KX_GameObject **obp= m_map_blender_to_gameobject[CHashedPtr(for_blenderobject)];
485         
486         return obp?*obp:NULL;
487 }
488
489 void KX_BlenderSceneConverter::RegisterGameMesh(
490                                                                         RAS_MeshObject *gamemesh,
491                                                                         struct Mesh *for_blendermesh)
492 {
493         m_map_mesh_to_gamemesh.insert(CHashedPtr(for_blendermesh),gamemesh);
494         m_meshobjects.push_back(pair<KX_Scene*,RAS_MeshObject*>(m_currentScene,gamemesh));
495 }
496
497
498
499 RAS_MeshObject *KX_BlenderSceneConverter::FindGameMesh(
500                                                                         struct Mesh *for_blendermesh/*,
501                                                                         unsigned int onlayer*/)
502 {
503         RAS_MeshObject** meshp = m_map_mesh_to_gamemesh[CHashedPtr(for_blendermesh)];
504         
505         if (meshp/* && onlayer==(*meshp)->GetLightLayer()*/) {
506                 return *meshp;
507         } else {
508                 return NULL;
509         }
510 }
511
512         
513
514
515         
516
517 void KX_BlenderSceneConverter::RegisterPolyMaterial(RAS_IPolyMaterial *polymat)
518 {
519         m_polymaterials.push_back(pair<KX_Scene*,RAS_IPolyMaterial*>(m_currentScene,polymat));
520 }
521
522
523
524 void KX_BlenderSceneConverter::RegisterInterpolatorList(
525                                                                         BL_InterpolatorList *adtList,
526                                                                         struct AnimData *for_adt)
527 {
528         m_map_blender_to_gameAdtList.insert(CHashedPtr(for_adt), adtList);
529 }
530
531
532
533 BL_InterpolatorList *KX_BlenderSceneConverter::FindInterpolatorList(
534                                                                         struct AnimData *for_adt)
535 {
536         BL_InterpolatorList **listp = m_map_blender_to_gameAdtList[CHashedPtr(for_adt)];
537                 
538         return listp?*listp:NULL;
539 }
540
541
542
543 void KX_BlenderSceneConverter::RegisterGameActuator(
544                                                                         SCA_IActuator *act,
545                                                                         struct bActuator *for_actuator)
546 {
547         m_map_blender_to_gameactuator.insert(CHashedPtr(for_actuator), act);
548 }
549
550
551
552 SCA_IActuator *KX_BlenderSceneConverter::FindGameActuator(
553                                                                         struct bActuator *for_actuator)
554 {
555         SCA_IActuator **actp = m_map_blender_to_gameactuator[CHashedPtr(for_actuator)];
556         
557         return actp?*actp:NULL;
558 }
559
560
561
562 void KX_BlenderSceneConverter::RegisterGameController(
563                                                                         SCA_IController *cont,
564                                                                         struct bController *for_controller)
565 {
566         m_map_blender_to_gamecontroller.insert(CHashedPtr(for_controller), cont);
567 }
568
569
570
571 SCA_IController *KX_BlenderSceneConverter::FindGameController(
572                                                                         struct bController *for_controller)
573 {
574         SCA_IController **contp = m_map_blender_to_gamecontroller[CHashedPtr(for_controller)];
575         
576         return contp?*contp:NULL;
577 }
578
579
580
581 void KX_BlenderSceneConverter::RegisterWorldInfo(
582                                                                         KX_WorldInfo *worldinfo)
583 {
584         m_worldinfos.push_back(pair<KX_Scene*,KX_WorldInfo*>(m_currentScene,worldinfo));
585 }
586
587 /*
588  * When deleting an IPO curve from Python, check if the IPO is being
589  * edited and if so clear the pointer to the old curve.
590  */
591 void KX_BlenderSceneConverter::localDel_ipoCurve ( IpoCurve * icu )
592 {
593 #if 0 //XXX
594         if (!G.sipo)
595                 return;
596
597         int i;
598         EditIpo *ei= (EditIpo *)G.sipo->editipo;
599         if (!ei) return;
600
601         for(i=0; i<G.sipo->totipo; i++, ei++) {
602                 if ( ei->icu == icu ) {
603                         ei->flag &= ~(IPO_SELECT | IPO_EDIT);
604                         ei->icu= 0;
605                         return;
606                 }
607         }
608 #endif
609 }
610
611 //quick hack
612 extern "C"
613 {
614         Ipo *add_ipo( char *name, int idcode );
615         //XXX char *getIpoCurveName( IpoCurve * icu );
616         //XXX struct IpoCurve *verify_ipocurve(struct ID *, short, char *, char *, char *, int);
617         //XXX void testhandles_ipocurve(struct IpoCurve *icu);
618         void insert_vert_icu(struct IpoCurve *, float, float, short);
619         float eval_icu(struct IpoCurve *icu, float ipotime);
620         //void Mat3ToEul(float tmat[][3], float *eul);
621         void Mat3ToCompatibleEul(float mat[][3], float *eul, float *oldrot);
622 }
623
624 IpoCurve* findIpoCurve(IpoCurve* first, const char* searchName)
625 {
626         IpoCurve* icu1;
627         for( icu1 = first; icu1; icu1 = icu1->next ) 
628         {
629                 /*XXX char* curveName = getIpoCurveName( icu1 );
630                 if( !strcmp( curveName, searchName) )
631                 {
632                         return icu1;
633                 }*/
634         }
635         return 0;
636 }
637
638 // this is not longer necesary //rcruiz
639 /*Ipo* KX_BlenderSceneConverter::findIpoForName(char* objName)
640 {
641         Ipo* ipo_iter = (Ipo*)m_maggie->ipo.first;
642
643         while( ipo_iter )
644         {
645                 if( strcmp( objName, ipo_iter->id.name + 2 ) == 0 ) 
646                 {
647                         return ipo_iter;
648                 }
649                 ipo_iter = (Ipo*)ipo_iter->id.next;
650         }
651         return 0;
652 }
653 */
654
655 void    KX_BlenderSceneConverter::ResetPhysicsObjectsAnimationIpo(bool clearIpo)
656 {
657
658         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
659         int numScenes = scenes->size();
660         int i;
661         for (i=0;i<numScenes;i++)
662         {
663                 KX_Scene* scene = scenes->at(i);
664                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
665                 CListValue* parentList = scene->GetRootParentList();
666                 int numObjects = parentList->GetCount();
667                 int g;
668                 for (g=0;g<numObjects;g++)
669                 {
670                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
671                         if (gameObj->IsDynamic())
672                         {
673                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
674                                 
675                                 Object* blenderObject = gameObj->GetBlenderObject();
676                                 if (blenderObject)
677                                 {
678                                         //erase existing ipo's
679                                         Ipo* ipo = blenderObject->ipo;//findIpoForName(blenderObject->id.name+2);
680                                         if (ipo)
681                                         {       //clear the curve data
682                                                 if (clearIpo){//rcruiz
683                                                         IpoCurve *icu1;
684                                                                                                                 
685                                                         int numCurves = 0;
686                                                         for( icu1 = (IpoCurve*)ipo->curve.first; icu1;  ) {
687                                                         
688                                                                 IpoCurve* tmpicu = icu1;
689                                                                 
690                                                                 /*int i;
691                                                                 BezTriple *bezt;
692                                                                 for( bezt = tmpicu->bezt, i = 0;        i < tmpicu->totvert; i++, bezt++){
693                                                                         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]);
694                                                                 }*/
695                                                                 
696                                                                 icu1 = icu1->next;
697                                                                 numCurves++;
698                         
699                                                                 BLI_remlink( &( blenderObject->ipo->curve ), tmpicu );
700                                                                 if( tmpicu->bezt )
701                                                                         MEM_freeN( tmpicu->bezt );
702                                                                 MEM_freeN( tmpicu );
703                                                                 localDel_ipoCurve( tmpicu );
704                                                         }
705                                                 }
706                                         } else
707                                         {       ipo = NULL; // XXX add_ipo(blenderObject->id.name+2, ID_OB);
708                                                 blenderObject->ipo = ipo;
709
710                                         }
711                                 
712                                         
713
714                                         
715
716                                 }
717                         }
718
719                 }
720                 
721         
722         }
723
724
725
726 }
727
728 void    KX_BlenderSceneConverter::resetNoneDynamicObjectToIpo(){
729         
730         if (addInitFromFrame){          
731                 KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
732                 int numScenes = scenes->size();
733                 if (numScenes>=0){
734                         KX_Scene* scene = scenes->at(0);
735                         CListValue* parentList = scene->GetRootParentList();
736                         for (int ix=0;ix<parentList->GetCount();ix++){
737                                 KX_GameObject* gameobj = (KX_GameObject*)parentList->GetValue(ix);
738                                 if (!gameobj->IsDynamic()){
739                                         Object* blenderobject = gameobj->GetBlenderObject();
740                                         if (!blenderobject)
741                                                 continue;
742                                         if (blenderobject->type==OB_ARMATURE)
743                                                 continue;
744                                         float eu[3];
745                                         Mat4ToEul(blenderobject->obmat,eu);                                     
746                                         MT_Point3 pos = MT_Point3(
747                                                 blenderobject->obmat[3][0],
748                                                 blenderobject->obmat[3][1],
749                                                 blenderobject->obmat[3][2]
750                                         );
751                                         MT_Vector3 eulxyz = MT_Vector3(
752                                                 eu[0],
753                                                 eu[1],
754                                                 eu[2]
755                                         );
756                                         MT_Vector3 scale = MT_Vector3(
757                                                 blenderobject->size[0],
758                                                 blenderobject->size[1],
759                                                 blenderobject->size[2]
760                                         );
761                                         gameobj->NodeSetLocalPosition(pos);
762                                         gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
763                                         gameobj->NodeSetLocalScale(scale);
764                                         gameobj->NodeUpdateGS(0);
765                                 }
766                         }
767                 }
768         }
769 }
770
771
772         ///this generates ipo curves for position, rotation, allowing to use game physics in animation
773 void    KX_BlenderSceneConverter::WritePhysicsObjectToAnimationIpo(int frameNumber)
774 {
775
776         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
777         int numScenes = scenes->size();
778         int i;
779         for (i=0;i<numScenes;i++)
780         {
781                 KX_Scene* scene = scenes->at(i);
782                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
783                 CListValue* parentList = scene->GetRootParentList();
784                 int numObjects = parentList->GetCount();
785                 int g;
786                 for (g=0;g<numObjects;g++)
787                 {
788                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
789                         if (gameObj->IsDynamic())
790                         {
791                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
792                                 
793                                 Object* blenderObject = gameObj->GetBlenderObject();
794                                 if (blenderObject && blenderObject->ipo)
795                                 {
796 #if 0
797                                         const MT_Point3& position = gameObj->NodeGetWorldPosition();
798                                         //const MT_Vector3& scale = gameObj->NodeGetWorldScaling();
799                                         const MT_Matrix3x3& orn = gameObj->NodeGetWorldOrientation();
800                                         
801                                         float eulerAngles[3];   
802                                         float eulerAnglesOld[3] = {0.0f, 0.0f, 0.0f};                                           
803                                         float tmat[3][3];
804                                         
805                                         // XXX animato
806                                         Ipo* ipo = blenderObject->ipo;
807
808                                         //create the curves, if not existing, set linear if new
809
810                                         IpoCurve *icu_lx = findIpoCurve((IpoCurve *)ipo->curve.first,"LocX");
811                                         if (!icu_lx) {
812                                                 icu_lx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_X, 1);
813                                                 if(icu_lx) icu_lx->ipo = IPO_LIN;
814                                         }
815                                         IpoCurve *icu_ly = findIpoCurve((IpoCurve *)ipo->curve.first,"LocY");
816                                         if (!icu_ly) {
817                                                 icu_ly = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Y, 1);
818                                                 if(icu_ly) icu_ly->ipo = IPO_LIN;
819                                         }
820                                         IpoCurve *icu_lz = findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ");
821                                         if (!icu_lz) {
822                                                 icu_lz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_LOC_Z, 1);
823                                                 if(icu_lz) icu_lz->ipo = IPO_LIN;
824                                         }
825                                         IpoCurve *icu_rx = findIpoCurve((IpoCurve *)ipo->curve.first,"RotX");
826                                         if (!icu_rx) {
827                                                 icu_rx = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_X, 1);
828                                                 if(icu_rx) icu_rx->ipo = IPO_LIN;
829                                         }
830                                         IpoCurve *icu_ry = findIpoCurve((IpoCurve *)ipo->curve.first,"RotY");
831                                         if (!icu_ry) {
832                                                 icu_ry = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Y, 1);
833                                                 if(icu_ry) icu_ry->ipo = IPO_LIN;
834                                         }
835                                         IpoCurve *icu_rz = findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ");
836                                         if (!icu_rz) {
837                                                 icu_rz = verify_ipocurve(&blenderObject->id, ipo->blocktype, NULL, NULL, NULL, OB_ROT_Z, 1);
838                                                 if(icu_rz) icu_rz->ipo = IPO_LIN;
839                                         }
840                                         
841                                         if(icu_rx) eulerAnglesOld[0]= eval_icu( icu_rx, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
842                                         if(icu_ry) eulerAnglesOld[1]= eval_icu( icu_ry, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
843                                         if(icu_rz) eulerAnglesOld[2]= eval_icu( icu_rz, frameNumber - 1 ) / ((180 / 3.14159265f) / 10);
844                                         
845                                         // orn.getValue((float *)tmat); // uses the wrong ordering, cant use this
846                                         for (int r=0;r<3;r++)
847                                                 for (int c=0;c<3;c++)
848                                                         tmat[r][c] = orn[c][r];
849                                         
850                                         // Mat3ToEul(tmat, eulerAngles); // better to use Mat3ToCompatibleEul
851                                         Mat3ToCompatibleEul(tmat, eulerAngles, eulerAnglesOld);
852                                         
853                                         //eval_icu
854                                         for(int x = 0; x < 3; x++)
855                                                 eulerAngles[x] *= (float) ((180 / 3.14159265f) / 10.0);
856                                         
857                                         //fill the curves with data
858                                         if (icu_lx) insert_vert_icu(icu_lx, frameNumber, position.x(), 1);
859                                         if (icu_ly) insert_vert_icu(icu_ly, frameNumber, position.y(), 1);
860                                         if (icu_lz) insert_vert_icu(icu_lz, frameNumber, position.z(), 1);
861                                         if (icu_rx) insert_vert_icu(icu_rx, frameNumber, eulerAngles[0], 1);
862                                         if (icu_ry) insert_vert_icu(icu_ry, frameNumber, eulerAngles[1], 1);
863                                         if (icu_rz) insert_vert_icu(icu_rz, frameNumber, eulerAngles[2], 1);
864                                         
865                                         // Handles are corrected at the end, testhandles_ipocurve isnt needed yet
866 #endif
867                                 }
868                         }
869                 }
870         }
871 }
872
873
874 void    KX_BlenderSceneConverter::TestHandlesPhysicsObjectToAnimationIpo()
875 {
876
877         KX_SceneList* scenes = m_ketsjiEngine->CurrentScenes();
878         int numScenes = scenes->size();
879         int i;
880         for (i=0;i<numScenes;i++)
881         {
882                 KX_Scene* scene = scenes->at(i);
883                 //PHY_IPhysicsEnvironment* physEnv = scene->GetPhysicsEnvironment();
884                 CListValue* parentList = scene->GetRootParentList();
885                 int numObjects = parentList->GetCount();
886                 int g;
887                 for (g=0;g<numObjects;g++)
888                 {
889                         KX_GameObject* gameObj = (KX_GameObject*)parentList->GetValue(g);
890                         if (gameObj->IsDynamic())
891                         {
892                                 //KX_IPhysicsController* physCtrl = gameObj->GetPhysicsController();
893                                 
894                                 Object* blenderObject = gameObj->GetBlenderObject();
895                                 if (blenderObject && blenderObject->ipo)
896                                 {
897                                         // XXX animato
898 #if 0
899                                         Ipo* ipo = blenderObject->ipo;
900                                         
901                                         //create the curves, if not existing
902                                         //testhandles_ipocurve checks for NULL
903                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocX"));
904                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocY"));
905                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"LocZ"));
906                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotX"));
907                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotY"));
908                                         testhandles_ipocurve(findIpoCurve((IpoCurve *)ipo->curve.first,"RotZ"));
909 #endif
910                                 }
911                         }
912
913                 }
914                 
915         
916         }
917
918
919
920 }
921
922 #ifndef DISABLE_PYTHON
923 PyObject *KX_BlenderSceneConverter::GetPyNamespace()
924 {
925         return m_ketsjiEngine->GetPyNamespace();
926 }
927 #endif