svn merge -r 30954:31059 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / gameengine / Ketsji / KX_Scene.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Ketsji scene. Holds references to all scene data.
29  */
30
31 #ifdef WIN32
32 #pragma warning (disable : 4786)
33 #endif //WIN32
34
35 #include "KX_Scene.h"
36 #include "KX_PythonInit.h"
37 #include "MT_assert.h"
38 #include "KX_KetsjiEngine.h"
39 #include "KX_BlenderMaterial.h"
40 #include "RAS_IPolygonMaterial.h"
41 #include "ListValue.h"
42 #include "SCA_LogicManager.h"
43 #include "SCA_TimeEventManager.h"
44 //#include "SCA_AlwaysEventManager.h"
45 //#include "SCA_RandomEventManager.h"
46 //#include "KX_RayEventManager.h"
47 #include "KX_TouchEventManager.h"
48 #include "SCA_KeyboardManager.h"
49 #include "SCA_MouseManager.h"
50 //#include "SCA_PropertyEventManager.h"
51 #include "SCA_ActuatorEventManager.h"
52 #include "SCA_BasicEventManager.h"
53 #include "KX_Camera.h"
54 #include "SCA_JoystickManager.h"
55
56 #include "RAS_MeshObject.h"
57
58 #include "RAS_IRasterizer.h"
59 #include "RAS_BucketManager.h"
60
61 #include "FloatValue.h"
62 #include "SCA_IController.h"
63 #include "SCA_IActuator.h"
64 #include "SG_Node.h"
65 #include "SYS_System.h"
66 #include "SG_Controller.h"
67 #include "SG_IObject.h"
68 #include "SG_Tree.h"
69 #include "DNA_group_types.h"
70 #include "DNA_scene_types.h"
71
72 #include "KX_SG_NodeRelationships.h"
73
74 #include "KX_NetworkEventManager.h"
75 #include "NG_NetworkScene.h"
76 #include "PHY_IPhysicsEnvironment.h"
77 #include "KX_IPhysicsController.h"
78 #include "PHY_IGraphicController.h"
79 #include "KX_BlenderSceneConverter.h"
80 #include "KX_MotionState.h"
81
82 #include "BL_ModifierDeformer.h"
83 #include "BL_ShapeDeformer.h"
84 #include "BL_DeformableGameObject.h"
85 #include "KX_SoftBodyDeformer.h"
86
87 // to get USE_BULLET!
88 #include "KX_ConvertPhysicsObject.h"
89
90 #ifdef USE_BULLET
91 #include "CcdPhysicsEnvironment.h"
92 #include "CcdPhysicsController.h"
93 #endif
94
95 #include "KX_Light.h"
96
97 #include <stdio.h>
98
99 void* KX_SceneReplicationFunc(SG_IObject* node,void* gameobj,void* scene)
100 {
101         KX_GameObject* replica = ((KX_Scene*)scene)->AddNodeReplicaObject(node,(KX_GameObject*)gameobj);
102
103         if(replica)
104                 replica->Release();
105
106         return (void*)replica;
107 }
108
109 void* KX_SceneDestructionFunc(SG_IObject* node,void* gameobj,void* scene)
110 {
111         ((KX_Scene*)scene)->RemoveNodeDestructObject(node,(KX_GameObject*)gameobj);
112
113         return NULL;
114 };
115
116 bool KX_Scene::KX_ScenegraphUpdateFunc(SG_IObject* node,void* gameobj,void* scene)
117 {
118         return ((SG_Node*)node)->Schedule(((KX_Scene*)scene)->m_sghead);
119 }
120
121 bool KX_Scene::KX_ScenegraphRescheduleFunc(SG_IObject* node,void* gameobj,void* scene)
122 {
123         return ((SG_Node*)node)->Reschedule(((KX_Scene*)scene)->m_sghead);
124 }
125
126 SG_Callbacks KX_Scene::m_callbacks = SG_Callbacks(
127         KX_SceneReplicationFunc,
128         KX_SceneDestructionFunc,
129         KX_GameObject::UpdateTransformFunc,
130         KX_Scene::KX_ScenegraphUpdateFunc,
131         KX_Scene::KX_ScenegraphRescheduleFunc);
132
133 // temporarily var until there is a button in the userinterface
134 // (defined in KX_PythonInit.cpp)
135 extern bool gUseVisibilityTemp;
136
137 KX_Scene::KX_Scene(class SCA_IInputDevice* keyboarddevice,
138                                    class SCA_IInputDevice* mousedevice,
139                                    class NG_NetworkDeviceInterface *ndi,
140                                    const STR_String& sceneName,
141                                    Scene *scene,
142                                    class RAS_ICanvas* canvas): 
143         PyObjectPlus(),
144         m_keyboardmgr(NULL),
145         m_mousemgr(NULL),
146         m_sceneConverter(NULL),
147         m_physicsEnvironment(0),
148         m_sceneName(sceneName),
149         m_networkDeviceInterface(ndi),
150         m_active_camera(NULL),
151         m_ueberExecutionPriority(0),
152         m_blenderScene(scene)
153 {
154         m_suspendedtime = 0.0;
155         m_suspendeddelta = 0.0;
156
157         m_dbvt_culling = false;
158         m_dbvt_occlusion_res = 0;
159         m_activity_culling = false;
160         m_suspend = false;
161         m_isclearingZbuffer = true;
162         m_tempObjectList = new CListValue();
163         m_objectlist = new CListValue();
164         m_parentlist = new CListValue();
165         m_lightlist= new CListValue();
166         m_inactivelist = new CListValue();
167         m_euthanasyobjects = new CListValue();
168
169         m_logicmgr = new SCA_LogicManager();
170         
171         m_timemgr = new SCA_TimeEventManager(m_logicmgr);
172         m_keyboardmgr = new SCA_KeyboardManager(m_logicmgr,keyboarddevice);
173         m_mousemgr = new SCA_MouseManager(m_logicmgr,mousedevice, canvas);
174         
175         //SCA_AlwaysEventManager* alwaysmgr = new SCA_AlwaysEventManager(m_logicmgr);
176         //SCA_PropertyEventManager* propmgr = new SCA_PropertyEventManager(m_logicmgr);
177         SCA_ActuatorEventManager* actmgr = new SCA_ActuatorEventManager(m_logicmgr);
178         //SCA_RandomEventManager* rndmgr = new SCA_RandomEventManager(m_logicmgr);
179         SCA_BasicEventManager* basicmgr = new SCA_BasicEventManager(m_logicmgr);
180         //KX_RayEventManager* raymgr = new KX_RayEventManager(m_logicmgr);
181
182         KX_NetworkEventManager* netmgr = new KX_NetworkEventManager(m_logicmgr, ndi);
183         
184         
185
186         //m_logicmgr->RegisterEventManager(alwaysmgr);
187         //m_logicmgr->RegisterEventManager(propmgr);
188         m_logicmgr->RegisterEventManager(actmgr);
189         m_logicmgr->RegisterEventManager(m_keyboardmgr);
190         m_logicmgr->RegisterEventManager(m_mousemgr);
191         m_logicmgr->RegisterEventManager(m_timemgr);
192         //m_logicmgr->RegisterEventManager(rndmgr);
193         //m_logicmgr->RegisterEventManager(raymgr);
194         m_logicmgr->RegisterEventManager(netmgr);
195         m_logicmgr->RegisterEventManager(basicmgr);
196
197
198         SYS_SystemHandle hSystem = SYS_GetSystem();
199         bool nojoystick= SYS_GetCommandLineInt(hSystem,"nojoystick",0);
200         if (!nojoystick)
201         {
202                 SCA_JoystickManager *joymgr     = new SCA_JoystickManager(m_logicmgr);
203                 m_logicmgr->RegisterEventManager(joymgr);
204         }
205
206         MT_assert (m_networkDeviceInterface != NULL);
207         m_networkScene = new NG_NetworkScene(m_networkDeviceInterface);
208         
209         m_rootnode = NULL;
210
211         m_bucketmanager=new RAS_BucketManager();
212         
213 #ifndef DISABLE_PYTHON
214         m_attr_dict = PyDict_New(); /* new ref */
215         m_draw_call_pre = NULL;
216         m_draw_call_post = NULL;
217 #endif
218 }
219
220
221
222 KX_Scene::~KX_Scene()
223 {
224         // The release of debug properties used to be in SCA_IScene::~SCA_IScene
225         // It's still there but we remove all properties here otherwise some
226         // reference might be hanging and causing late release of objects
227         RemoveAllDebugProperties();
228
229         while (GetRootParentList()->GetCount() > 0) 
230         {
231                 KX_GameObject* parentobj = (KX_GameObject*) GetRootParentList()->GetValue(0);
232                 this->RemoveObject(parentobj);
233         }
234
235         if(m_objectlist)
236                 m_objectlist->Release();
237
238         if (m_parentlist)
239                 m_parentlist->Release();
240         
241         if (m_inactivelist)
242                 m_inactivelist->Release();
243
244         if (m_lightlist)
245                 m_lightlist->Release();
246         
247         if (m_tempObjectList)
248                 m_tempObjectList->Release();
249
250         if (m_euthanasyobjects)
251                 m_euthanasyobjects->Release();
252
253         if (m_logicmgr)
254                 delete m_logicmgr;
255
256         if (m_physicsEnvironment)
257                 delete m_physicsEnvironment;
258
259         if (m_networkScene)
260                 delete m_networkScene;
261         
262         if (m_bucketmanager)
263         {
264                 delete m_bucketmanager;
265         }
266
267 #ifndef DISABLE_PYTHON
268         PyDict_Clear(m_attr_dict);
269         Py_DECREF(m_attr_dict);
270
271         Py_XDECREF(m_draw_call_pre);
272         Py_XDECREF(m_draw_call_post);
273 #endif
274 }
275
276 RAS_BucketManager* KX_Scene::GetBucketManager()
277 {
278         return m_bucketmanager;
279 }
280
281
282 CListValue* KX_Scene::GetTempObjectList()
283 {
284         return m_tempObjectList;
285 }
286
287 CListValue* KX_Scene::GetObjectList()
288 {
289         return m_objectlist;
290 }
291
292
293 CListValue* KX_Scene::GetRootParentList()
294 {
295         return m_parentlist;
296 }
297
298 CListValue* KX_Scene::GetInactiveList()
299 {
300         return m_inactivelist;
301 }
302
303
304
305 CListValue* KX_Scene::GetLightList()
306 {
307         return m_lightlist;
308 }
309
310 SCA_LogicManager* KX_Scene::GetLogicManager()
311 {
312         return m_logicmgr;
313 }
314
315 SCA_TimeEventManager* KX_Scene::GetTimeEventManager()
316 {
317         return m_timemgr;
318 }
319
320
321
322  
323 list<class KX_Camera*>* KX_Scene::GetCameras()
324 {
325         return &m_cameras;
326 }
327
328
329
330 void KX_Scene::SetFramingType(RAS_FrameSettings & frame_settings)
331 {
332         m_frame_settings = frame_settings;
333 };
334
335 /**
336  * Return a const reference to the framing 
337  * type set by the above call.
338  * The contents are not guarenteed to be sensible
339  * if you don't call the above function.
340  */
341 const RAS_FrameSettings& KX_Scene::GetFramingType() const 
342 {
343         return m_frame_settings;
344 };      
345
346
347
348 /**
349  * Store the current scene's viewport on the 
350  * game engine canvas.
351  */
352 void KX_Scene::SetSceneViewport(const RAS_Rect &viewport)
353 {
354         m_viewport = viewport;
355 }
356
357
358
359 const RAS_Rect& KX_Scene::GetSceneViewport() const 
360 {
361         return m_viewport;
362 }
363
364
365
366 void KX_Scene::SetWorldInfo(class KX_WorldInfo* worldinfo)
367 {
368         m_worldinfo = worldinfo;
369 }
370
371
372
373 class KX_WorldInfo* KX_Scene::GetWorldInfo()
374 {
375         return m_worldinfo;
376 }
377
378
379 const STR_String& KX_Scene::GetName()
380 {
381         return m_sceneName;
382 }
383
384
385 void KX_Scene::Suspend()
386 {
387         m_suspend = true;
388 }
389
390 void KX_Scene::Resume()
391 {
392         m_suspend = false;
393 }
394
395 void KX_Scene::SetActivityCulling(bool b)
396 {
397         m_activity_culling = b;
398 }
399
400 bool KX_Scene::IsSuspended()
401 {
402         return m_suspend;
403 }
404
405 bool KX_Scene::IsClearingZBuffer()
406 {
407         return m_isclearingZbuffer;
408 }
409
410 void KX_Scene::EnableZBufferClearing(bool isclearingZbuffer)
411 {
412         m_isclearingZbuffer = isclearingZbuffer;
413 }
414
415 void KX_Scene::RemoveNodeDestructObject(class SG_IObject* node,class CValue* gameobj)
416 {
417         KX_GameObject* orgobj = (KX_GameObject*)gameobj;        
418         if (NewRemoveObject(orgobj) != 0)
419         {
420                 // object is not yet deleted because a reference is hanging somewhere.
421                 // This should not happen anymore since we use proxy object for Python
422                 // confident enough to put an assert?
423                 //assert(false);
424                 printf("Zombie object! name=%s\n", orgobj->GetName().ReadPtr());
425                 orgobj->SetSGNode(NULL);
426                 PHY_IGraphicController* ctrl = orgobj->GetGraphicController();
427                 if (ctrl)
428                 {
429                         // a graphic controller is set, we must delete it as the node will be deleted
430                         delete ctrl;
431                         orgobj->SetGraphicController(NULL);
432                 }
433         }
434         if (node)
435                 delete node;
436 }
437
438 KX_GameObject* KX_Scene::AddNodeReplicaObject(class SG_IObject* node, class CValue* gameobj)
439 {
440         // for group duplication, limit the duplication of the hierarchy to the
441         // objects that are part of the group. 
442         if (!IsObjectInGroup(gameobj))
443                 return NULL;
444         
445         KX_GameObject* orgobj = (KX_GameObject*)gameobj;
446         KX_GameObject* newobj = (KX_GameObject*)orgobj->GetReplica();
447         m_map_gameobject_to_replica.insert(orgobj, newobj);
448
449         // also register 'timers' (time properties) of the replica
450         int numprops = newobj->GetPropertyCount();
451
452         for (int i = 0; i < numprops; i++)
453         {
454                 CValue* prop = newobj->GetProperty(i);
455
456                 if (prop->GetProperty("timer"))
457                         this->m_timemgr->AddTimeProperty(prop);
458         }
459
460         if (node)
461         {
462                 newobj->SetSGNode((SG_Node*)node);
463         }
464         else
465         {
466                 m_rootnode = new SG_Node(newobj,this,KX_Scene::m_callbacks);
467         
468                 // this fixes part of the scaling-added object bug
469                 SG_Node* orgnode = orgobj->GetSGNode();
470                 m_rootnode->SetLocalScale(orgnode->GetLocalScale());
471                 m_rootnode->SetLocalPosition(orgnode->GetLocalPosition());
472                 m_rootnode->SetLocalOrientation(orgnode->GetLocalOrientation());
473
474                 // define the relationship between this node and it's parent.
475                 KX_NormalParentRelation * parent_relation = 
476                         KX_NormalParentRelation::New();
477                 m_rootnode->SetParentRelation(parent_relation);
478
479                 newobj->SetSGNode(m_rootnode);
480         }
481         
482         SG_IObject* replicanode = newobj->GetSGNode();
483 //      SG_Node* rootnode = (replicanode == m_rootnode ? NULL : m_rootnode);
484
485         replicanode->SetSGClientObject(newobj);
486
487         // this is the list of object that are send to the graphics pipeline
488         m_objectlist->Add(newobj->AddRef());
489         if (newobj->GetGameObjectType()==SCA_IObject::OBJ_LIGHT)
490                 m_lightlist->Add(newobj->AddRef());
491         newobj->AddMeshUser();
492
493         // logic cannot be replicated, until the whole hierarchy is replicated.
494         m_logicHierarchicalGameObjects.push_back(newobj);
495         //replicate controllers of this node
496         SGControllerList        scenegraphcontrollers = orgobj->GetSGNode()->GetSGControllerList();
497         replicanode->RemoveAllControllers();
498         SGControllerList::iterator cit;
499         //int numcont = scenegraphcontrollers.size();
500         
501         for (cit = scenegraphcontrollers.begin();!(cit==scenegraphcontrollers.end());++cit)
502         {
503                 // controller replication is quite complicated
504                 // only replicate ipo and physics controller for now
505
506                 SG_Controller* replicacontroller = (*cit)->GetReplica((SG_Node*) replicanode);
507                 if (replicacontroller)
508                 {
509                         replicacontroller->SetObject(replicanode);
510                         replicanode->AddSGController(replicacontroller);
511                 }
512         }
513         // replicate graphic controller
514         if (orgobj->GetGraphicController())
515         {
516                 PHY_IMotionState* motionstate = new KX_MotionState(newobj->GetSGNode());
517                 PHY_IGraphicController* newctrl = orgobj->GetGraphicController()->GetReplica(motionstate);
518                 newctrl->setNewClientInfo(newobj->getClientInfo());
519                 newobj->SetGraphicController(newctrl);
520         }
521         return newobj;
522 }
523
524
525
526 // before calling this method KX_Scene::ReplicateLogic(), make sure to
527 // have called 'GameObject::ReParentLogic' for each object this
528 // hierarchy that's because first ALL bricks must exist in the new
529 // replica of the hierarchy in order to make cross-links work properly
530 // !
531 // It is VERY important that the order of sensors and actuators in
532 // the replicated object is preserved: it is used to reconnect the logic.
533 // This method is more robust then using the bricks name in case of complex 
534 // group replication. The replication of logic bricks is done in 
535 // SCA_IObject::ReParentLogic(), make sure it preserves the order of the bricks.
536 void KX_Scene::ReplicateLogic(KX_GameObject* newobj)
537 {
538         // also relink the controller to sensors/actuators
539         SCA_ControllerList& controllers = newobj->GetControllers();
540         //SCA_SensorList&     sensors     = newobj->GetSensors();
541         //SCA_ActuatorList&   actuators   = newobj->GetActuators();
542
543         for (SCA_ControllerList::iterator itc = controllers.begin(); !(itc==controllers.end());itc++)
544         {
545                 SCA_IController* cont = (*itc);
546                 cont->SetUeberExecutePriority(m_ueberExecutionPriority);
547                 vector<SCA_ISensor*> linkedsensors = cont->GetLinkedSensors();
548                 vector<SCA_IActuator*> linkedactuators = cont->GetLinkedActuators();
549
550                 // disconnect the sensors and actuators
551                 // do it directly on the list at this controller is not connected to anything at this stage
552                 cont->GetLinkedSensors().clear();
553                 cont->GetLinkedActuators().clear();
554                 
555                 // now relink each sensor
556                 for (vector<SCA_ISensor*>::iterator its = linkedsensors.begin();!(its==linkedsensors.end());its++)
557                 {
558                         SCA_ISensor* oldsensor = (*its);
559                         SCA_IObject* oldsensorobj = oldsensor->GetParent();
560                         SCA_IObject* newsensorobj = NULL;
561                 
562                         // the original owner of the sensor has been replicated?
563                         void **h_obj = m_map_gameobject_to_replica[oldsensorobj];
564                         if (h_obj)
565                                 newsensorobj = (SCA_IObject*)(*h_obj);
566                         if (!newsensorobj)
567                         {
568                                 // no, then the sensor points outside the hierachy, keep it the same
569                                 if (m_objectlist->SearchValue(oldsensorobj))
570                                         // only replicate links that points to active objects
571                                         m_logicmgr->RegisterToSensor(cont,oldsensor);
572                         }
573                         else
574                         {
575                                 // yes, then the new sensor has the same position
576                                 SCA_SensorList& sensorlist = oldsensorobj->GetSensors();
577                                 SCA_SensorList::iterator sit;
578                                 SCA_ISensor* newsensor = NULL;
579                                 int sensorpos;
580
581                                 for (sensorpos=0, sit=sensorlist.begin(); sit!=sensorlist.end(); sit++, sensorpos++)
582                                 {
583                                         if ((*sit) == oldsensor) 
584                                         {
585                                                 newsensor = newsensorobj->GetSensors().at(sensorpos);
586                                                 break;
587                                         }
588                                 }
589                                 assert(newsensor != NULL);
590                                 m_logicmgr->RegisterToSensor(cont,newsensor);
591                         }
592                 }
593                 
594                 // now relink each actuator
595                 for (vector<SCA_IActuator*>::iterator ita = linkedactuators.begin();!(ita==linkedactuators.end());ita++)
596                 {
597                         SCA_IActuator* oldactuator = (*ita);
598                         SCA_IObject* oldactuatorobj = oldactuator->GetParent();
599                         SCA_IObject* newactuatorobj = NULL;
600
601                         // the original owner of the sensor has been replicated?
602                         void **h_obj = m_map_gameobject_to_replica[oldactuatorobj];
603                         if (h_obj)
604                                 newactuatorobj = (SCA_IObject*)(*h_obj);
605
606                         if (!newactuatorobj)
607                         {
608                                 // no, then the sensor points outside the hierachy, keep it the same
609                                 if (m_objectlist->SearchValue(oldactuatorobj))
610                                         // only replicate links that points to active objects
611                                         m_logicmgr->RegisterToActuator(cont,oldactuator);
612                         }
613                         else
614                         {
615                                 // yes, then the new sensor has the same position
616                                 SCA_ActuatorList& actuatorlist = oldactuatorobj->GetActuators();
617                                 SCA_ActuatorList::iterator ait;
618                                 SCA_IActuator* newactuator = NULL;
619                                 int actuatorpos;
620
621                                 for (actuatorpos=0, ait=actuatorlist.begin(); ait!=actuatorlist.end(); ait++, actuatorpos++)
622                                 {
623                                         if ((*ait) == oldactuator) 
624                                         {
625                                                 newactuator = newactuatorobj->GetActuators().at(actuatorpos);
626                                                 break;
627                                         }
628                                 }
629                                 assert(newactuator != NULL);
630                                 m_logicmgr->RegisterToActuator(cont,newactuator);
631                                 newactuator->SetUeberExecutePriority(m_ueberExecutionPriority);
632                         }
633                 }
634         }
635         // ready to set initial state
636         newobj->ResetState();
637 }
638
639 void KX_Scene::DupliGroupRecurse(CValue* obj, int level)
640 {
641         KX_GameObject* groupobj = (KX_GameObject*) obj;
642         KX_GameObject* replica;
643         KX_GameObject* gameobj;
644         Object* blgroupobj = groupobj->GetBlenderObject();
645         Group* group;
646         GroupObject *go;
647         vector<KX_GameObject*> duplilist;
648
649         if (!groupobj->GetSGNode() ||
650                 !groupobj->IsDupliGroup() ||
651                 level>MAX_DUPLI_RECUR)
652                 return;
653
654         // we will add one group at a time
655         m_logicHierarchicalGameObjects.clear();
656         m_map_gameobject_to_replica.clear();
657         m_ueberExecutionPriority++;
658         // for groups will do something special: 
659         // we will force the creation of objects to those in the group only
660         // Again, this is match what Blender is doing (it doesn't care of parent relationship)
661         m_groupGameObjects.clear();
662
663         group = blgroupobj->dup_group;
664         for(go=(GroupObject*)group->gobject.first; go; go=(GroupObject*)go->next) 
665         {
666                 Object* blenderobj = go->ob;
667                 if (blgroupobj == blenderobj)
668                         // this check is also in group_duplilist()
669                         continue;
670
671                 gameobj = (KX_GameObject*)m_logicmgr->FindGameObjByBlendObj(blenderobj);
672                 if (gameobj == NULL) 
673                 {
674                         // this object has not been converted!!!
675                         // Should not happen as dupli group are created automatically 
676                         continue;
677                 }
678
679                 gameobj->SetBlenderGroupObject(blgroupobj);
680
681                 if ((blenderobj->lay & group->layer)==0)
682                 {
683                         // object is not visible in the 3D view, will not be instantiated
684                         continue;
685                 }
686                 m_groupGameObjects.insert(gameobj);
687         }
688
689         set<CValue*>::iterator oit;
690         for (oit=m_groupGameObjects.begin(); oit != m_groupGameObjects.end(); oit++)
691         {
692                 gameobj = (KX_GameObject*)(*oit);
693
694                 KX_GameObject *parent = gameobj->GetParent();
695                 if (parent != NULL)
696                 {
697                         parent->Release(); // GetParent() increased the refcount
698
699                         // this object is not a top parent. Either it is the child of another
700                         // object in the group and it will be added automatically when the parent
701                         // is added. Or it is the child of an object outside the group and the group
702                         // is inconsistent, skip it anyway
703                         continue;
704                 }
705                 replica = (KX_GameObject*) AddNodeReplicaObject(NULL,gameobj);
706                 // add to 'rootparent' list (this is the list of top hierarchy objects, updated each frame)
707                 m_parentlist->Add(replica->AddRef());
708
709                 // recurse replication into children nodes
710                 NodeList& children = gameobj->GetSGNode()->GetSGChildren();
711
712                 replica->GetSGNode()->ClearSGChildren();
713                 for (NodeList::iterator childit = children.begin();!(childit==children.end());++childit)
714                 {
715                         SG_Node* orgnode = (*childit);
716                         SG_Node* childreplicanode = orgnode->GetSGReplica();
717                         if (childreplicanode)
718                                 replica->GetSGNode()->AddChild(childreplicanode);
719                 }
720                 // don't replicate logic now: we assume that the objects in the group can have
721                 // logic relationship, even outside parent relationship
722                 // In order to match 3D view, the position of groupobj is used as a 
723                 // transformation matrix instead of the new position. This means that 
724                 // the group reference point is 0,0,0
725
726                 // get the rootnode's scale
727                 MT_Vector3 newscale = groupobj->NodeGetWorldScaling();
728                 // set the replica's relative scale with the rootnode's scale
729                 replica->NodeSetRelativeScale(newscale);
730
731                 MT_Point3 offset(group->dupli_ofs);
732                 MT_Point3 newpos = groupobj->NodeGetWorldPosition() + 
733                         newscale*(groupobj->NodeGetWorldOrientation() * (gameobj->NodeGetWorldPosition()-offset));
734                 replica->NodeSetLocalPosition(newpos);
735                 // set the orientation after position for softbody!
736                 MT_Matrix3x3 newori = groupobj->NodeGetWorldOrientation() * gameobj->NodeGetWorldOrientation();
737                 replica->NodeSetLocalOrientation(newori);
738                 // update scenegraph for entire tree of children
739                 replica->GetSGNode()->UpdateWorldData(0);
740                 replica->GetSGNode()->SetBBox(gameobj->GetSGNode()->BBox());
741                 replica->GetSGNode()->SetRadius(gameobj->GetSGNode()->Radius());
742                 // we can now add the graphic controller to the physic engine
743                 replica->ActivateGraphicController(true);
744
745                 // done with replica
746                 replica->Release();
747         }
748
749         // the logic must be replicated first because we need
750         // the new logic bricks before relinking
751         vector<KX_GameObject*>::iterator git;
752         for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
753         {
754                 (*git)->ReParentLogic();
755         }
756         
757         //      relink any pointers as necessary, sort of a temporary solution
758         for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
759         {
760                 // this will also relink the actuator to objects within the hierarchy
761                 (*git)->Relink(&m_map_gameobject_to_replica);
762                 // add the object in the layer of the parent
763                 (*git)->SetLayer(groupobj->GetLayer());
764                 // If the object was a light, we need to update it's RAS_LightObject as well
765                 if ((*git)->GetGameObjectType()==SCA_IObject::OBJ_LIGHT)
766                 {
767                         KX_LightObject* lightobj = static_cast<KX_LightObject*>(*git);
768                         lightobj->GetLightData()->m_layer = groupobj->GetLayer();
769                 }
770         }
771
772         // replicate crosslinks etc. between logic bricks
773         for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
774         {
775                 ReplicateLogic((*git));
776         }
777         
778         // now look if object in the hierarchy have dupli group and recurse
779         for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
780         {
781                 if ((*git) != groupobj && (*git)->IsDupliGroup())
782                         // can't instantiate group immediately as it destroys m_logicHierarchicalGameObjects
783                         duplilist.push_back((*git));
784         }
785
786         for (git = duplilist.begin(); !(git == duplilist.end()); ++git)
787         {
788                 DupliGroupRecurse((*git), level+1);
789         }
790 }
791
792
793 SCA_IObject* KX_Scene::AddReplicaObject(class CValue* originalobject,
794                                                                                 class CValue* parentobject,
795                                                                                 int lifespan)
796 {
797
798         m_logicHierarchicalGameObjects.clear();
799         m_map_gameobject_to_replica.clear();
800         m_groupGameObjects.clear();
801
802         // todo: place a timebomb in the object, for temporarily objects :)
803         // lifespan of zero means 'this object lives forever'
804         KX_GameObject* originalobj = (KX_GameObject*) originalobject;
805         KX_GameObject* parentobj = (KX_GameObject*) parentobject;
806
807         m_ueberExecutionPriority++;
808
809         // lets create a replica
810         KX_GameObject* replica = (KX_GameObject*) AddNodeReplicaObject(NULL,originalobj);
811
812         if (lifespan > 0)
813         {
814                 // add a timebomb to this object
815                 // for now, convert between so called frames and realtime
816                 m_tempObjectList->Add(replica->AddRef());
817                 CValue *fval = new CFloatValue(lifespan*0.02);
818                 replica->SetProperty("::timebomb",fval);
819                 fval->Release();
820         }
821
822         // add to 'rootparent' list (this is the list of top hierarchy objects, updated each frame)
823         m_parentlist->Add(replica->AddRef());
824
825         // recurse replication into children nodes
826
827         NodeList& children = originalobj->GetSGNode()->GetSGChildren();
828
829         replica->GetSGNode()->ClearSGChildren();
830         for (NodeList::iterator childit = children.begin();!(childit==children.end());++childit)
831         {
832                 SG_Node* orgnode = (*childit);
833                 SG_Node* childreplicanode = orgnode->GetSGReplica();
834                 if (childreplicanode)
835                         replica->GetSGNode()->AddChild(childreplicanode);
836         }
837
838         // At this stage all the objects in the hierarchy have been duplicated,
839         // we can update the scenegraph, we need it for the duplication of logic
840         MT_Point3 newpos = ((KX_GameObject*) parentobject)->NodeGetWorldPosition();
841         replica->NodeSetLocalPosition(newpos);
842
843         MT_Matrix3x3 newori = ((KX_GameObject*) parentobject)->NodeGetWorldOrientation();
844         replica->NodeSetLocalOrientation(newori);
845         
846         // get the rootnode's scale
847         MT_Vector3 newscale = parentobj->GetSGNode()->GetRootSGParent()->GetLocalScale();
848
849         // set the replica's relative scale with the rootnode's scale
850         replica->NodeSetRelativeScale(newscale);
851
852         replica->GetSGNode()->UpdateWorldData(0);
853         replica->GetSGNode()->SetBBox(originalobj->GetSGNode()->BBox());
854         replica->GetSGNode()->SetRadius(originalobj->GetSGNode()->Radius());
855         // the size is correct, we can add the graphic controller to the physic engine
856         replica->ActivateGraphicController(true);
857
858         // now replicate logic
859         vector<KX_GameObject*>::iterator git;
860         for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
861         {
862                 (*git)->ReParentLogic();
863         }
864         
865         //      relink any pointers as necessary, sort of a temporary solution
866         for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
867         {
868                 // this will also relink the actuators in the hierarchy
869                 (*git)->Relink(&m_map_gameobject_to_replica);
870                 // add the object in the layer of the parent
871                 (*git)->SetLayer(parentobj->GetLayer());
872                 // If the object was a light, we need to update it's RAS_LightObject as well
873                 if ((*git)->GetGameObjectType()==SCA_IObject::OBJ_LIGHT)
874                 {
875                         KX_LightObject* lightobj = static_cast<KX_LightObject*>(*git);
876                         lightobj->GetLightData()->m_layer = parentobj->GetLayer();
877                 }
878         }
879
880         // replicate crosslinks etc. between logic bricks
881         for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
882         {
883                 ReplicateLogic((*git));
884         }
885         
886         // check if there are objects with dupligroup in the hierarchy
887         vector<KX_GameObject*> duplilist;
888         for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
889         {
890                 if ((*git)->IsDupliGroup())
891                 {
892                         // separate list as m_logicHierarchicalGameObjects is also used by DupliGroupRecurse()
893                         duplilist.push_back(*git);
894                 }
895         }
896         for (git = duplilist.begin();!(git==duplilist.end());++git)
897         {
898                 DupliGroupRecurse(*git, 0);
899         }
900         //      don't release replica here because we are returning it, not done with it...
901         return replica;
902 }
903
904
905
906 void KX_Scene::RemoveObject(class CValue* gameobj)
907 {
908         KX_GameObject* newobj = (KX_GameObject*) gameobj;
909
910         // disconnect child from parent
911         SG_Node* node = newobj->GetSGNode();
912
913         if (node)
914         {
915                 node->DisconnectFromParent();
916
917                 // recursively destruct
918                 node->Destruct();
919         }
920         //no need to do that: the object is destroyed and memory released 
921         //newobj->SetSGNode(0);
922 }
923
924 void KX_Scene::DelayedRemoveObject(class CValue* gameobj)
925 {
926         //KX_GameObject* newobj = (KX_GameObject*) gameobj;
927         if (!m_euthanasyobjects->SearchValue(gameobj))
928         {
929                 m_euthanasyobjects->Add(gameobj->AddRef());
930         } 
931 }
932
933
934
935 int KX_Scene::NewRemoveObject(class CValue* gameobj)
936 {
937         int ret;
938         KX_GameObject* newobj = (KX_GameObject*) gameobj;
939
940         /* Invalidate the python reference, since the object may exist in script lists
941          * its possible that it wont be automatically invalidated, so do it manually here,
942          * 
943          * if for some reason the object is added back into the scene python can always get a new Proxy
944          */
945         newobj->InvalidateProxy();
946
947         // keep the blender->game object association up to date
948         // note that all the replicas of an object will have the same
949         // blender object, that's why we need to check the game object
950         // as only the deletion of the original object must be recorded
951         m_logicmgr->UnregisterGameObj(newobj->GetBlenderObject(), gameobj);
952
953         //todo: look at this
954         //GetPhysicsEnvironment()->RemovePhysicsController(gameobj->getPhysicsController());
955
956         // remove all sensors/controllers/actuators from logicsystem...
957         
958         SCA_SensorList& sensors = newobj->GetSensors();
959         for (SCA_SensorList::iterator its = sensors.begin();
960                  !(its==sensors.end());its++)
961         {
962                 m_logicmgr->RemoveSensor(*its);
963         }
964
965         SCA_ControllerList& controllers = newobj->GetControllers();
966         for (SCA_ControllerList::iterator itc = controllers.begin();
967                  !(itc==controllers.end());itc++)
968         {
969                 m_logicmgr->RemoveController(*itc);
970         }
971
972         SCA_ActuatorList& actuators = newobj->GetActuators();
973         for (SCA_ActuatorList::iterator ita = actuators.begin();
974                  !(ita==actuators.end());ita++)
975         {
976                 m_logicmgr->RemoveActuator(*ita);
977         }
978         // the sensors/controllers/actuators must also be released, this is done in ~SCA_IObject
979
980         // now remove the timer properties from the time manager
981         int numprops = newobj->GetPropertyCount();
982
983         for (int i = 0; i < numprops; i++)
984         {
985                 CValue* propval = newobj->GetProperty(i);
986                 if (propval->GetProperty("timer"))
987                 {
988                         m_timemgr->RemoveTimeProperty(propval);
989                 }
990         }
991         
992         newobj->RemoveMeshes();
993         ret = 1;
994         if (newobj->GetGameObjectType()==SCA_IObject::OBJ_LIGHT && m_lightlist->RemoveValue(newobj))
995                 ret = newobj->Release();
996         if (m_objectlist->RemoveValue(newobj))
997                 ret = newobj->Release();
998         if (m_tempObjectList->RemoveValue(newobj))
999                 ret = newobj->Release();
1000         if (m_parentlist->RemoveValue(newobj))
1001                 ret = newobj->Release();
1002         if (m_inactivelist->RemoveValue(newobj))
1003                 ret = newobj->Release();
1004         if (m_euthanasyobjects->RemoveValue(newobj))
1005                 ret = newobj->Release();
1006                 
1007         if (newobj == m_active_camera)
1008         {
1009                 //no AddRef done on m_active_camera so no Release
1010                 //m_active_camera->Release();
1011                 m_active_camera = NULL;
1012         }
1013
1014         // in case this is a camera
1015         m_cameras.remove((KX_Camera*)newobj);
1016
1017         /* currently does nothing, keep incase we need to Unregister something */
1018 #if 0
1019         if (m_sceneConverter)
1020                 m_sceneConverter->UnregisterGameObject(newobj);
1021 #endif
1022         
1023         // return value will be 0 if the object is actually deleted (all reference gone)
1024         
1025         return ret;
1026 }
1027
1028
1029
1030 void KX_Scene::ReplaceMesh(class CValue* obj,void* meshobj, bool use_gfx, bool use_phys)
1031 {
1032         KX_GameObject* gameobj = static_cast<KX_GameObject*>(obj);
1033         RAS_MeshObject* mesh = static_cast<RAS_MeshObject*>(meshobj);
1034
1035         if(!gameobj) {
1036                 std::cout << "KX_Scene::ReplaceMesh Warning: invalid object, doing nothing" << std::endl;
1037                 return;
1038         }
1039
1040         if(use_gfx && mesh != NULL)
1041         {               
1042         gameobj->RemoveMeshes();
1043         gameobj->AddMesh(mesh);
1044         
1045         if (gameobj->m_isDeformable)
1046         {
1047                 BL_DeformableGameObject* newobj = static_cast<BL_DeformableGameObject*>( gameobj );
1048                 
1049                 if (newobj->GetDeformer())
1050                 {
1051                         delete newobj->GetDeformer();
1052                         newobj->SetDeformer(NULL);
1053                 }
1054
1055                 if (mesh->GetMesh()) 
1056                 {
1057                         // we must create a new deformer but which one?
1058                         KX_GameObject* parentobj = newobj->GetParent();
1059                         // this always return the original game object (also for replicate)
1060                         Object* blendobj = newobj->GetBlenderObject();
1061                         // object that owns the new mesh
1062                         Object* oldblendobj = static_cast<struct Object*>(m_logicmgr->FindBlendObjByGameMeshName(mesh->GetName()));
1063                         Mesh* blendmesh = mesh->GetMesh();
1064
1065                         bool bHasModifier = BL_ModifierDeformer::HasCompatibleDeformer(blendobj);
1066                         bool bHasShapeKey = blendmesh->key != NULL && blendmesh->key->type==KEY_RELATIVE;
1067                         bool bHasDvert = blendmesh->dvert != NULL;
1068                         bool bHasArmature = 
1069                                 BL_ModifierDeformer::HasArmatureDeformer(blendobj) &&
1070                                 parentobj &&                                                            // current parent is armature
1071                                 parentobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE &&
1072                                 oldblendobj &&                                                          // needed for mesh deform
1073                                 blendobj->parent &&                                                     // original object had armature (not sure this test is needed)
1074                                 blendobj->parent->type == OB_ARMATURE &&
1075                                 blendmesh->dvert!=NULL;                                         // mesh has vertex group
1076                         bool bHasSoftBody = (!parentobj && (blendobj->gameflag & OB_SOFT_BODY));
1077
1078                         bool releaseParent = true;
1079
1080                         
1081                         if (oldblendobj==NULL) {
1082                                 if (bHasModifier || bHasShapeKey || bHasDvert || bHasArmature) {
1083                                         std::cout << "warning: ReplaceMesh() new mesh is not used in an object from the current scene, you will get incorrect behavior" << std::endl;
1084                                         bHasShapeKey= bHasDvert= bHasArmature=bHasModifier= false;
1085                                 }
1086                         }
1087                         
1088                         if (bHasModifier)
1089                         {
1090                                 BL_ModifierDeformer* modifierDeformer;
1091                                 if (bHasShapeKey || bHasArmature)
1092                                 {
1093                                         modifierDeformer = new BL_ModifierDeformer(
1094                                                 newobj,
1095                                                 m_blenderScene,
1096                                                 oldblendobj, blendobj,
1097                                                 mesh,
1098                                                 true,
1099                                                 static_cast<BL_ArmatureObject*>( parentobj )
1100                                         );
1101                                         releaseParent= false;
1102                                         modifierDeformer->LoadShapeDrivers(blendobj->parent);
1103                                 }
1104                                 else
1105                                 {
1106                                         modifierDeformer = new BL_ModifierDeformer(
1107                                                 newobj,
1108                                                 m_blenderScene,
1109                                                 oldblendobj, blendobj,
1110                                                 mesh,
1111                                                 false,
1112                                                 NULL
1113                                         );
1114                                 }
1115                                 newobj->SetDeformer(modifierDeformer);
1116                         } 
1117                         else    if (bHasShapeKey)
1118                         {
1119                                 BL_ShapeDeformer* shapeDeformer;
1120                                 if (bHasArmature) 
1121                                 {
1122                                         shapeDeformer = new BL_ShapeDeformer(
1123                                                 newobj,
1124                                                 oldblendobj, blendobj,
1125                                                 mesh,
1126                                                 true,
1127                                                 true,
1128                                                 static_cast<BL_ArmatureObject*>( parentobj )
1129                                         );
1130                                         releaseParent= false;
1131                                         shapeDeformer->LoadShapeDrivers(blendobj->parent);
1132                                 }
1133                                 else
1134                                 {
1135                                         shapeDeformer = new BL_ShapeDeformer(
1136                                                 newobj,
1137                                                 oldblendobj, blendobj,
1138                                                 mesh,
1139                                                 false,
1140                                                 true,
1141                                                 NULL
1142                                         );
1143                                 }
1144                                 newobj->SetDeformer( shapeDeformer);
1145                         }
1146                         else if (bHasArmature) 
1147                         {
1148                                 BL_SkinDeformer* skinDeformer = new BL_SkinDeformer(
1149                                         newobj,
1150                                         oldblendobj, blendobj,
1151                                         mesh,
1152                                         true,
1153                                         true,
1154                                         static_cast<BL_ArmatureObject*>( parentobj )
1155                                 );
1156                                 releaseParent= false;
1157                                 newobj->SetDeformer(skinDeformer);
1158                         }
1159                         else if (bHasDvert)
1160                         {
1161                                 BL_MeshDeformer* meshdeformer = new BL_MeshDeformer(
1162                                         newobj, oldblendobj, mesh
1163                                 );
1164                                 newobj->SetDeformer(meshdeformer);
1165                         }
1166                         else if (bHasSoftBody)
1167                         {
1168                                 KX_SoftBodyDeformer *softdeformer = new KX_SoftBodyDeformer(mesh, newobj);
1169                                 newobj->SetDeformer(softdeformer);
1170                         }
1171
1172                         // release parent reference if its not being used 
1173                         if( releaseParent && parentobj)
1174                                 parentobj->Release();
1175                 }
1176         }
1177
1178         gameobj->AddMeshUser();
1179         }
1180         
1181         if(use_phys) { /* update the new assigned mesh with the physics mesh */
1182                 KX_ReInstanceBulletShapeFromMesh(gameobj, NULL, use_gfx?NULL:mesh);
1183         }
1184 }
1185
1186 KX_Camera* KX_Scene::FindCamera(KX_Camera* cam)
1187 {
1188         list<KX_Camera*>::iterator it = m_cameras.begin();
1189
1190         while ( (it != m_cameras.end()) 
1191                         && ((*it) != cam) ) {
1192           it++;
1193         }
1194
1195         return ((it == m_cameras.end()) ? NULL : (*it));
1196 }
1197
1198
1199 KX_Camera* KX_Scene::FindCamera(STR_String& name)
1200 {
1201         list<KX_Camera*>::iterator it = m_cameras.begin();
1202
1203         while ( (it != m_cameras.end()) 
1204                         && ((*it)->GetName() != name) ) {
1205           it++;
1206         }
1207
1208         return ((it == m_cameras.end()) ? NULL : (*it));
1209 }
1210
1211 void KX_Scene::AddCamera(KX_Camera* cam)
1212 {
1213         if (!FindCamera(cam))
1214                 m_cameras.push_back(cam);
1215 }
1216
1217
1218 KX_Camera* KX_Scene::GetActiveCamera()
1219 {       
1220         // NULL if not defined
1221         return m_active_camera;
1222 }
1223
1224
1225 void KX_Scene::SetActiveCamera(KX_Camera* cam)
1226 {
1227         // only set if the cam is in the active list? Or add it otherwise?
1228         if (!FindCamera(cam)){
1229                 AddCamera(cam);
1230                 if (cam) std::cout << "Added cam " << cam->GetName() << std::endl;
1231         } 
1232
1233         m_active_camera = cam;
1234 }
1235
1236 void KX_Scene::SetCameraOnTop(KX_Camera* cam)
1237 {
1238         if (!FindCamera(cam)){
1239                 // adding is always done at the back, so that's all that needs to be done
1240                 AddCamera(cam);
1241                 if (cam) std::cout << "Added cam " << cam->GetName() << std::endl;
1242         } else {
1243                 m_cameras.remove(cam);
1244                 m_cameras.push_back(cam);
1245         }
1246 }
1247
1248
1249 void KX_Scene::UpdateMeshTransformations()
1250 {
1251         // do this incrementally in the future
1252         for (int i = 0; i < m_objectlist->GetCount(); i++)
1253         {
1254                 KX_GameObject* gameobj = (KX_GameObject*)m_objectlist->GetValue(i);
1255                 gameobj->GetOpenGLMatrix();
1256         }
1257 }
1258
1259 void KX_Scene::MarkVisible(SG_Tree *node, RAS_IRasterizer* rasty, KX_Camera* cam, int layer)
1260 {
1261         int intersect = KX_Camera::INTERSECT;
1262         KX_GameObject *gameobj = node->Client()?(KX_GameObject*) node->Client()->GetSGClientObject():NULL;
1263         bool visible = (gameobj && gameobj->GetVisible() && (!layer || (gameobj->GetLayer() & layer)));
1264         bool dotest = visible || node->Left() || node->Right();
1265
1266         /* If the camera is inside the box, assume intersect. */
1267         if (dotest && !node->inside( cam->NodeGetWorldPosition()))
1268         {
1269                 MT_Scalar radius = node->Radius();
1270                 MT_Point3 center = node->Center();
1271                 
1272                 intersect =  cam->SphereInsideFrustum(center, radius); 
1273                 
1274                 if (intersect == KX_Camera::INTERSECT)
1275                 {
1276                         MT_Point3 box[8];
1277                         node->get(box);
1278                         intersect = cam->BoxInsideFrustum(box);
1279                 }
1280         }
1281
1282         switch (intersect)
1283         {
1284                 case KX_Camera::OUTSIDE:
1285                         MarkSubTreeVisible(node, rasty, false, cam);
1286                         break;
1287                 case KX_Camera::INTERSECT:
1288                         if (gameobj)
1289                                 MarkVisible(rasty, gameobj, cam, layer);
1290                         if (node->Left())
1291                                 MarkVisible(node->Left(), rasty, cam, layer);
1292                         if (node->Right())
1293                                 MarkVisible(node->Right(), rasty, cam, layer);
1294                         break;
1295                 case KX_Camera::INSIDE:
1296                         MarkSubTreeVisible(node, rasty, true, cam, layer);
1297                         break;
1298         }
1299 }
1300
1301 void KX_Scene::MarkSubTreeVisible(SG_Tree *node, RAS_IRasterizer* rasty, bool visible, KX_Camera* cam, int layer)
1302 {
1303         if (node->Client())
1304         {
1305                 KX_GameObject *gameobj = (KX_GameObject*) node->Client()->GetSGClientObject();
1306                 if (gameobj->GetVisible())
1307                 {
1308                         if (visible)
1309                         {
1310                                 int nummeshes = gameobj->GetMeshCount();
1311                                 
1312                                 // this adds the vertices to the display list
1313                                 for (int m=0;m<nummeshes;m++)
1314                                         (gameobj->GetMesh(m))->SchedulePolygons(rasty->GetDrawingMode());
1315                         }
1316
1317                         gameobj->SetCulled(!visible);
1318                         gameobj->UpdateBuckets(false);
1319                 }
1320         }
1321         if (node->Left())
1322                 MarkSubTreeVisible(node->Left(), rasty, visible, cam, layer);
1323         if (node->Right())
1324                 MarkSubTreeVisible(node->Right(), rasty, visible, cam, layer);
1325 }
1326
1327 void KX_Scene::MarkVisible(RAS_IRasterizer* rasty, KX_GameObject* gameobj,KX_Camera*  cam,int layer)
1328 {
1329         // User (Python/Actuator) has forced object invisible...
1330         if (!gameobj->GetSGNode() || !gameobj->GetVisible())
1331                 return;
1332         
1333         // Shadow lamp layers
1334         if(layer && !(gameobj->GetLayer() & layer)) {
1335                 gameobj->SetCulled(true);
1336                 gameobj->UpdateBuckets(false);
1337                 return;
1338         }
1339
1340         // If Frustum culling is off, the object is always visible.
1341         bool vis = !cam->GetFrustumCulling();
1342         
1343         // If the camera is inside this node, then the object is visible.
1344         if (!vis)
1345         {
1346                 vis = gameobj->GetSGNode()->inside( cam->GetCameraLocation() );
1347         }
1348                 
1349         // Test the object's bound sphere against the view frustum.
1350         if (!vis)
1351         {
1352                 MT_Vector3 scale = gameobj->GetSGNode()->GetWorldScaling();
1353                 MT_Scalar radius = fabs(scale[scale.closestAxis()] * gameobj->GetSGNode()->Radius());
1354                 switch (cam->SphereInsideFrustum(gameobj->NodeGetWorldPosition(), radius))
1355                 {
1356                         case KX_Camera::INSIDE:
1357                                 vis = true;
1358                                 break;
1359                         case KX_Camera::OUTSIDE:
1360                                 vis = false;
1361                                 break;
1362                         case KX_Camera::INTERSECT:
1363                                 // Test the object's bound box against the view frustum.
1364                                 MT_Point3 box[8];
1365                                 gameobj->GetSGNode()->getBBox(box); 
1366                                 vis = cam->BoxInsideFrustum(box) != KX_Camera::OUTSIDE;
1367                                 break;
1368                 }
1369         }
1370         
1371         if (vis)
1372         {
1373                 int nummeshes = gameobj->GetMeshCount();
1374                 
1375                 for (int m=0;m<nummeshes;m++)
1376                 {
1377                         // this adds the vertices to the display list
1378                         (gameobj->GetMesh(m))->SchedulePolygons(rasty->GetDrawingMode());
1379                 }
1380                 // Visibility/ non-visibility are marked
1381                 // elsewhere now.
1382                 gameobj->SetCulled(false);
1383                 gameobj->UpdateBuckets(false);
1384         } else {
1385                 gameobj->SetCulled(true);
1386                 gameobj->UpdateBuckets(false);
1387         }
1388 }
1389
1390 void KX_Scene::PhysicsCullingCallback(KX_ClientObjectInfo* objectInfo, void* cullingInfo)
1391 {
1392         KX_GameObject* gameobj = objectInfo->m_gameobject;
1393         if (!gameobj->GetVisible())
1394                 // ideally, invisible objects should be removed from the culling tree temporarily
1395                 return;
1396         if(((CullingInfo*)cullingInfo)->m_layer && !(gameobj->GetLayer() & ((CullingInfo*)cullingInfo)->m_layer))
1397                 // used for shadow: object is not in shadow layer
1398                 return;
1399
1400         // make object visible
1401         gameobj->SetCulled(false);
1402         gameobj->UpdateBuckets(false);
1403 }
1404
1405 void KX_Scene::CalculateVisibleMeshes(RAS_IRasterizer* rasty,KX_Camera* cam, int layer)
1406 {
1407         bool dbvt_culling = false;
1408         if (m_dbvt_culling) 
1409         {
1410                 // test culling through Bullet
1411                 PHY__Vector4 planes[6];
1412                 // get the clip planes
1413                 MT_Vector4* cplanes = cam->GetNormalizedClipPlanes();
1414                 // and convert
1415                 planes[0].setValue(cplanes[4].getValue());      // near
1416                 planes[1].setValue(cplanes[5].getValue());      // far
1417                 planes[2].setValue(cplanes[0].getValue());      // left
1418                 planes[3].setValue(cplanes[1].getValue());      // right
1419                 planes[4].setValue(cplanes[2].getValue());      // top
1420                 planes[5].setValue(cplanes[3].getValue());      // bottom
1421                 CullingInfo info(layer);
1422                 dbvt_culling = m_physicsEnvironment->cullingTest(PhysicsCullingCallback,&info,planes,5,m_dbvt_occlusion_res);
1423         }
1424         if (!dbvt_culling) {
1425                 // the physics engine couldn't help us, do it the hard way
1426                 for (int i = 0; i < m_objectlist->GetCount(); i++)
1427                 {
1428                         MarkVisible(rasty, static_cast<KX_GameObject*>(m_objectlist->GetValue(i)), cam, layer);
1429                 }
1430         }
1431 }
1432
1433 // logic stuff
1434 void KX_Scene::LogicBeginFrame(double curtime)
1435 {
1436         // have a look at temp objects ...
1437         int lastobj = m_tempObjectList->GetCount() - 1;
1438         
1439         for (int i = lastobj; i >= 0; i--)
1440         {
1441                 CValue* objval = m_tempObjectList->GetValue(i);
1442                 CFloatValue* propval = (CFloatValue*) objval->GetProperty("::timebomb");
1443                 
1444                 if (propval)
1445                 {
1446                         float timeleft = propval->GetNumber() - 1.0/KX_KetsjiEngine::GetTicRate();
1447                         
1448                         if (timeleft > 0)
1449                         {
1450                                 propval->SetFloat(timeleft);
1451                         }
1452                         else
1453                         {
1454                                 DelayedRemoveObject(objval);
1455                                 // remove obj
1456                         }
1457                 }
1458                 else
1459                 {
1460                         // all object is the tempObjectList should have a clock
1461                 }
1462         }
1463         m_logicmgr->BeginFrame(curtime, 1.0/KX_KetsjiEngine::GetTicRate());
1464 }
1465
1466
1467
1468 void KX_Scene::LogicUpdateFrame(double curtime, bool frame)
1469 {
1470         m_logicmgr->UpdateFrame(curtime, frame);
1471 }
1472
1473
1474
1475 void KX_Scene::LogicEndFrame()
1476 {
1477         m_logicmgr->EndFrame();
1478         int numobj = m_euthanasyobjects->GetCount();
1479
1480         KX_GameObject* obj;
1481
1482         while ((numobj = m_euthanasyobjects->GetCount()) > 0)
1483         {
1484                 // remove the object from this list to make sure we will not hit it again
1485                 obj = (KX_GameObject*)m_euthanasyobjects->GetValue(numobj-1);
1486                 m_euthanasyobjects->Remove(numobj-1);
1487                 obj->Release();
1488                 RemoveObject(obj);
1489         }
1490 }
1491
1492
1493
1494 /**
1495   * UpdateParents: SceneGraph transformation update.
1496   */
1497 void KX_Scene::UpdateParents(double curtime)
1498 {
1499         // we use the SG dynamic list
1500         SG_Node* node;
1501
1502         while ((node = SG_Node::GetNextScheduled(m_sghead)) != NULL)
1503         {
1504                 node->UpdateWorldData(curtime);
1505         }
1506
1507         //for (int i=0; i<GetRootParentList()->GetCount(); i++)
1508         //{
1509         //      KX_GameObject* parentobj = (KX_GameObject*)GetRootParentList()->GetValue(i);
1510         //      parentobj->NodeUpdateGS(curtime);
1511         //}
1512
1513         // the list must be empty here
1514         assert(m_sghead.Empty());
1515         // some nodes may be ready for reschedule, move them to schedule list for next time
1516         while ((node = SG_Node::GetNextRescheduled(m_sghead)) != NULL)
1517         {
1518                 node->Schedule(m_sghead);
1519         }
1520 }
1521
1522
1523 RAS_MaterialBucket* KX_Scene::FindBucket(class RAS_IPolyMaterial* polymat, bool &bucketCreated)
1524 {
1525         return m_bucketmanager->FindBucket(polymat, bucketCreated);
1526 }
1527
1528
1529
1530 void KX_Scene::RenderBuckets(const MT_Transform & cameratransform,
1531                                                          class RAS_IRasterizer* rasty,
1532                                                          class RAS_IRenderTools* rendertools)
1533 {
1534         m_bucketmanager->Renderbuckets(cameratransform,rasty,rendertools);
1535         KX_BlenderMaterial::EndFrame();
1536 }
1537
1538 void KX_Scene::UpdateObjectActivity(void) 
1539 {
1540         if (m_activity_culling) {
1541                 /* determine the activity criterium and set objects accordingly */
1542                 int i=0;
1543                 
1544                 MT_Point3 camloc = GetActiveCamera()->NodeGetWorldPosition(); //GetCameraLocation();
1545                 
1546                 for (i=0;i<GetObjectList()->GetCount();i++)
1547                 {
1548                         KX_GameObject* ob = (KX_GameObject*) GetObjectList()->GetValue(i);
1549                         
1550                         if (!ob->GetIgnoreActivityCulling()) {
1551                                 /* Simple test: more than 10 away from the camera, count
1552                                  * Manhattan distance. */
1553                                 MT_Point3 obpos = ob->NodeGetWorldPosition();
1554                                 
1555                                 if ( (fabs(camloc[0] - obpos[0]) > m_activity_box_radius)
1556                                          || (fabs(camloc[1] - obpos[1]) > m_activity_box_radius)
1557                                          || (fabs(camloc[2] - obpos[2]) > m_activity_box_radius) )
1558                                 {                       
1559                                         ob->Suspend();
1560                                 } else {
1561                                         ob->Resume();
1562                                 }
1563                         }
1564                 }               
1565         }
1566 }
1567
1568 void KX_Scene::SetActivityCullingRadius(float f)
1569 {
1570         if (f < 0.5)
1571                 f = 0.5;
1572         m_activity_box_radius = f;
1573 }
1574         
1575 NG_NetworkDeviceInterface* KX_Scene::GetNetworkDeviceInterface()
1576 {
1577         return m_networkDeviceInterface;
1578 }
1579
1580 NG_NetworkScene* KX_Scene::GetNetworkScene()
1581 {
1582         return m_networkScene;
1583 }
1584
1585 void KX_Scene::SetNetworkDeviceInterface(NG_NetworkDeviceInterface* newInterface)
1586 {
1587         m_networkDeviceInterface = newInterface;
1588 }
1589
1590 void KX_Scene::SetNetworkScene(NG_NetworkScene *newScene)
1591 {
1592         m_networkScene = newScene;
1593 }
1594
1595
1596 void    KX_Scene::SetGravity(const MT_Vector3& gravity)
1597 {
1598         GetPhysicsEnvironment()->setGravity(gravity[0],gravity[1],gravity[2]);
1599 }
1600
1601 void KX_Scene::SetSceneConverter(class KX_BlenderSceneConverter* sceneConverter)
1602 {
1603         m_sceneConverter = sceneConverter;
1604 }
1605
1606 void KX_Scene::SetPhysicsEnvironment(class PHY_IPhysicsEnvironment* physEnv)
1607 {
1608         m_physicsEnvironment = physEnv;
1609         if(m_physicsEnvironment) {
1610                 KX_TouchEventManager* touchmgr = new KX_TouchEventManager(m_logicmgr, physEnv);
1611                 m_logicmgr->RegisterEventManager(touchmgr);
1612         }
1613 }
1614  
1615 void KX_Scene::setSuspendedTime(double suspendedtime)
1616 {
1617         m_suspendedtime = suspendedtime;
1618 }
1619 double KX_Scene::getSuspendedTime()
1620 {
1621         return m_suspendedtime;
1622 }
1623 void KX_Scene::setSuspendedDelta(double suspendeddelta)
1624 {
1625         m_suspendeddelta = suspendeddelta;
1626 }
1627 double KX_Scene::getSuspendedDelta()
1628 {
1629         return m_suspendeddelta;
1630 }
1631
1632 #include "KX_BulletPhysicsController.h"
1633
1634 static void MergeScene_LogicBrick(SCA_ILogicBrick* brick, KX_Scene *to)
1635 {
1636         SCA_LogicManager *logicmgr= to->GetLogicManager();
1637
1638         brick->Replace_IScene(to);
1639         brick->Replace_NetworkScene(to->GetNetworkScene());
1640
1641         SCA_ISensor *sensor=  dynamic_cast<class SCA_ISensor *>(brick);
1642         if(sensor) {
1643                 sensor->Replace_EventManager(logicmgr);
1644         }
1645
1646         /* near sensors have physics controllers */
1647         KX_TouchSensor *touch_sensor = dynamic_cast<class KX_TouchSensor *>(brick);
1648         if(touch_sensor) {
1649                 touch_sensor->GetPhysicsController()->SetPhysicsEnvironment(to->GetPhysicsEnvironment());
1650         }
1651 }
1652
1653 #include "CcdGraphicController.h" // XXX  ctrl->SetPhysicsEnvironment(to->GetPhysicsEnvironment());
1654 #include "CcdPhysicsEnvironment.h" // XXX  ctrl->SetPhysicsEnvironment(to->GetPhysicsEnvironment());
1655 #include "KX_BulletPhysicsController.h"
1656
1657
1658 static void MergeScene_GameObject(KX_GameObject* gameobj, KX_Scene *to, KX_Scene *from)
1659 {
1660         {
1661                 SCA_ActuatorList& actuators= gameobj->GetActuators();
1662                 SCA_ActuatorList::iterator ita;
1663
1664                 for (ita = actuators.begin(); !(ita==actuators.end()); ++ita)
1665                 {
1666                         MergeScene_LogicBrick(*ita, to);
1667                 }
1668         }
1669
1670
1671         {
1672                 SCA_SensorList& sensors= gameobj->GetSensors();
1673                 SCA_SensorList::iterator its;
1674
1675                 for (its = sensors.begin(); !(its==sensors.end()); ++its)
1676                 {
1677                         MergeScene_LogicBrick(*its, to);
1678                 }
1679         }
1680
1681         {
1682                 SCA_ControllerList& controllers= gameobj->GetControllers();
1683                 SCA_ControllerList::iterator itc;
1684
1685                 for (itc = controllers.begin(); !(itc==controllers.end()); ++itc)
1686                 {
1687                         SCA_IController *cont= *itc;
1688                         MergeScene_LogicBrick(cont, to);
1689
1690                         vector<SCA_ISensor*> linkedsensors = cont->GetLinkedSensors();
1691                         vector<SCA_IActuator*> linkedactuators = cont->GetLinkedActuators();
1692
1693                         for (vector<SCA_IActuator*>::iterator ita = linkedactuators.begin();!(ita==linkedactuators.end());++ita) {
1694                                 MergeScene_LogicBrick(*ita, to);
1695                         }
1696
1697                         for (vector<SCA_ISensor*>::iterator its = linkedsensors.begin();!(its==linkedsensors.end());++its) {
1698                                 MergeScene_LogicBrick(*its, to);
1699                         }
1700                 }
1701         }
1702
1703         /* graphics controller */
1704         PHY_IGraphicController *ctrl = gameobj->GetGraphicController();
1705         if(ctrl) {
1706                 /* SHOULD update the m_cullingTree */
1707                 ctrl->SetPhysicsEnvironment(to->GetPhysicsEnvironment());
1708         }
1709
1710         /* SG_Node can hold a scene reference */
1711         SG_Node *sg= gameobj->GetSGNode();
1712         if(sg) {
1713                 if(sg->GetSGClientInfo() == from) {
1714                         sg->SetSGClientInfo(to);
1715                 }
1716
1717                 SGControllerList::iterator contit;
1718                 SGControllerList& controllers = sg->GetSGControllerList();
1719                 for (contit = controllers.begin();contit!=controllers.end();++contit)
1720                 {
1721                         KX_BulletPhysicsController *phys_ctrl= dynamic_cast<KX_BulletPhysicsController *>(*contit);
1722                         if (phys_ctrl)
1723                                 phys_ctrl->SetPhysicsEnvironment(to->GetPhysicsEnvironment());
1724                 }
1725         }
1726         /* If the object is a light, update it's scene */
1727         if (gameobj->GetGameObjectType() == SCA_IObject::OBJ_LIGHT)
1728                 ((KX_LightObject*)gameobj)->UpdateScene(to);
1729
1730         /* Add the object to the scene's logic manager */
1731         to->GetLogicManager()->RegisterGameObjectName(gameobj->GetName(), gameobj);
1732         to->GetLogicManager()->RegisterGameObj(gameobj->GetBlenderObject(), gameobj);
1733
1734         for (int i=0; i<gameobj->GetMeshCount(); ++i)
1735                 to->GetLogicManager()->RegisterGameMeshName(gameobj->GetMesh(i)->GetName(), gameobj->GetBlenderObject());
1736 }
1737
1738 bool KX_Scene::MergeScene(KX_Scene *other)
1739 {
1740         CcdPhysicsEnvironment *env=                     dynamic_cast<CcdPhysicsEnvironment *>(this->GetPhysicsEnvironment());
1741         CcdPhysicsEnvironment *env_other=       dynamic_cast<CcdPhysicsEnvironment *>(other->GetPhysicsEnvironment());
1742
1743         if((env==NULL) != (env_other==NULL)) /* TODO - even when both scenes have NONE physics, the other is loaded with bullet enabled, ??? */
1744         {
1745                 printf("KX_Scene::MergeScene: physics scenes type differ, aborting\n");
1746                 printf("\tsource %d, terget %d\n", (int)(env!=NULL), (int)(env_other!=NULL));
1747                 return false;
1748         }
1749
1750         if(GetSceneConverter() != other->GetSceneConverter()) {
1751                 printf("KX_Scene::MergeScene: converters differ, aborting\n");
1752                 return false;
1753         }
1754
1755
1756         GetBucketManager()->MergeBucketManager(other->GetBucketManager(), this);
1757
1758         /* move materials across, assume they both use the same scene-converters */
1759         GetSceneConverter()->MergeScene(this, other);
1760
1761         /* active + inactive == all ??? - lets hope so */
1762         for (int i = 0; i < other->GetObjectList()->GetCount(); i++)
1763         {
1764                 KX_GameObject* gameobj = (KX_GameObject*)other->GetObjectList()->GetValue(i);
1765                 MergeScene_GameObject(gameobj, this, other);
1766
1767                 gameobj->UpdateBuckets(false); /* only for active objects */
1768         }
1769
1770         for (int i = 0; i < other->GetInactiveList()->GetCount(); i++)
1771         {
1772                 KX_GameObject* gameobj = (KX_GameObject*)other->GetInactiveList()->GetValue(i);
1773                 MergeScene_GameObject(gameobj, this, other);
1774         }
1775
1776         GetTempObjectList()->MergeList(other->GetTempObjectList());
1777         other->GetTempObjectList()->ReleaseAndRemoveAll();
1778
1779         GetObjectList()->MergeList(other->GetObjectList());
1780         other->GetObjectList()->ReleaseAndRemoveAll();
1781
1782         GetInactiveList()->MergeList(other->GetInactiveList());
1783         other->GetInactiveList()->ReleaseAndRemoveAll();
1784
1785         GetRootParentList()->MergeList(other->GetRootParentList());
1786         other->GetRootParentList()->ReleaseAndRemoveAll();
1787
1788         GetLightList()->MergeList(other->GetLightList());
1789         other->GetLightList()->ReleaseAndRemoveAll();
1790
1791         if(env) /* bullet scene? - dummy scenes dont need touching */
1792                 env->MergeEnvironment(env_other);
1793
1794         /* merge logic */
1795         {
1796                 SCA_LogicManager *logicmgr=                     GetLogicManager();
1797                 SCA_LogicManager *logicmgr_other=       other->GetLogicManager();
1798
1799                 vector<class SCA_EventManager*>evtmgrs= logicmgr->GetEventManagers();
1800                 //vector<class SCA_EventManager*>evtmgrs_others= logicmgr_other->GetEventManagers();
1801
1802                 //SCA_EventManager *evtmgr;
1803                 SCA_EventManager *evtmgr_other;
1804
1805                 for(unsigned int i= 0; i < evtmgrs.size(); i++) {
1806                         evtmgr_other= logicmgr_other->FindEventManager(evtmgrs[i]->GetType());
1807
1808                         if(evtmgr_other) /* unlikely but possible one scene has a joystick and not the other */
1809                                 evtmgr_other->Replace_LogicManager(logicmgr);
1810
1811                         /* when merging objects sensors are moved across into the new manager, dont need to do this here */
1812                 }
1813         }
1814         return true;
1815 }
1816
1817 void KX_Scene::Update2DFilter(vector<STR_String>& propNames, void* gameObj, RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text)
1818 {
1819         m_filtermanager.EnableFilter(propNames, gameObj, filtermode, pass, text);
1820 }
1821
1822 void KX_Scene::Render2DFilters(RAS_ICanvas* canvas)
1823 {
1824         m_filtermanager.RenderFilters(canvas);
1825 }
1826
1827 #ifndef DISABLE_PYTHON
1828
1829 void KX_Scene::RunDrawingCallbacks(PyObject* cb_list)
1830 {
1831         int len;
1832
1833         if (cb_list && (len=PyList_GET_SIZE(cb_list)))
1834         {
1835                 PyObject* args= PyTuple_New(0); // save python creating each call
1836                 PyObject* func;
1837                 PyObject* ret;
1838
1839                 // Iterate the list and run the callbacks
1840                 for (int pos=0; pos < len; pos++)
1841                 {
1842                         func= PyList_GET_ITEM(cb_list, pos);
1843                         ret= PyObject_Call(func, args, NULL);
1844                         if (ret==NULL) {
1845                                 PyErr_Print();
1846                                 PyErr_Clear();
1847                         }
1848                         else {
1849                                 Py_DECREF(ret);
1850                         }
1851                 }
1852
1853                 Py_DECREF(args);
1854         }
1855 }
1856
1857 //----------------------------------------------------------------------------
1858 //Python
1859
1860 PyTypeObject KX_Scene::Type = {
1861         PyVarObject_HEAD_INIT(NULL, 0)
1862         "KX_Scene",
1863         sizeof(PyObjectPlus_Proxy),
1864         0,
1865         py_base_dealloc,
1866         0,
1867         0,
1868         0,
1869         0,
1870         py_base_repr,
1871         0,
1872         &Sequence,
1873         &Mapping,
1874         0,0,0,0,0,0,
1875         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1876         0,0,0,0,0,0,0,
1877         Methods,
1878         0,
1879         0,
1880         &CValue::Type,
1881         0,0,0,0,0,0,
1882         py_base_new
1883 };
1884
1885 PyMethodDef KX_Scene::Methods[] = {
1886         KX_PYMETHODTABLE(KX_Scene, addObject),
1887         KX_PYMETHODTABLE(KX_Scene, end),
1888         KX_PYMETHODTABLE(KX_Scene, restart),
1889         KX_PYMETHODTABLE(KX_Scene, replace),
1890         KX_PYMETHODTABLE(KX_Scene, suspend),
1891         KX_PYMETHODTABLE(KX_Scene, resume),
1892         
1893         /* dict style access */
1894         KX_PYMETHODTABLE(KX_Scene, get),
1895         
1896         {NULL,NULL} //Sentinel
1897 };
1898 static PyObject *Map_GetItem(PyObject *self_v, PyObject *item)
1899 {
1900         KX_Scene* self= static_cast<KX_Scene*>BGE_PROXY_REF(self_v);
1901         const char *attr_str= _PyUnicode_AsString(item);
1902         PyObject* pyconvert;
1903         
1904         if (self==NULL) {
1905                 PyErr_SetString(PyExc_SystemError, "val = scene[key]: KX_Scene, "BGE_PROXY_ERROR_MSG);
1906                 return NULL;
1907         }
1908         
1909         if (self->m_attr_dict && (pyconvert=PyDict_GetItem(self->m_attr_dict, item))) {
1910                 
1911                 if (attr_str)
1912                         PyErr_Clear();
1913                 Py_INCREF(pyconvert);
1914                 return pyconvert;
1915         }
1916         else {
1917                 if(attr_str)    PyErr_Format(PyExc_KeyError, "value = scene[key]: KX_Scene, key \"%s\" does not exist", attr_str);
1918                 else                    PyErr_SetString(PyExc_KeyError, "value = scene[key]: KX_Scene, key does not exist");
1919                 return NULL;
1920         }
1921                 
1922 }
1923
1924 static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val)
1925 {
1926         KX_Scene* self= static_cast<KX_Scene*>BGE_PROXY_REF(self_v);
1927         const char *attr_str= _PyUnicode_AsString(key);
1928         if(attr_str==NULL)
1929                 PyErr_Clear();
1930         
1931         if (self==NULL) {
1932                 PyErr_SetString(PyExc_SystemError, "scene[key] = value: KX_Scene, "BGE_PROXY_ERROR_MSG);
1933                 return -1;
1934         }
1935         
1936         if (val==NULL) { /* del ob["key"] */
1937                 int del= 0;
1938                 
1939                 if(self->m_attr_dict)
1940                         del |= (PyDict_DelItem(self->m_attr_dict, key)==0) ? 1:0;
1941                 
1942                 if (del==0) {
1943                         if(attr_str)    PyErr_Format(PyExc_KeyError, "scene[key] = value: KX_Scene, key \"%s\" could not be set", attr_str);
1944                         else                    PyErr_SetString(PyExc_KeyError, "del scene[key]: KX_Scene, key could not be deleted");
1945                         return -1;
1946                 }
1947                 else if (self->m_attr_dict) {
1948                         PyErr_Clear(); /* PyDict_DelItem sets an error when it fails */
1949                 }
1950         }
1951         else { /* ob["key"] = value */
1952                 int set = 0;
1953
1954                 if (self->m_attr_dict==NULL) /* lazy init */
1955                         self->m_attr_dict= PyDict_New();
1956                 
1957                 
1958                 if(PyDict_SetItem(self->m_attr_dict, key, val)==0)
1959                         set= 1;
1960                 else
1961                         PyErr_SetString(PyExc_KeyError, "scene[key] = value: KX_Scene, key not be added to internal dictionary");
1962         
1963                 if(set==0)
1964                         return -1; /* pythons error value */
1965                 
1966         }
1967         
1968         return 0; /* success */
1969 }
1970
1971 static int Seq_Contains(PyObject *self_v, PyObject *value)
1972 {
1973         KX_Scene* self= static_cast<KX_Scene*>BGE_PROXY_REF(self_v);
1974         
1975         if (self==NULL) {
1976                 PyErr_SetString(PyExc_SystemError, "val in scene: KX_Scene, "BGE_PROXY_ERROR_MSG);
1977                 return -1;
1978         }
1979         
1980         if (self->m_attr_dict && PyDict_GetItem(self->m_attr_dict, value))
1981                 return 1;
1982         
1983         return 0;
1984 }
1985
1986 PyMappingMethods KX_Scene::Mapping = {
1987         (lenfunc)NULL                                   ,                       /*inquiry mp_length */
1988         (binaryfunc)Map_GetItem,                /*binaryfunc mp_subscript */
1989         (objobjargproc)Map_SetItem,     /*objobjargproc mp_ass_subscript */
1990 };
1991
1992 PySequenceMethods KX_Scene::Sequence = {
1993         NULL,           /* Cant set the len otherwise it can evaluate as false */
1994         NULL,           /* sq_concat */
1995         NULL,           /* sq_repeat */
1996         NULL,           /* sq_item */
1997         NULL,           /* sq_slice */
1998         NULL,           /* sq_ass_item */
1999         NULL,           /* sq_ass_slice */
2000         (objobjproc)Seq_Contains,       /* sq_contains */
2001         (binaryfunc) NULL, /* sq_inplace_concat */
2002         (ssizeargfunc) NULL, /* sq_inplace_repeat */
2003 };
2004
2005 PyObject* KX_Scene::pyattr_get_name(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
2006 {
2007         KX_Scene* self= static_cast<KX_Scene*>(self_v);
2008         return PyUnicode_FromString(self->GetName().ReadPtr());
2009 }
2010
2011 PyObject* KX_Scene::pyattr_get_objects(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
2012 {
2013         KX_Scene* self= static_cast<KX_Scene*>(self_v);
2014         return self->GetObjectList()->GetProxy();
2015 }
2016
2017 PyObject* KX_Scene::pyattr_get_objects_inactive(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
2018 {
2019         KX_Scene* self= static_cast<KX_Scene*>(self_v);
2020         return self->GetInactiveList()->GetProxy();
2021 }
2022
2023 PyObject* KX_Scene::pyattr_get_lights(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
2024 {
2025         KX_Scene* self= static_cast<KX_Scene*>(self_v);
2026         return self->GetLightList()->GetProxy();
2027 }
2028
2029 PyObject* KX_Scene::pyattr_get_cameras(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
2030 {
2031         /* With refcounts in this case...
2032          * the new CListValue is owned by python, so its possible python holds onto it longer then the BGE
2033          * however this is the same with "scene.objects + []", when you make a copy by adding lists.
2034          */
2035         
2036         KX_Scene* self= static_cast<KX_Scene*>(self_v);
2037         CListValue* clist = new CListValue();
2038         
2039         /* return self->GetCameras()->GetProxy(); */
2040         
2041         list<KX_Camera*>::iterator it = self->GetCameras()->begin();
2042         while (it != self->GetCameras()->end()) {
2043                 clist->Add((*it)->AddRef());
2044                 it++;
2045         }
2046         
2047         return clist->NewProxy(true);
2048 }
2049
2050 PyObject* KX_Scene::pyattr_get_active_camera(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
2051 {
2052         KX_Scene* self= static_cast<KX_Scene*>(self_v);
2053         return self->GetActiveCamera()->GetProxy();
2054 }
2055
2056
2057 int KX_Scene::pyattr_set_active_camera(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
2058 {
2059         KX_Scene* self= static_cast<KX_Scene*>(self_v);
2060         KX_Camera *camOb;
2061         
2062         if (!ConvertPythonToCamera(value, &camOb, false, "scene.active_camera = value: KX_Scene"))
2063                 return PY_SET_ATTR_FAIL;
2064         
2065         self->SetActiveCamera(camOb);
2066         return PY_SET_ATTR_SUCCESS;
2067 }
2068
2069 PyObject* KX_Scene::pyattr_get_drawing_callback_pre(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
2070 {
2071         KX_Scene* self = static_cast<KX_Scene*>(self_v);
2072
2073         if(self->m_draw_call_pre==NULL)
2074                 self->m_draw_call_pre= PyList_New(0);
2075         else
2076                 Py_INCREF(self->m_draw_call_pre);
2077         return self->m_draw_call_pre;
2078 }
2079
2080 PyObject* KX_Scene::pyattr_get_drawing_callback_post(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
2081 {
2082         KX_Scene* self = static_cast<KX_Scene*>(self_v);
2083
2084         if(self->m_draw_call_post==NULL)
2085                 self->m_draw_call_post= PyList_New(0);
2086         else
2087                 Py_INCREF(self->m_draw_call_post);
2088         return self->m_draw_call_post;
2089 }
2090
2091 int KX_Scene::pyattr_set_drawing_callback_pre(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
2092 {
2093         KX_Scene* self = static_cast<KX_Scene*>(self_v);
2094
2095         if (!PyList_CheckExact(value))
2096         {
2097                 PyErr_SetString(PyExc_ValueError, "Expected a list");
2098                 return PY_SET_ATTR_FAIL;
2099         }
2100         Py_XDECREF(self->m_draw_call_pre);
2101
2102         Py_INCREF(value);
2103         self->m_draw_call_pre = value;
2104
2105         return PY_SET_ATTR_SUCCESS;
2106 }
2107
2108 int KX_Scene::pyattr_set_drawing_callback_post(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
2109 {
2110         KX_Scene* self = static_cast<KX_Scene*>(self_v);
2111
2112         if (!PyList_CheckExact(value))
2113         {
2114                 PyErr_SetString(PyExc_ValueError, "Expected a list");
2115                 return PY_SET_ATTR_FAIL;
2116         }
2117         Py_XDECREF(self->m_draw_call_post);
2118
2119         Py_INCREF(value);
2120         self->m_draw_call_post = value;
2121
2122         return PY_SET_ATTR_SUCCESS;
2123 }
2124
2125 PyAttributeDef KX_Scene::Attributes[] = {
2126         KX_PYATTRIBUTE_RO_FUNCTION("name",                              KX_Scene, pyattr_get_name),
2127         KX_PYATTRIBUTE_RO_FUNCTION("objects",                   KX_Scene, pyattr_get_objects),
2128         KX_PYATTRIBUTE_RO_FUNCTION("objectsInactive",   KX_Scene, pyattr_get_objects_inactive),
2129         KX_PYATTRIBUTE_RO_FUNCTION("lights",                    KX_Scene, pyattr_get_lights),
2130         KX_PYATTRIBUTE_RO_FUNCTION("cameras",                   KX_Scene, pyattr_get_cameras),
2131         KX_PYATTRIBUTE_RO_FUNCTION("lights",                    KX_Scene, pyattr_get_lights),
2132         KX_PYATTRIBUTE_RW_FUNCTION("active_camera",             KX_Scene, pyattr_get_active_camera, pyattr_set_active_camera),
2133         KX_PYATTRIBUTE_RW_FUNCTION("pre_draw",                  KX_Scene, pyattr_get_drawing_callback_pre, pyattr_set_drawing_callback_pre),
2134         KX_PYATTRIBUTE_RW_FUNCTION("post_draw",                 KX_Scene, pyattr_get_drawing_callback_post, pyattr_set_drawing_callback_post),
2135         KX_PYATTRIBUTE_BOOL_RO("suspended",                             KX_Scene, m_suspend),
2136         KX_PYATTRIBUTE_BOOL_RO("activity_culling",              KX_Scene, m_activity_culling),
2137         KX_PYATTRIBUTE_FLOAT_RW("activity_culling_radius", 0.5f, FLT_MAX, KX_Scene, m_activity_box_radius),
2138         KX_PYATTRIBUTE_BOOL_RO("dbvt_culling",                  KX_Scene, m_dbvt_culling),
2139         { NULL }        //Sentinel
2140 };
2141
2142 KX_PYMETHODDEF_DOC(KX_Scene, addObject,
2143 "addObject(object, other, time=0)\n"
2144 "Returns the added object.\n")
2145 {
2146         PyObject *pyob, *pyother;
2147         KX_GameObject *ob, *other;
2148
2149         int time = 0;
2150
2151         if (!PyArg_ParseTuple(args, "OO|i:addObject", &pyob, &pyother, &time))
2152                 return NULL;
2153
2154         if (    !ConvertPythonToGameObject(pyob, &ob, false, "scene.addObject(object, other, time): KX_Scene (first argument)") ||
2155                         !ConvertPythonToGameObject(pyother, &other, false, "scene.addObject(object, other, time): KX_Scene (second argument)") )
2156                 return NULL;
2157
2158
2159         SCA_IObject* replica = AddReplicaObject((SCA_IObject*)ob, other, time);
2160         
2161         // release here because AddReplicaObject AddRef's
2162         // the object is added to the scene so we dont want python to own a reference
2163         replica->Release();
2164         return replica->GetProxy();
2165 }
2166
2167 KX_PYMETHODDEF_DOC(KX_Scene, end,
2168 "end()\n"
2169 "Removes this scene from the game.\n")
2170 {
2171         
2172         KX_GetActiveEngine()->RemoveScene(m_sceneName);
2173         
2174         Py_RETURN_NONE;
2175 }
2176
2177 KX_PYMETHODDEF_DOC(KX_Scene, restart,
2178                                    "restart()\n"
2179                                    "Restarts this scene.\n")
2180 {
2181         KX_GetActiveEngine()->ReplaceScene(m_sceneName, m_sceneName);
2182         
2183         Py_RETURN_NONE;
2184 }
2185
2186 KX_PYMETHODDEF_DOC(KX_Scene, replace,
2187                                    "replace(newScene)\n"
2188                                    "Replaces this scene with another one.\n")
2189 {
2190         char* name;
2191         
2192         if (!PyArg_ParseTuple(args, "s:replace", &name))
2193                 return NULL;
2194         
2195         KX_GetActiveEngine()->ReplaceScene(m_sceneName, name);
2196         
2197         Py_RETURN_NONE;
2198 }
2199
2200 KX_PYMETHODDEF_DOC(KX_Scene, suspend,
2201                                         "suspend()\n"
2202                                         "Suspends this scene.\n")
2203 {
2204         Suspend();
2205         
2206         Py_RETURN_NONE;
2207 }
2208
2209 KX_PYMETHODDEF_DOC(KX_Scene, resume,
2210                                         "resume()\n"
2211                                         "Resumes this scene.\n")
2212 {
2213         Resume();
2214         
2215         Py_RETURN_NONE;
2216 }
2217
2218 /* Matches python dict.get(key, [default]) */
2219 KX_PYMETHODDEF_DOC(KX_Scene, get, "")
2220 {
2221         PyObject *key;
2222         PyObject* def = Py_None;
2223         PyObject* ret;
2224
2225         if (!PyArg_ParseTuple(args, "O|O:get", &key, &def))
2226                 return NULL;
2227         
2228         if (m_attr_dict && (ret=PyDict_GetItem(m_attr_dict, key))) {
2229                 Py_INCREF(ret);
2230                 return ret;
2231         }
2232         
2233         Py_INCREF(def);
2234         return def;
2235 }
2236
2237 #endif // DISABLE_PYTHON