4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * Ketsji scene. Holds references to all scene data.
32 #pragma warning (disable : 4786)
36 #include "MT_assert.h"
37 #include "KX_KetsjiEngine.h"
38 #include "KX_BlenderMaterial.h"
39 #include "RAS_IPolygonMaterial.h"
40 #include "ListValue.h"
41 #include "SCA_LogicManager.h"
42 #include "SCA_TimeEventManager.h"
43 #include "SCA_AlwaysEventManager.h"
44 #include "SCA_RandomEventManager.h"
45 #include "KX_RayEventManager.h"
46 #include "KX_TouchEventManager.h"
47 #include "SCA_KeyboardManager.h"
48 #include "SCA_MouseManager.h"
49 #include "SCA_PropertyEventManager.h"
50 #include "SCA_ActuatorEventManager.h"
51 #include "KX_Camera.h"
52 #include "SCA_JoystickManager.h"
54 #include "RAS_MeshObject.h"
55 #include "BL_SkinMeshObject.h"
57 #include "RAS_IRasterizer.h"
58 #include "RAS_BucketManager.h"
60 #include "FloatValue.h"
61 #include "SCA_IController.h"
62 #include "SCA_IActuator.h"
64 #include "SYS_System.h"
65 #include "SG_Controller.h"
66 #include "SG_IObject.h"
68 #include "DNA_group_types.h"
69 #include "DNA_scene_types.h"
72 #include "KX_SG_NodeRelationships.h"
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"
82 #include "BL_ModifierDeformer.h"
83 #include "BL_ShapeDeformer.h"
84 #include "BL_DeformableGameObject.h"
87 #include "KX_ConvertPhysicsObject.h"
90 #include "CcdPhysicsEnvironment.h"
91 #include "CcdPhysicsController.h"
96 void* KX_SceneReplicationFunc(SG_IObject* node,void* gameobj,void* scene)
98 KX_GameObject* replica = ((KX_Scene*)scene)->AddNodeReplicaObject(node,(KX_GameObject*)gameobj);
103 return (void*)replica;
106 void* KX_SceneDestructionFunc(SG_IObject* node,void* gameobj,void* scene)
108 ((KX_Scene*)scene)->RemoveNodeDestructObject(node,(KX_GameObject*)gameobj);
113 bool KX_Scene::KX_ScenegraphUpdateFunc(SG_IObject* node,void* gameobj,void* scene)
115 return ((SG_Node*)node)->Schedule(((KX_Scene*)scene)->m_sghead);
118 bool KX_Scene::KX_ScenegraphRescheduleFunc(SG_IObject* node,void* gameobj,void* scene)
120 return ((SG_Node*)node)->Reschedule(((KX_Scene*)scene)->m_sghead);
123 SG_Callbacks KX_Scene::m_callbacks = SG_Callbacks(
124 KX_SceneReplicationFunc,
125 KX_SceneDestructionFunc,
126 KX_GameObject::UpdateTransformFunc,
127 KX_Scene::KX_ScenegraphUpdateFunc,
128 KX_Scene::KX_ScenegraphRescheduleFunc);
130 // temporarily var until there is a button in the userinterface
131 // (defined in KX_PythonInit.cpp)
132 extern bool gUseVisibilityTemp;
134 KX_Scene::KX_Scene(class SCA_IInputDevice* keyboarddevice,
135 class SCA_IInputDevice* mousedevice,
136 class NG_NetworkDeviceInterface *ndi,
137 const STR_String& sceneName,
142 m_sceneConverter(NULL),
143 m_physicsEnvironment(0),
144 m_sceneName(sceneName),
145 m_networkDeviceInterface(ndi),
146 m_active_camera(NULL),
147 m_ueberExecutionPriority(0),
148 m_blenderScene(scene)
150 m_suspendedtime = 0.0;
151 m_suspendeddelta = 0.0;
153 m_dbvt_culling = false;
154 m_dbvt_occlusion_res = 0;
155 m_activity_culling = false;
157 m_isclearingZbuffer = true;
158 m_tempObjectList = new CListValue();
159 m_objectlist = new CListValue();
160 m_parentlist = new CListValue();
161 m_lightlist= new CListValue();
162 m_inactivelist = new CListValue();
163 m_euthanasyobjects = new CListValue();
165 m_logicmgr = new SCA_LogicManager();
167 m_timemgr = new SCA_TimeEventManager(m_logicmgr);
168 m_keyboardmgr = new SCA_KeyboardManager(m_logicmgr,keyboarddevice);
169 m_mousemgr = new SCA_MouseManager(m_logicmgr,mousedevice);
171 SCA_AlwaysEventManager* alwaysmgr = new SCA_AlwaysEventManager(m_logicmgr);
172 SCA_PropertyEventManager* propmgr = new SCA_PropertyEventManager(m_logicmgr);
173 SCA_ActuatorEventManager* actmgr = new SCA_ActuatorEventManager(m_logicmgr);
174 SCA_RandomEventManager* rndmgr = new SCA_RandomEventManager(m_logicmgr);
175 KX_RayEventManager* raymgr = new KX_RayEventManager(m_logicmgr);
177 KX_NetworkEventManager* netmgr = new KX_NetworkEventManager(m_logicmgr, ndi);
181 m_logicmgr->RegisterEventManager(alwaysmgr);
182 m_logicmgr->RegisterEventManager(propmgr);
183 m_logicmgr->RegisterEventManager(actmgr);
184 m_logicmgr->RegisterEventManager(m_keyboardmgr);
185 m_logicmgr->RegisterEventManager(m_mousemgr);
186 m_logicmgr->RegisterEventManager(m_timemgr);
187 m_logicmgr->RegisterEventManager(rndmgr);
188 m_logicmgr->RegisterEventManager(raymgr);
189 m_logicmgr->RegisterEventManager(netmgr);
192 SYS_SystemHandle hSystem = SYS_GetSystem();
193 bool nojoystick= SYS_GetCommandLineInt(hSystem,"nojoystick",0);
196 SCA_JoystickManager *joymgr = new SCA_JoystickManager(m_logicmgr);
197 m_logicmgr->RegisterEventManager(joymgr);
200 MT_assert (m_networkDeviceInterface != NULL);
201 m_networkScene = new NG_NetworkScene(m_networkDeviceInterface);
205 m_bucketmanager=new RAS_BucketManager();
207 m_attr_dict = PyDict_New(); /* new ref */
212 KX_Scene::~KX_Scene()
214 // The release of debug properties used to be in SCA_IScene::~SCA_IScene
215 // It's still there but we remove all properties here otherwise some
216 // reference might be hanging and causing late release of objects
217 RemoveAllDebugProperties();
219 while (GetRootParentList()->GetCount() > 0)
221 KX_GameObject* parentobj = (KX_GameObject*) GetRootParentList()->GetValue(0);
222 this->RemoveObject(parentobj);
226 m_objectlist->Release();
229 m_parentlist->Release();
232 m_inactivelist->Release();
235 m_lightlist->Release();
237 if (m_tempObjectList)
238 m_tempObjectList->Release();
240 if (m_euthanasyobjects)
241 m_euthanasyobjects->Release();
246 if (m_physicsEnvironment)
247 delete m_physicsEnvironment;
250 delete m_networkScene;
254 delete m_bucketmanager;
256 PyDict_Clear(m_attr_dict);
257 Py_DECREF(m_attr_dict);
260 RAS_BucketManager* KX_Scene::GetBucketManager()
262 return m_bucketmanager;
267 CListValue* KX_Scene::GetObjectList()
274 CListValue* KX_Scene::GetRootParentList()
279 CListValue* KX_Scene::GetInactiveList()
281 return m_inactivelist;
286 CListValue* KX_Scene::GetLightList()
291 SCA_LogicManager* KX_Scene::GetLogicManager()
296 SCA_TimeEventManager* KX_Scene::GetTimeEventManager()
304 list<class KX_Camera*>* KX_Scene::GetCameras()
311 void KX_Scene::SetFramingType(RAS_FrameSettings & frame_settings)
313 m_frame_settings = frame_settings;
317 * Return a const reference to the framing
318 * type set by the above call.
319 * The contents are not guarenteed to be sensible
320 * if you don't call the above function.
322 const RAS_FrameSettings& KX_Scene::GetFramingType() const
324 return m_frame_settings;
330 * Store the current scene's viewport on the
331 * game engine canvas.
333 void KX_Scene::SetSceneViewport(const RAS_Rect &viewport)
335 m_viewport = viewport;
340 const RAS_Rect& KX_Scene::GetSceneViewport() const
347 void KX_Scene::SetWorldInfo(class KX_WorldInfo* worldinfo)
349 m_worldinfo = worldinfo;
354 class KX_WorldInfo* KX_Scene::GetWorldInfo()
360 const STR_String& KX_Scene::GetName()
366 void KX_Scene::Suspend()
371 void KX_Scene::Resume()
376 void KX_Scene::SetActivityCulling(bool b)
378 m_activity_culling = b;
381 bool KX_Scene::IsSuspended()
386 bool KX_Scene::IsClearingZBuffer()
388 return m_isclearingZbuffer;
391 void KX_Scene::EnableZBufferClearing(bool isclearingZbuffer)
393 m_isclearingZbuffer = isclearingZbuffer;
396 void KX_Scene::RemoveNodeDestructObject(class SG_IObject* node,class CValue* gameobj)
398 KX_GameObject* orgobj = (KX_GameObject*)gameobj;
399 if (NewRemoveObject(orgobj) != 0)
401 // object is not yet deleted because a reference is hanging somewhere.
402 // This should not happen anymore since we use proxy object for Python
403 // confident enough to put an assert?
405 printf("Zombie object! name=%s\n", orgobj->GetName().ReadPtr());
406 orgobj->SetSGNode(NULL);
407 PHY_IGraphicController* ctrl = orgobj->GetGraphicController();
410 // a graphic controller is set, we must delete it as the node will be deleted
412 orgobj->SetGraphicController(NULL);
419 KX_GameObject* KX_Scene::AddNodeReplicaObject(class SG_IObject* node, class CValue* gameobj)
421 // for group duplication, limit the duplication of the hierarchy to the
422 // objects that are part of the group.
423 if (!IsObjectInGroup(gameobj))
426 KX_GameObject* orgobj = (KX_GameObject*)gameobj;
427 KX_GameObject* newobj = (KX_GameObject*)orgobj->GetReplica();
428 m_map_gameobject_to_replica.insert(orgobj, newobj);
430 // also register 'timers' (time properties) of the replica
431 int numprops = newobj->GetPropertyCount();
433 for (int i = 0; i < numprops; i++)
435 CValue* prop = newobj->GetProperty(i);
437 if (prop->GetProperty("timer"))
438 this->m_timemgr->AddTimeProperty(prop);
443 newobj->SetSGNode((SG_Node*)node);
447 m_rootnode = new SG_Node(newobj,this,KX_Scene::m_callbacks);
449 // this fixes part of the scaling-added object bug
450 SG_Node* orgnode = orgobj->GetSGNode();
451 m_rootnode->SetLocalScale(orgnode->GetLocalScale());
452 m_rootnode->SetLocalPosition(orgnode->GetLocalPosition());
453 m_rootnode->SetLocalOrientation(orgnode->GetLocalOrientation());
455 // define the relationship between this node and it's parent.
456 KX_NormalParentRelation * parent_relation =
457 KX_NormalParentRelation::New();
458 m_rootnode->SetParentRelation(parent_relation);
460 newobj->SetSGNode(m_rootnode);
463 SG_IObject* replicanode = newobj->GetSGNode();
464 // SG_Node* rootnode = (replicanode == m_rootnode ? NULL : m_rootnode);
466 replicanode->SetSGClientObject(newobj);
468 // this is the list of object that are send to the graphics pipeline
469 m_objectlist->Add(newobj->AddRef());
470 if (newobj->IsLight())
471 m_lightlist->Add(newobj->AddRef());
472 newobj->AddMeshUser();
474 // logic cannot be replicated, until the whole hierarchy is replicated.
475 m_logicHierarchicalGameObjects.push_back(newobj);
476 //replicate controllers of this node
477 SGControllerList scenegraphcontrollers = orgobj->GetSGNode()->GetSGControllerList();
478 replicanode->RemoveAllControllers();
479 SGControllerList::iterator cit;
480 //int numcont = scenegraphcontrollers.size();
482 for (cit = scenegraphcontrollers.begin();!(cit==scenegraphcontrollers.end());++cit)
484 // controller replication is quite complicated
485 // only replicate ipo and physics controller for now
487 SG_Controller* replicacontroller = (*cit)->GetReplica((SG_Node*) replicanode);
488 if (replicacontroller)
490 replicacontroller->SetObject(replicanode);
491 replicanode->AddSGController(replicacontroller);
494 // replicate graphic controller
495 if (orgobj->GetGraphicController())
497 PHY_IMotionState* motionstate = new KX_MotionState(newobj->GetSGNode());
498 PHY_IGraphicController* newctrl = orgobj->GetGraphicController()->GetReplica(motionstate);
499 newctrl->setNewClientInfo(newobj->getClientInfo());
500 newobj->SetGraphicController(newctrl);
507 // before calling this method KX_Scene::ReplicateLogic(), make sure to
508 // have called 'GameObject::ReParentLogic' for each object this
509 // hierarchy that's because first ALL bricks must exist in the new
510 // replica of the hierarchy in order to make cross-links work properly
512 // It is VERY important that the order of sensors and actuators in
513 // the replicated object is preserved: it is is used to reconnect the logic.
514 // This method is more robust then using the bricks name in case of complex
515 // group replication. The replication of logic bricks is done in
516 // SCA_IObject::ReParentLogic(), make sure it preserves the order of the bricks.
517 void KX_Scene::ReplicateLogic(KX_GameObject* newobj)
519 // also relink the controller to sensors/actuators
520 SCA_ControllerList& controllers = newobj->GetControllers();
521 //SCA_SensorList& sensors = newobj->GetSensors();
522 //SCA_ActuatorList& actuators = newobj->GetActuators();
524 for (SCA_ControllerList::iterator itc = controllers.begin(); !(itc==controllers.end());itc++)
526 SCA_IController* cont = (*itc);
527 cont->SetUeberExecutePriority(m_ueberExecutionPriority);
528 vector<SCA_ISensor*> linkedsensors = cont->GetLinkedSensors();
529 vector<SCA_IActuator*> linkedactuators = cont->GetLinkedActuators();
531 // disconnect the sensors and actuators
532 // do it directly on the list at this controller is not connected to anything at this stage
533 cont->GetLinkedSensors().clear();
534 cont->GetLinkedActuators().clear();
536 // now relink each sensor
537 for (vector<SCA_ISensor*>::iterator its = linkedsensors.begin();!(its==linkedsensors.end());its++)
539 SCA_ISensor* oldsensor = (*its);
540 SCA_IObject* oldsensorobj = oldsensor->GetParent();
541 SCA_IObject* newsensorobj = NULL;
543 // the original owner of the sensor has been replicated?
544 void **h_obj = m_map_gameobject_to_replica[oldsensorobj];
546 newsensorobj = (SCA_IObject*)(*h_obj);
549 // no, then the sensor points outside the hierachy, keep it the same
550 if (m_objectlist->SearchValue(oldsensorobj))
551 // only replicate links that points to active objects
552 m_logicmgr->RegisterToSensor(cont,oldsensor);
556 // yes, then the new sensor has the same position
557 SCA_SensorList& sensorlist = oldsensorobj->GetSensors();
558 SCA_SensorList::iterator sit;
559 SCA_ISensor* newsensor = NULL;
562 for (sensorpos=0, sit=sensorlist.begin(); sit!=sensorlist.end(); sit++, sensorpos++)
564 if ((*sit) == oldsensor)
566 newsensor = newsensorobj->GetSensors().at(sensorpos);
570 assert(newsensor != NULL);
571 m_logicmgr->RegisterToSensor(cont,newsensor);
575 // now relink each actuator
576 for (vector<SCA_IActuator*>::iterator ita = linkedactuators.begin();!(ita==linkedactuators.end());ita++)
578 SCA_IActuator* oldactuator = (*ita);
579 SCA_IObject* oldactuatorobj = oldactuator->GetParent();
580 SCA_IObject* newactuatorobj = NULL;
582 // the original owner of the sensor has been replicated?
583 void **h_obj = m_map_gameobject_to_replica[oldactuatorobj];
585 newactuatorobj = (SCA_IObject*)(*h_obj);
589 // no, then the sensor points outside the hierachy, keep it the same
590 if (m_objectlist->SearchValue(oldactuatorobj))
591 // only replicate links that points to active objects
592 m_logicmgr->RegisterToActuator(cont,oldactuator);
596 // yes, then the new sensor has the same position
597 SCA_ActuatorList& actuatorlist = oldactuatorobj->GetActuators();
598 SCA_ActuatorList::iterator ait;
599 SCA_IActuator* newactuator = NULL;
602 for (actuatorpos=0, ait=actuatorlist.begin(); ait!=actuatorlist.end(); ait++, actuatorpos++)
604 if ((*ait) == oldactuator)
606 newactuator = newactuatorobj->GetActuators().at(actuatorpos);
610 assert(newactuator != NULL);
611 m_logicmgr->RegisterToActuator(cont,newactuator);
612 newactuator->SetUeberExecutePriority(m_ueberExecutionPriority);
616 // ready to set initial state
617 newobj->ResetState();
620 void KX_Scene::DupliGroupRecurse(CValue* obj, int level)
622 KX_GameObject* groupobj = (KX_GameObject*) obj;
623 KX_GameObject* replica;
624 KX_GameObject* gameobj;
625 Object* blgroupobj = groupobj->GetBlenderObject();
628 vector<KX_GameObject*> duplilist;
630 if (!groupobj->GetSGNode() ||
631 !groupobj->IsDupliGroup() ||
632 level>MAX_DUPLI_RECUR)
635 // we will add one group at a time
636 m_logicHierarchicalGameObjects.clear();
637 m_map_gameobject_to_replica.clear();
638 m_ueberExecutionPriority++;
639 // for groups will do something special:
640 // we will force the creation of objects to those in the group only
641 // Again, this is match what Blender is doing (it doesn't care of parent relationship)
642 m_groupGameObjects.clear();
644 group = blgroupobj->dup_group;
645 for(go=(GroupObject*)group->gobject.first; go; go=(GroupObject*)go->next)
647 Object* blenderobj = go->ob;
648 if (blgroupobj == blenderobj)
649 // this check is also in group_duplilist()
652 gameobj = (KX_GameObject*)m_logicmgr->FindGameObjByBlendObj(blenderobj);
655 // this object has not been converted!!!
656 // Should not happen as dupli group are created automatically
660 gameobj->SetBlenderGroupObject(blgroupobj);
662 if ((blenderobj->lay & group->layer)==0)
664 // object is not visible in the 3D view, will not be instantiated
667 m_groupGameObjects.insert(gameobj);
670 set<CValue*>::iterator oit;
671 for (oit=m_groupGameObjects.begin(); oit != m_groupGameObjects.end(); oit++)
673 gameobj = (KX_GameObject*)(*oit);
675 KX_GameObject *parent = gameobj->GetParent();
678 parent->Release(); // GetParent() increased the refcount
680 // this object is not a top parent. Either it is the child of another
681 // object in the group and it will be added automatically when the parent
682 // is added. Or it is the child of an object outside the group and the group
683 // is inconsistent, skip it anyway
686 replica = (KX_GameObject*) AddNodeReplicaObject(NULL,gameobj);
687 // add to 'rootparent' list (this is the list of top hierarchy objects, updated each frame)
688 m_parentlist->Add(replica->AddRef());
690 // recurse replication into children nodes
691 NodeList& children = gameobj->GetSGNode()->GetSGChildren();
693 replica->GetSGNode()->ClearSGChildren();
694 for (NodeList::iterator childit = children.begin();!(childit==children.end());++childit)
696 SG_Node* orgnode = (*childit);
697 SG_Node* childreplicanode = orgnode->GetSGReplica();
698 if (childreplicanode)
699 replica->GetSGNode()->AddChild(childreplicanode);
701 // don't replicate logic now: we assume that the objects in the group can have
702 // logic relationship, even outside parent relationship
703 // In order to match 3D view, the position of groupobj is used as a
704 // transformation matrix instead of the new position. This means that
705 // the group reference point is 0,0,0
707 // get the rootnode's scale
708 MT_Vector3 newscale = groupobj->NodeGetWorldScaling();
709 // set the replica's relative scale with the rootnode's scale
710 replica->NodeSetRelativeScale(newscale);
712 MT_Point3 offset(group->dupli_ofs);
713 MT_Point3 newpos = groupobj->NodeGetWorldPosition() +
714 newscale*(groupobj->NodeGetWorldOrientation() * (gameobj->NodeGetWorldPosition()-offset));
715 replica->NodeSetLocalPosition(newpos);
716 // set the orientation after position for softbody!
717 MT_Matrix3x3 newori = groupobj->NodeGetWorldOrientation() * gameobj->NodeGetWorldOrientation();
718 replica->NodeSetLocalOrientation(newori);
719 // update scenegraph for entire tree of children
720 replica->GetSGNode()->UpdateWorldData(0);
721 replica->GetSGNode()->SetBBox(gameobj->GetSGNode()->BBox());
722 replica->GetSGNode()->SetRadius(gameobj->GetSGNode()->Radius());
723 // we can now add the graphic controller to the physic engine
724 replica->ActivateGraphicController(true);
730 // the logic must be replicated first because we need
731 // the new logic bricks before relinking
732 vector<KX_GameObject*>::iterator git;
733 for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
735 (*git)->ReParentLogic();
738 // relink any pointers as necessary, sort of a temporary solution
739 for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
741 // this will also relink the actuator to objects within the hierarchy
742 (*git)->Relink(&m_map_gameobject_to_replica);
743 // add the object in the layer of the parent
744 (*git)->SetLayer(groupobj->GetLayer());
745 // If the object was a light, we need to update it's RAS_LightObject as well
746 if ((*git)->IsLight())
748 KX_LightObject* lightobj = static_cast<KX_LightObject*>(*git);
749 lightobj->GetLightData()->m_layer = groupobj->GetLayer();
753 // replicate crosslinks etc. between logic bricks
754 for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
756 ReplicateLogic((*git));
759 // now look if object in the hierarchy have dupli group and recurse
760 for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
762 if ((*git) != groupobj && (*git)->IsDupliGroup())
763 // can't instantiate group immediately as it destroys m_logicHierarchicalGameObjects
764 duplilist.push_back((*git));
767 for (git = duplilist.begin(); !(git == duplilist.end()); ++git)
769 DupliGroupRecurse((*git), level+1);
774 SCA_IObject* KX_Scene::AddReplicaObject(class CValue* originalobject,
775 class CValue* parentobject,
779 m_logicHierarchicalGameObjects.clear();
780 m_map_gameobject_to_replica.clear();
781 m_groupGameObjects.clear();
783 // todo: place a timebomb in the object, for temporarily objects :)
784 // lifespan of zero means 'this object lives forever'
785 KX_GameObject* originalobj = (KX_GameObject*) originalobject;
786 KX_GameObject* parentobj = (KX_GameObject*) parentobject;
788 m_ueberExecutionPriority++;
790 // lets create a replica
791 KX_GameObject* replica = (KX_GameObject*) AddNodeReplicaObject(NULL,originalobj);
795 // add a timebomb to this object
796 // for now, convert between so called frames and realtime
797 m_tempObjectList->Add(replica->AddRef());
798 CValue *fval = new CFloatValue(lifespan*0.02);
799 replica->SetProperty("::timebomb",fval);
803 // add to 'rootparent' list (this is the list of top hierarchy objects, updated each frame)
804 m_parentlist->Add(replica->AddRef());
806 // recurse replication into children nodes
808 NodeList& children = originalobj->GetSGNode()->GetSGChildren();
810 replica->GetSGNode()->ClearSGChildren();
811 for (NodeList::iterator childit = children.begin();!(childit==children.end());++childit)
813 SG_Node* orgnode = (*childit);
814 SG_Node* childreplicanode = orgnode->GetSGReplica();
815 if (childreplicanode)
816 replica->GetSGNode()->AddChild(childreplicanode);
819 // At this stage all the objects in the hierarchy have been duplicated,
820 // we can update the scenegraph, we need it for the duplication of logic
821 MT_Point3 newpos = ((KX_GameObject*) parentobject)->NodeGetWorldPosition();
822 replica->NodeSetLocalPosition(newpos);
824 MT_Matrix3x3 newori = ((KX_GameObject*) parentobject)->NodeGetWorldOrientation();
825 replica->NodeSetLocalOrientation(newori);
827 // get the rootnode's scale
828 MT_Vector3 newscale = parentobj->GetSGNode()->GetRootSGParent()->GetLocalScale();
830 // set the replica's relative scale with the rootnode's scale
831 replica->NodeSetRelativeScale(newscale);
833 replica->GetSGNode()->UpdateWorldData(0);
834 replica->GetSGNode()->SetBBox(originalobj->GetSGNode()->BBox());
835 replica->GetSGNode()->SetRadius(originalobj->GetSGNode()->Radius());
836 // the size is correct, we can add the graphic controller to the physic engine
837 replica->ActivateGraphicController(true);
839 // now replicate logic
840 vector<KX_GameObject*>::iterator git;
841 for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
843 (*git)->ReParentLogic();
846 // relink any pointers as necessary, sort of a temporary solution
847 for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
849 // this will also relink the actuators in the hierarchy
850 (*git)->Relink(&m_map_gameobject_to_replica);
851 // add the object in the layer of the parent
852 (*git)->SetLayer(parentobj->GetLayer());
853 // If the object was a light, we need to update it's RAS_LightObject as well
854 if ((*git)->IsLight())
856 KX_LightObject* lightobj = static_cast<KX_LightObject*>(*git);
857 lightobj->GetLightData()->m_layer = parentobj->GetLayer();
861 // replicate crosslinks etc. between logic bricks
862 for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
864 ReplicateLogic((*git));
867 // check if there are objects with dupligroup in the hierarchy
868 vector<KX_GameObject*> duplilist;
869 for (git = m_logicHierarchicalGameObjects.begin();!(git==m_logicHierarchicalGameObjects.end());++git)
871 if ((*git)->IsDupliGroup())
873 // separate list as m_logicHierarchicalGameObjects is also used by DupliGroupRecurse()
874 duplilist.push_back(*git);
877 for (git = duplilist.begin();!(git==duplilist.end());++git)
879 DupliGroupRecurse(*git, 0);
881 // don't release replica here because we are returning it, not done with it...
887 void KX_Scene::RemoveObject(class CValue* gameobj)
889 KX_GameObject* newobj = (KX_GameObject*) gameobj;
891 // disconnect child from parent
892 SG_Node* node = newobj->GetSGNode();
896 node->DisconnectFromParent();
898 // recursively destruct
901 //no need to do that: the object is destroyed and memory released
902 //newobj->SetSGNode(0);
905 void KX_Scene::DelayedRemoveObject(class CValue* gameobj)
907 //KX_GameObject* newobj = (KX_GameObject*) gameobj;
908 if (!m_euthanasyobjects->SearchValue(gameobj))
910 m_euthanasyobjects->Add(gameobj->AddRef());
916 int KX_Scene::NewRemoveObject(class CValue* gameobj)
919 KX_GameObject* newobj = (KX_GameObject*) gameobj;
921 /* Invalidate the python reference, since the object may exist in script lists
922 * its possible that it wont be automatically invalidated, so do it manually here,
924 * if for some reason the object is added back into the scene python can always get a new Proxy
926 newobj->InvalidateProxy();
928 // keep the blender->game object association up to date
929 // note that all the replicas of an object will have the same
930 // blender object, that's why we need to check the game object
931 // as only the deletion of the original object must be recorded
932 m_logicmgr->UnregisterGameObj(newobj->GetBlenderObject(), gameobj);
935 //GetPhysicsEnvironment()->RemovePhysicsController(gameobj->getPhysicsController());
937 // remove all sensors/controllers/actuators from logicsystem...
939 SCA_SensorList& sensors = newobj->GetSensors();
940 for (SCA_SensorList::iterator its = sensors.begin();
941 !(its==sensors.end());its++)
943 m_logicmgr->RemoveSensor(*its);
946 SCA_ControllerList& controllers = newobj->GetControllers();
947 for (SCA_ControllerList::iterator itc = controllers.begin();
948 !(itc==controllers.end());itc++)
950 m_logicmgr->RemoveController(*itc);
953 SCA_ActuatorList& actuators = newobj->GetActuators();
954 for (SCA_ActuatorList::iterator ita = actuators.begin();
955 !(ita==actuators.end());ita++)
957 m_logicmgr->RemoveActuator(*ita);
959 // the sensors/controllers/actuators must also be released, this is done in ~SCA_IObject
961 // now remove the timer properties from the time manager
962 int numprops = newobj->GetPropertyCount();
964 for (int i = 0; i < numprops; i++)
966 CValue* propval = newobj->GetProperty(i);
967 if (propval->GetProperty("timer"))
969 m_timemgr->RemoveTimeProperty(propval);
973 newobj->RemoveMeshes();
975 if (newobj->IsLight() && m_lightlist->RemoveValue(newobj))
976 ret = newobj->Release();
977 if (m_objectlist->RemoveValue(newobj))
978 ret = newobj->Release();
979 if (m_tempObjectList->RemoveValue(newobj))
980 ret = newobj->Release();
981 if (m_parentlist->RemoveValue(newobj))
982 ret = newobj->Release();
983 if (m_inactivelist->RemoveValue(newobj))
984 ret = newobj->Release();
985 if (m_euthanasyobjects->RemoveValue(newobj))
986 ret = newobj->Release();
988 if (newobj == m_active_camera)
990 //no AddRef done on m_active_camera so no Release
991 //m_active_camera->Release();
992 m_active_camera = NULL;
995 // in case this is a camera
996 m_cameras.remove((KX_Camera*)newobj);
998 /* currently does nothing, keep incase we need to Unregister something */
1000 if (m_sceneConverter)
1001 m_sceneConverter->UnregisterGameObject(newobj);
1004 // return value will be 0 if the object is actually deleted (all reference gone)
1011 void KX_Scene::ReplaceMesh(class CValue* obj,void* meshobj, bool use_gfx, bool use_phys)
1013 KX_GameObject* gameobj = static_cast<KX_GameObject*>(obj);
1014 RAS_MeshObject* mesh = static_cast<RAS_MeshObject*>(meshobj);
1017 std::cout << "KX_Scene::ReplaceMesh Warning: invalid object, doing nothing" << std::endl;
1021 if(use_gfx && mesh != NULL)
1023 gameobj->RemoveMeshes();
1024 gameobj->AddMesh(mesh);
1026 if (gameobj->m_isDeformable)
1028 BL_DeformableGameObject* newobj = static_cast<BL_DeformableGameObject*>( gameobj );
1030 if (newobj->GetDeformer())
1032 delete newobj->GetDeformer();
1033 newobj->SetDeformer(NULL);
1036 if (mesh->IsDeformed())
1038 // we must create a new deformer but which one?
1039 KX_GameObject* parentobj = newobj->GetParent();
1040 // this always return the original game object (also for replicate)
1041 Object* blendobj = newobj->GetBlenderObject();
1042 // object that owns the new mesh
1043 Object* oldblendobj = static_cast<struct Object*>(m_logicmgr->FindBlendObjByGameMeshName(mesh->GetName()));
1044 Mesh* blendmesh = mesh->GetMesh();
1046 bool bHasModifier = BL_ModifierDeformer::HasCompatibleDeformer(blendobj);
1047 bool bHasShapeKey = blendmesh->key != NULL && blendmesh->key->type==KEY_RELATIVE;
1048 bool bHasDvert = blendmesh->dvert != NULL;
1050 parentobj && // current parent is armature
1051 parentobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE &&
1052 oldblendobj && // needed for mesh deform
1053 blendobj->parent && // original object had armature (not sure this test is needed)
1054 blendobj->parent->type == OB_ARMATURE &&
1055 blendobj->partype==PARSKEL &&
1056 blendmesh->dvert!=NULL; // mesh has vertex group
1057 bool releaseParent = true;
1060 if (oldblendobj==NULL) {
1061 std::cout << "warning: ReplaceMesh() new mesh is not used in an object from the current scene, you will get incorrect behavior" << std::endl;
1062 bHasShapeKey= bHasDvert= bHasArmature=bHasModifier= false;
1067 BL_ModifierDeformer* modifierDeformer;
1068 if (bHasShapeKey || bHasArmature)
1070 modifierDeformer = new BL_ModifierDeformer(
1073 oldblendobj, blendobj,
1074 static_cast<BL_SkinMeshObject*>(mesh),
1076 static_cast<BL_ArmatureObject*>( parentobj )
1078 releaseParent= false;
1079 modifierDeformer->LoadShapeDrivers(blendobj->parent);
1083 modifierDeformer = new BL_ModifierDeformer(
1086 oldblendobj, blendobj,
1087 static_cast<BL_SkinMeshObject*>(mesh),
1092 newobj->SetDeformer(modifierDeformer);
1094 else if (bHasShapeKey)
1096 BL_ShapeDeformer* shapeDeformer;
1099 shapeDeformer = new BL_ShapeDeformer(
1101 oldblendobj, blendobj,
1102 static_cast<BL_SkinMeshObject*>(mesh),
1105 static_cast<BL_ArmatureObject*>( parentobj )
1107 releaseParent= false;
1108 shapeDeformer->LoadShapeDrivers(blendobj->parent);
1112 shapeDeformer = new BL_ShapeDeformer(
1114 oldblendobj, blendobj,
1115 static_cast<BL_SkinMeshObject*>(mesh),
1121 newobj->SetDeformer( shapeDeformer);
1123 else if (bHasArmature)
1125 BL_SkinDeformer* skinDeformer = new BL_SkinDeformer(
1127 oldblendobj, blendobj,
1128 static_cast<BL_SkinMeshObject*>(mesh),
1131 static_cast<BL_ArmatureObject*>( parentobj )
1133 releaseParent= false;
1134 newobj->SetDeformer(skinDeformer);
1138 BL_MeshDeformer* meshdeformer = new BL_MeshDeformer(
1139 newobj, oldblendobj, static_cast<BL_SkinMeshObject*>(mesh)
1141 newobj->SetDeformer(meshdeformer);
1144 // release parent reference if its not being used
1145 if( releaseParent && parentobj)
1146 parentobj->Release();
1150 gameobj->AddMeshUser();
1153 if(use_phys) { /* update the new assigned mesh with the physics mesh */
1154 KX_ReInstanceBulletShapeFromMesh(gameobj, NULL, use_gfx?NULL:mesh);
1158 KX_Camera* KX_Scene::FindCamera(KX_Camera* cam)
1160 list<KX_Camera*>::iterator it = m_cameras.begin();
1162 while ( (it != m_cameras.end())
1163 && ((*it) != cam) ) {
1167 return ((it == m_cameras.end()) ? NULL : (*it));
1171 KX_Camera* KX_Scene::FindCamera(STR_String& name)
1173 list<KX_Camera*>::iterator it = m_cameras.begin();
1175 while ( (it != m_cameras.end())
1176 && ((*it)->GetName() != name) ) {
1180 return ((it == m_cameras.end()) ? NULL : (*it));
1183 void KX_Scene::AddCamera(KX_Camera* cam)
1185 if (!FindCamera(cam))
1186 m_cameras.push_back(cam);
1190 KX_Camera* KX_Scene::GetActiveCamera()
1192 // NULL if not defined
1193 return m_active_camera;
1197 void KX_Scene::SetActiveCamera(KX_Camera* cam)
1199 // only set if the cam is in the active list? Or add it otherwise?
1200 if (!FindCamera(cam)){
1202 if (cam) std::cout << "Added cam " << cam->GetName() << std::endl;
1205 m_active_camera = cam;
1208 void KX_Scene::SetCameraOnTop(KX_Camera* cam)
1210 if (!FindCamera(cam)){
1211 // adding is always done at the back, so that's all that needs to be done
1213 if (cam) std::cout << "Added cam " << cam->GetName() << std::endl;
1215 m_cameras.remove(cam);
1216 m_cameras.push_back(cam);
1221 void KX_Scene::UpdateMeshTransformations()
1223 // do this incrementally in the future
1224 for (int i = 0; i < m_objectlist->GetCount(); i++)
1226 KX_GameObject* gameobj = (KX_GameObject*)m_objectlist->GetValue(i);
1227 gameobj->GetOpenGLMatrix();
1231 void KX_Scene::MarkVisible(SG_Tree *node, RAS_IRasterizer* rasty, KX_Camera* cam, int layer)
1233 int intersect = KX_Camera::INTERSECT;
1234 KX_GameObject *gameobj = node->Client()?(KX_GameObject*) node->Client()->GetSGClientObject():NULL;
1235 bool visible = (gameobj && gameobj->GetVisible() && (!layer || (gameobj->GetLayer() & layer)));
1236 bool dotest = visible || node->Left() || node->Right();
1238 /* If the camera is inside the box, assume intersect. */
1239 if (dotest && !node->inside( cam->NodeGetWorldPosition()))
1241 MT_Scalar radius = node->Radius();
1242 MT_Point3 center = node->Center();
1244 intersect = cam->SphereInsideFrustum(center, radius);
1246 if (intersect == KX_Camera::INTERSECT)
1250 intersect = cam->BoxInsideFrustum(box);
1256 case KX_Camera::OUTSIDE:
1257 MarkSubTreeVisible(node, rasty, false, cam);
1259 case KX_Camera::INTERSECT:
1261 MarkVisible(rasty, gameobj, cam, layer);
1263 MarkVisible(node->Left(), rasty, cam, layer);
1265 MarkVisible(node->Right(), rasty, cam, layer);
1267 case KX_Camera::INSIDE:
1268 MarkSubTreeVisible(node, rasty, true, cam, layer);
1273 void KX_Scene::MarkSubTreeVisible(SG_Tree *node, RAS_IRasterizer* rasty, bool visible, KX_Camera* cam, int layer)
1277 KX_GameObject *gameobj = (KX_GameObject*) node->Client()->GetSGClientObject();
1278 if (gameobj->GetVisible())
1282 int nummeshes = gameobj->GetMeshCount();
1284 // this adds the vertices to the display list
1285 for (int m=0;m<nummeshes;m++)
1286 (gameobj->GetMesh(m))->SchedulePolygons(rasty->GetDrawingMode());
1289 gameobj->SetCulled(!visible);
1290 gameobj->UpdateBuckets(false);
1294 MarkSubTreeVisible(node->Left(), rasty, visible, cam, layer);
1296 MarkSubTreeVisible(node->Right(), rasty, visible, cam, layer);
1299 void KX_Scene::MarkVisible(RAS_IRasterizer* rasty, KX_GameObject* gameobj,KX_Camera* cam,int layer)
1301 // User (Python/Actuator) has forced object invisible...
1302 if (!gameobj->GetSGNode() || !gameobj->GetVisible())
1305 // Shadow lamp layers
1306 if(layer && !(gameobj->GetLayer() & layer)) {
1307 gameobj->SetCulled(true);
1308 gameobj->UpdateBuckets(false);
1312 // If Frustum culling is off, the object is always visible.
1313 bool vis = !cam->GetFrustumCulling();
1315 // If the camera is inside this node, then the object is visible.
1318 vis = gameobj->GetSGNode()->inside( cam->GetCameraLocation() );
1321 // Test the object's bound sphere against the view frustum.
1324 MT_Vector3 scale = gameobj->GetSGNode()->GetWorldScaling();
1325 MT_Scalar radius = fabs(scale[scale.closestAxis()] * gameobj->GetSGNode()->Radius());
1326 switch (cam->SphereInsideFrustum(gameobj->NodeGetWorldPosition(), radius))
1328 case KX_Camera::INSIDE:
1331 case KX_Camera::OUTSIDE:
1334 case KX_Camera::INTERSECT:
1335 // Test the object's bound box against the view frustum.
1337 gameobj->GetSGNode()->getBBox(box);
1338 vis = cam->BoxInsideFrustum(box) != KX_Camera::OUTSIDE;
1345 int nummeshes = gameobj->GetMeshCount();
1347 for (int m=0;m<nummeshes;m++)
1349 // this adds the vertices to the display list
1350 (gameobj->GetMesh(m))->SchedulePolygons(rasty->GetDrawingMode());
1352 // Visibility/ non-visibility are marked
1354 gameobj->SetCulled(false);
1355 gameobj->UpdateBuckets(false);
1357 gameobj->SetCulled(true);
1358 gameobj->UpdateBuckets(false);
1362 void KX_Scene::PhysicsCullingCallback(KX_ClientObjectInfo* objectInfo, void* cullingInfo)
1364 KX_GameObject* gameobj = objectInfo->m_gameobject;
1365 if (!gameobj->GetVisible())
1366 // ideally, invisible objects should be removed from the culling tree temporarily
1368 if(((CullingInfo*)cullingInfo)->m_layer && !(gameobj->GetLayer() & ((CullingInfo*)cullingInfo)->m_layer))
1369 // used for shadow: object is not in shadow layer
1372 // make object visible
1373 gameobj->SetCulled(false);
1374 gameobj->UpdateBuckets(false);
1377 void KX_Scene::CalculateVisibleMeshes(RAS_IRasterizer* rasty,KX_Camera* cam, int layer)
1379 bool dbvt_culling = false;
1382 // test culling through Bullet
1383 PHY__Vector4 planes[6];
1384 // get the clip planes
1385 MT_Vector4* cplanes = cam->GetNormalizedClipPlanes();
1387 planes[0].setValue(cplanes[4].getValue()); // near
1388 planes[1].setValue(cplanes[5].getValue()); // far
1389 planes[2].setValue(cplanes[0].getValue()); // left
1390 planes[3].setValue(cplanes[1].getValue()); // right
1391 planes[4].setValue(cplanes[2].getValue()); // top
1392 planes[5].setValue(cplanes[3].getValue()); // bottom
1393 CullingInfo info(layer);
1394 dbvt_culling = m_physicsEnvironment->cullingTest(PhysicsCullingCallback,&info,planes,5,m_dbvt_occlusion_res);
1396 if (!dbvt_culling) {
1397 // the physics engine couldn't help us, do it the hard way
1398 for (int i = 0; i < m_objectlist->GetCount(); i++)
1400 MarkVisible(rasty, static_cast<KX_GameObject*>(m_objectlist->GetValue(i)), cam, layer);
1406 void KX_Scene::LogicBeginFrame(double curtime)
1408 // have a look at temp objects ...
1409 int lastobj = m_tempObjectList->GetCount() - 1;
1411 for (int i = lastobj; i >= 0; i--)
1413 CValue* objval = m_tempObjectList->GetValue(i);
1414 CFloatValue* propval = (CFloatValue*) objval->GetProperty("::timebomb");
1418 float timeleft = propval->GetNumber() - 1.0/KX_KetsjiEngine::GetTicRate();
1422 propval->SetFloat(timeleft);
1426 DelayedRemoveObject(objval);
1432 // all object is the tempObjectList should have a clock
1435 m_logicmgr->BeginFrame(curtime, 1.0/KX_KetsjiEngine::GetTicRate());
1440 void KX_Scene::LogicUpdateFrame(double curtime, bool frame)
1442 m_logicmgr->UpdateFrame(curtime, frame);
1447 void KX_Scene::LogicEndFrame()
1449 m_logicmgr->EndFrame();
1450 int numobj = m_euthanasyobjects->GetCount();
1454 while ((numobj = m_euthanasyobjects->GetCount()) > 0)
1456 // remove the object from this list to make sure we will not hit it again
1457 obj = (KX_GameObject*)m_euthanasyobjects->GetValue(numobj-1);
1458 m_euthanasyobjects->Remove(numobj-1);
1467 * UpdateParents: SceneGraph transformation update.
1469 void KX_Scene::UpdateParents(double curtime)
1471 // we use the SG dynamic list
1474 while ((node = SG_Node::GetNextScheduled(m_sghead)) != NULL)
1476 node->UpdateWorldData(curtime);
1479 //for (int i=0; i<GetRootParentList()->GetCount(); i++)
1481 // KX_GameObject* parentobj = (KX_GameObject*)GetRootParentList()->GetValue(i);
1482 // parentobj->NodeUpdateGS(curtime);
1485 // the list must be empty here
1486 assert(m_sghead.Empty());
1487 // some nodes may be ready for reschedule, move them to schedule list for next time
1488 while ((node = SG_Node::GetNextRescheduled(m_sghead)) != NULL)
1490 node->Schedule(m_sghead);
1495 RAS_MaterialBucket* KX_Scene::FindBucket(class RAS_IPolyMaterial* polymat, bool &bucketCreated)
1497 return m_bucketmanager->FindBucket(polymat, bucketCreated);
1502 void KX_Scene::RenderBuckets(const MT_Transform & cameratransform,
1503 class RAS_IRasterizer* rasty,
1504 class RAS_IRenderTools* rendertools)
1506 m_bucketmanager->Renderbuckets(cameratransform,rasty,rendertools);
1507 KX_BlenderMaterial::EndFrame();
1510 void KX_Scene::UpdateObjectActivity(void)
1512 if (m_activity_culling) {
1513 /* determine the activity criterium and set objects accordingly */
1516 MT_Point3 camloc = GetActiveCamera()->NodeGetWorldPosition(); //GetCameraLocation();
1518 for (i=0;i<GetObjectList()->GetCount();i++)
1520 KX_GameObject* ob = (KX_GameObject*) GetObjectList()->GetValue(i);
1522 if (!ob->GetIgnoreActivityCulling()) {
1523 /* Simple test: more than 10 away from the camera, count
1524 * Manhattan distance. */
1525 MT_Point3 obpos = ob->NodeGetWorldPosition();
1527 if ( (fabs(camloc[0] - obpos[0]) > m_activity_box_radius)
1528 || (fabs(camloc[1] - obpos[1]) > m_activity_box_radius)
1529 || (fabs(camloc[2] - obpos[2]) > m_activity_box_radius) )
1540 void KX_Scene::SetActivityCullingRadius(float f)
1544 m_activity_box_radius = f;
1547 NG_NetworkDeviceInterface* KX_Scene::GetNetworkDeviceInterface()
1549 return m_networkDeviceInterface;
1552 NG_NetworkScene* KX_Scene::GetNetworkScene()
1554 return m_networkScene;
1557 void KX_Scene::SetNetworkDeviceInterface(NG_NetworkDeviceInterface* newInterface)
1559 m_networkDeviceInterface = newInterface;
1562 void KX_Scene::SetNetworkScene(NG_NetworkScene *newScene)
1564 m_networkScene = newScene;
1568 void KX_Scene::SetGravity(const MT_Vector3& gravity)
1570 GetPhysicsEnvironment()->setGravity(gravity[0],gravity[1],gravity[2]);
1573 void KX_Scene::SetSceneConverter(class KX_BlenderSceneConverter* sceneConverter)
1575 m_sceneConverter = sceneConverter;
1578 void KX_Scene::SetPhysicsEnvironment(class PHY_IPhysicsEnvironment* physEnv)
1580 m_physicsEnvironment = physEnv;
1582 KX_TouchEventManager* touchmgr = new KX_TouchEventManager(m_logicmgr, physEnv);
1583 m_logicmgr->RegisterEventManager(touchmgr);
1587 void KX_Scene::setSuspendedTime(double suspendedtime)
1589 m_suspendedtime = suspendedtime;
1591 double KX_Scene::getSuspendedTime()
1593 return m_suspendedtime;
1595 void KX_Scene::setSuspendedDelta(double suspendeddelta)
1597 m_suspendeddelta = suspendeddelta;
1599 double KX_Scene::getSuspendedDelta()
1601 return m_suspendeddelta;
1604 //----------------------------------------------------------------------------
1607 PyTypeObject KX_Scene::Type = {
1608 PyVarObject_HEAD_INIT(NULL, 0)
1610 sizeof(PyObjectPlus_Proxy),
1619 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1629 PyMethodDef KX_Scene::Methods[] = {
1630 KX_PYMETHODTABLE_NOARGS(KX_Scene, getLightList),
1631 KX_PYMETHODTABLE_NOARGS(KX_Scene, getObjectList),
1632 KX_PYMETHODTABLE_NOARGS(KX_Scene, getName),
1633 KX_PYMETHODTABLE(KX_Scene, addObject),
1635 {NULL,NULL} //Sentinel
1638 PyObject* KX_Scene::pyattr_get_name(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
1640 KX_Scene* self= static_cast<KX_Scene*>(self_v);
1641 return PyUnicode_FromString(self->GetName().ReadPtr());
1644 PyObject* KX_Scene::pyattr_get_objects(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
1646 KX_Scene* self= static_cast<KX_Scene*>(self_v);
1647 return self->GetObjectList()->GetProxy();
1650 PyObject* KX_Scene::pyattr_get_objects_inactive(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
1652 KX_Scene* self= static_cast<KX_Scene*>(self_v);
1653 return self->GetInactiveList()->GetProxy();
1656 PyObject* KX_Scene::pyattr_get_lights(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
1658 KX_Scene* self= static_cast<KX_Scene*>(self_v);
1659 return self->GetLightList()->GetProxy();
1662 PyObject* KX_Scene::pyattr_get_cameras(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
1664 /* With refcounts in this case...
1665 * the new CListValue is owned by python, so its possible python holds onto it longer then the BGE
1666 * however this is the same with "scene.objects + []", when you make a copy by adding lists.
1669 KX_Scene* self= static_cast<KX_Scene*>(self_v);
1670 CListValue* clist = new CListValue();
1672 /* return self->GetCameras()->GetProxy(); */
1674 list<KX_Camera*>::iterator it = self->GetCameras()->begin();
1675 while (it != self->GetCameras()->end()) {
1676 clist->Add((*it)->AddRef());
1680 return clist->NewProxy(true);
1683 PyObject* KX_Scene::pyattr_get_active_camera(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
1685 KX_Scene* self= static_cast<KX_Scene*>(self_v);
1686 return self->GetActiveCamera()->GetProxy();
1690 int KX_Scene::pyattr_set_active_camera(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
1692 KX_Scene* self= static_cast<KX_Scene*>(self_v);
1695 if (!ConvertPythonToCamera(value, &camOb, false, "scene.active_camera = value: KX_Scene"))
1696 return PY_SET_ATTR_FAIL;
1698 self->SetActiveCamera(camOb);
1699 return PY_SET_ATTR_SUCCESS;
1703 PyAttributeDef KX_Scene::Attributes[] = {
1704 KX_PYATTRIBUTE_RO_FUNCTION("name", KX_Scene, pyattr_get_name),
1705 KX_PYATTRIBUTE_RO_FUNCTION("objects", KX_Scene, pyattr_get_objects),
1706 KX_PYATTRIBUTE_RO_FUNCTION("objectsInactive", KX_Scene, pyattr_get_objects_inactive), KX_PYATTRIBUTE_RO_FUNCTION("lights", KX_Scene, pyattr_get_lights),
1707 KX_PYATTRIBUTE_RO_FUNCTION("cameras", KX_Scene, pyattr_get_cameras),
1708 KX_PYATTRIBUTE_RO_FUNCTION("lights", KX_Scene, pyattr_get_lights),
1709 KX_PYATTRIBUTE_RW_FUNCTION("active_camera", KX_Scene, pyattr_get_active_camera, pyattr_set_active_camera),
1710 KX_PYATTRIBUTE_BOOL_RO("suspended", KX_Scene, m_suspend),
1711 KX_PYATTRIBUTE_BOOL_RO("activity_culling", KX_Scene, m_activity_culling),
1712 KX_PYATTRIBUTE_FLOAT_RW("activity_culling_radius", 0.5f, FLT_MAX, KX_Scene, m_activity_box_radius),
1713 KX_PYATTRIBUTE_BOOL_RO("dbvt_culling", KX_Scene, m_dbvt_culling),
1717 KX_PYMETHODDEF_DOC_NOARGS(KX_Scene, getLightList,
1718 "getLightList() -> list [KX_Light]\n"
1719 "Returns a list of all lights in the scene.\n"
1722 ShowDeprecationWarning("getLightList()", "the lights property");
1723 return m_lightlist->GetProxy();
1726 KX_PYMETHODDEF_DOC_NOARGS(KX_Scene, getObjectList,
1727 "getObjectList() -> list [KX_GameObject]\n"
1728 "Returns a list of all game objects in the scene.\n"
1731 ShowDeprecationWarning("getObjectList()", "the objects property");
1732 return m_objectlist->GetProxy();
1735 KX_PYMETHODDEF_DOC_NOARGS(KX_Scene, getName,
1736 "getName() -> string\n"
1737 "Returns the name of the scene.\n"
1740 ShowDeprecationWarning("getName()", "the name property");
1741 return PyUnicode_FromString(GetName());
1744 KX_PYMETHODDEF_DOC(KX_Scene, addObject,
1745 "addObject(object, other, time=0)\n"
1746 "Returns the added object.\n")
1748 PyObject *pyob, *pyother;
1749 KX_GameObject *ob, *other;
1753 if (!PyArg_ParseTuple(args, "OO|i:addObject", &pyob, &pyother, &time))
1756 if ( !ConvertPythonToGameObject(pyob, &ob, false, "scene.addObject(object, other, time): KX_Scene (first argument)") ||
1757 !ConvertPythonToGameObject(pyother, &other, false, "scene.addObject(object, other, time): KX_Scene (second argument)") )
1761 SCA_IObject* replica = AddReplicaObject((SCA_IObject*)ob, other, time);
1763 // release here because AddReplicaObject AddRef's
1764 // the object is added to the scene so we dont want python to own a reference
1766 return replica->GetProxy();