svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r22717:22875
[blender.git] / source / gameengine / Converter / BL_BlenderDataConversion.cpp
index 1d2b275..2308207 100644 (file)
 #include "KX_KetsjiEngine.h"
 #include "KX_BlenderSceneConverter.h"
 
-#include"SND_Scene.h"
-#include "SND_SoundListener.h"
-
 /* This little block needed for linking to Blender... */
 #ifdef WIN32
 #include "BLI_winstuff.h"
@@ -163,7 +160,6 @@ extern "C" {
 #include "SG_BBox.h"
 #include "SG_Tree.h"
 
-// defines USE_ODE to choose physics engine
 #include "KX_ConvertPhysicsObject.h"
 #ifdef USE_BULLET
 #include "CcdPhysicsEnvironment.h"
@@ -183,8 +179,8 @@ extern "C" {
 #ifdef __cplusplus
 extern "C" {
 #endif
-#include "BSE_headerbuttons.h"
-void update_for_newframe();
+//XXX #include "BSE_headerbuttons.h"
+//XXX void update_for_newframe();
 //void scene_update_for_newframe(struct Scene *sce, unsigned int lay);
 //#include "BKE_ipo.h"
 //void do_all_data_ipos(void);
@@ -324,9 +320,7 @@ bool ConvertMaterial(
        MTFace* tface,  
        const char *tfaceName,
        MFace* mface, 
-       MCol* mmcol, 
-       int lightlayer, 
-       Object* blenderobj,
+       MCol* mmcol,
        MTF_localLayer *layers,
        bool glslmat)
 {
@@ -560,7 +554,7 @@ bool ConvertMaterial(
                material->ref                   = mat->ref;
                material->amb                   = mat->amb;
 
-               material->ras_mode |= (mat->mode & MA_WIRE)? WIRE: 0;
+               material->ras_mode |= (mat->material_type == MA_TYPE_WIRE)? WIRE: 0;
        }
        else {
                int valid = 0;
@@ -629,7 +623,7 @@ bool ConvertMaterial(
        }
 
        // with ztransp enabled, enforce alpha blending mode
-       if(validmat && (mat->mode & MA_ZTRA) && (material->transp == TF_SOLID))
+       if(validmat && (mat->mode & MA_TRANSP) && (mat->mode & MA_ZTRANSP) && (material->transp == TF_SOLID))
                material->transp = TF_ALPHA;
 
        // always zsort alpha + add
@@ -722,13 +716,15 @@ bool ConvertMaterial(
        return true;
 }
 
-
-RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools* rendertools, KX_Scene* scene, KX_BlenderSceneConverter *converter)
+/* blenderobj can be NULL, make sure its checked for */
+RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, KX_Scene* scene, KX_BlenderSceneConverter *converter)
 {
        RAS_MeshObject *meshobj;
        bool skinMesh = false;
-       int lightlayer = blenderobj->lay;
+       int lightlayer = blenderobj ? blenderobj->lay:(1<<20)-1; // all layers if no object.
 
+       if ((meshobj = converter->FindGameMesh(mesh/*, ob->lay*/)) != NULL)
+               return meshobj;
        // Get DerivedMesh data
        DerivedMesh *dm = CDDM_from_mesh(mesh, blenderobj);
 
@@ -748,13 +744,13 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools*
        }
 
        // Determine if we need to make a skinned mesh
-       if (mesh->dvert || mesh->key || ((blenderobj->gameflag & OB_SOFT_BODY) != 0) || BL_ModifierDeformer::HasCompatibleDeformer(blenderobj))
+       if (blenderobj && (mesh->dvert || mesh->key || ((blenderobj->gameflag & OB_SOFT_BODY) != 0) || BL_ModifierDeformer::HasCompatibleDeformer(blenderobj)))
        {
-               meshobj = new BL_SkinMeshObject(mesh, lightlayer);
+               meshobj = new BL_SkinMeshObject(mesh);
                skinMesh = true;
        }
        else
-               meshobj = new RAS_MeshObject(mesh, lightlayer);
+               meshobj = new RAS_MeshObject(mesh);
 
        // Extract avaiable layers
        MTF_localLayer *layers =  new MTF_localLayer[MAX_MTFACE];
@@ -843,9 +839,8 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools*
                                tan3 = tangent[f*4 + 3];
                }
 
-               /* get material */
-               ma = give_current_material(blenderobj, mface->mat_nr+1);
-       
+               ma = give_current_material(blenderobj, mface->mat_nr+1);
+
                {
                        bool visible = true;
                        bool twoside = false;
@@ -859,7 +854,7 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools*
                                if (!bl_mat)
                                        bl_mat = new BL_Material();
                                ConvertMaterial(bl_mat, ma, tface, tfaceName, mface, mcol,
-                                       lightlayer, blenderobj, layers, converter->GetGLSLMaterials());
+                                       layers, converter->GetGLSLMaterials());
 
                                visible = ((bl_mat->ras_mode & POLY_VIS)!=0);
                                collider = ((bl_mat->ras_mode & COLLIDER)!=0);
@@ -882,7 +877,7 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools*
                                if (kx_blmat == NULL)
                                        kx_blmat = new KX_BlenderMaterial();
 
-                               kx_blmat->Initialize(scene, bl_mat, skinMesh, lightlayer);
+                               kx_blmat->Initialize(scene, bl_mat, skinMesh);
                                polymat = static_cast<RAS_IPolyMaterial*>(kx_blmat);
                        }
                        else {
@@ -1038,12 +1033,15 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools*
                        layer.face++;
                }
        }
-       meshobj->m_sharedvertex_map.clear();
+       // keep meshobj->m_sharedvertex_map for reinstance phys mesh.
+       // 2.49a and before it did: meshobj->m_sharedvertex_map.clear();
+       // but this didnt save much ram. - Campbell
+       meshobj->EndConversion();
 
        // pre calculate texture generation
        for(list<RAS_MeshMaterial>::iterator mit = meshobj->GetFirstMaterial();
                mit != meshobj->GetLastMaterial(); ++ mit) {
-               mit->m_bucket->GetPolyMaterial()->OnConstruction();
+               mit->m_bucket->GetPolyMaterial()->OnConstruction(lightlayer);
        }
 
        if (layers)
@@ -1057,13 +1055,13 @@ RAS_MeshObject* BL_ConvertMesh(Mesh* mesh, Object* blenderobj, RAS_IRenderTools*
                delete kx_blmat;
        if (kx_polymat)
                delete kx_polymat;
+       converter->RegisterGameMesh(meshobj, mesh);
        return meshobj;
 }
 
        
        
-static PHY_MaterialProps *CreateMaterialFromBlenderObject(struct Object* blenderobject,
-                                                                                                 KX_Scene *kxscene)
+static PHY_MaterialProps *CreateMaterialFromBlenderObject(struct Object* blenderobject)
 {
        PHY_MaterialProps *materialProps = new PHY_MaterialProps;
        
@@ -1096,8 +1094,7 @@ static PHY_MaterialProps *CreateMaterialFromBlenderObject(struct Object* blender
        return materialProps;
 }
 
-static PHY_ShapeProps *CreateShapePropsFromBlenderObject(struct Object* blenderobject,
-                                                                                                KX_Scene *kxscene)
+static PHY_ShapeProps *CreateShapePropsFromBlenderObject(struct Object* blenderobject)
 {
        PHY_ShapeProps *shapeProps = new PHY_ShapeProps;
        
@@ -1249,18 +1246,34 @@ static void my_tex_space_mesh(Mesh *me)
        
 }
 
-static void my_get_local_bounds(Object *ob, float *center, float *size)
+static void my_get_local_bounds(Object *ob, DerivedMesh *dm, float *center, float *size)
 {
        BoundBox *bb= NULL;
        /* uses boundbox, function used by Ketsji */
        switch (ob->type)
        {
                case OB_MESH:
-                       bb= ( (Mesh *)ob->data )->bb;
-                       if(bb==0) 
+                       if (dm)
+                       {
+                               float min_r[3], max_r[3];
+                               INIT_MINMAX(min_r, max_r);
+                               dm->getMinMax(dm, min_r, max_r);
+                               size[0]= 0.5*fabs(max_r[0] - min_r[0]);
+                               size[1]= 0.5*fabs(max_r[1] - min_r[1]);
+                               size[2]= 0.5*fabs(max_r[2] - min_r[2]);
+                                       
+                               center[0]= 0.5*(max_r[0] + min_r[0]);
+                               center[1]= 0.5*(max_r[1] + min_r[1]);
+                               center[2]= 0.5*(max_r[2] + min_r[2]);
+                               return;
+                       } else
                        {
-                               my_tex_space_mesh((struct Mesh *)ob->data);
                                bb= ( (Mesh *)ob->data )->bb;
+                               if(bb==0) 
+                               {
+                                       my_tex_space_mesh((struct Mesh *)ob->data);
+                                       bb= ( (Mesh *)ob->data )->bb;
+                               }
                        }
                        break;
                case OB_CURVE:
@@ -1319,8 +1332,9 @@ void BL_CreateGraphicObjectNew(KX_GameObject* gameobj,
                                ctrl->setNewClientInfo(gameobj->getClientInfo());
                                ctrl->setLocalAabb(localAabbMin, localAabbMax);
                                if (isActive) {
-                                       // add first, this will create the proxy handle
-                                       env->addCcdGraphicController(ctrl);
+                                       // add first, this will create the proxy handle, only if the object is visible
+                                       if (gameobj->GetVisible())
+                                               env->addCcdGraphicController(ctrl);
                                        // update the mesh if there is a deformer, this will also update the bounding box for modifiers
                                        RAS_Deformer* deformer = gameobj->GetDeformer();
                                        if (deformer)
@@ -1361,10 +1375,13 @@ void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
        }
 
        bool isCompoundChild = false;
+       bool hasCompoundChildren = !parent && (blenderobject->gameflag & OB_CHILD);
 
-       if (parent && (parent->gameflag & OB_DYNAMIC)) {
+       /* When the parent is not OB_DYNAMIC and has no OB_COLLISION then it gets no bullet controller
+        * and cant be apart of the parents compound shape */
+       if (parent && (parent->gameflag & (OB_DYNAMIC | OB_COLLISION))) {
                
-               if ((parent->gameflag & OB_CHILD) != 0)
+               if ((parent->gameflag & OB_CHILD) != 0 && (blenderobject->gameflag & OB_CHILD))
                {
                        isCompoundChild = true;
                } 
@@ -1374,12 +1391,11 @@ void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
 
 
        PHY_ShapeProps* shapeprops =
-                       CreateShapePropsFromBlenderObject(blenderobject, 
-                       kxscene);
+                       CreateShapePropsFromBlenderObject(blenderobject);
 
        
        PHY_MaterialProps* smmaterial = 
-               CreateMaterialFromBlenderObject(blenderobject, kxscene);
+               CreateMaterialFromBlenderObject(blenderobject);
                                        
        KX_ObjectProperties objprop;
        objprop.m_lockXaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_X_AXIS) !=0;
@@ -1390,14 +1406,26 @@ void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
        objprop.m_lockZRotaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_Z_ROT_AXIS) !=0;
 
        objprop.m_isCompoundChild = isCompoundChild;
-       objprop.m_hasCompoundChildren = (blenderobject->gameflag & OB_CHILD) != 0;
+       objprop.m_hasCompoundChildren = hasCompoundChildren;
        objprop.m_margin = blenderobject->margin;
+       
        // ACTOR is now a separate feature
        objprop.m_isactor = (blenderobject->gameflag & OB_ACTOR)!=0;
        objprop.m_dyna = (blenderobject->gameflag & OB_DYNAMIC) != 0;
        objprop.m_softbody = (blenderobject->gameflag & OB_SOFT_BODY) != 0;
        objprop.m_angular_rigidbody = (blenderobject->gameflag & OB_RIGID_BODY) != 0;
        
+       ///contact processing threshold is only for rigid bodies and static geometry, not 'dynamic'
+       if (objprop.m_angular_rigidbody || !objprop.m_dyna )
+       {
+               objprop.m_contactProcessingThreshold = blenderobject->m_contactProcessingThreshold;
+       } else
+       {
+               objprop.m_contactProcessingThreshold = 0.f;
+       }
+
+       objprop.m_sensor = (blenderobject->gameflag & OB_SENSOR) != 0;
+       
        if (objprop.m_softbody)
        {
                ///for game soft bodies
@@ -1439,7 +1467,8 @@ void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
                        objprop.m_soft_collisionflags= blenderobject->bsoft->collisionflags;    /* Vertex/Face or Signed Distance Field(SDF) or Clusters, Soft versus Soft or Rigid */
                        objprop.m_soft_numclusteriterations= blenderobject->bsoft->numclusteriterations;        /* number of iterations to refine collision clusters*/
                        objprop.m_soft_welding = blenderobject->bsoft->welding;         /* welding */
-               
+                       objprop.m_margin = blenderobject->bsoft->margin;
+                       objprop.m_contactProcessingThreshold = 0.f;
                } else
                {
                        objprop.m_gamesoftFlag = OB_BSB_BENDING_CONSTRAINTS | OB_BSB_SHAPE_MATCHING | OB_BSB_AERO_VPOINT;
@@ -1479,6 +1508,8 @@ void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
                        objprop.m_soft_collisionflags= OB_BSB_COL_SDF_RS + OB_BSB_COL_VF_SS;
                        objprop.m_soft_numclusteriterations= 16;
                        objprop.m_soft_welding = 0.f;
+                       objprop.m_margin = 0.f;
+                       objprop.m_contactProcessingThreshold = 0.f;
                }
        }
 
@@ -1498,7 +1529,10 @@ void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
        }
 
        KX_BoxBounds bb;
-       my_get_local_bounds(blenderobject,objprop.m_boundobject.box.m_center,bb.m_extends);
+       DerivedMesh* dm = NULL;
+       if (gameobj->GetDeformer())
+               dm = gameobj->GetDeformer()->GetFinalMesh();
+       my_get_local_bounds(blenderobject,dm,objprop.m_boundobject.box.m_center,bb.m_extends);
        if (blenderobject->gameflag & OB_BOUNDS)
        {
                switch (blenderobject->boundtype)
@@ -1550,8 +1584,8 @@ void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
        }
 
        
-       if (parent && (parent->gameflag & OB_DYNAMIC)) {
-               
+       if (parent/* && (parent->gameflag & OB_DYNAMIC)*/) {
+               // parented object cannot be dynamic
                KX_GameObject *parentgameobject = converter->FindGameObject(parent);
                objprop.m_dynamic_parent = parentgameobject;
                //cannot be dynamic:
@@ -1567,22 +1601,10 @@ void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
        {
 #ifdef USE_BULLET
                case UseBullet:
-                       KX_ConvertBulletObject(gameobj, meshobj, kxscene, shapeprops, smmaterial, &objprop);
+                       KX_ConvertBulletObject(gameobj, meshobj, dm, kxscene, shapeprops, smmaterial, &objprop);
                        break;
 
 #endif
-#ifdef USE_SUMO_SOLID
-               case UseSumo:
-                       KX_ConvertSumoObject(gameobj, meshobj, kxscene, shapeprops, smmaterial, &objprop);
-                       break;
-#endif
-                       
-#ifdef USE_ODE
-               case UseODE:
-                       KX_ConvertODEEngineObject(gameobj, meshobj, kxscene, shapeprops, smmaterial, &objprop);
-                       break;
-#endif //USE_ODE
-
                case UseDynamo:
                        //KX_ConvertDynamoObject(gameobj,meshobj,kxscene,shapeprops,    smmaterial,     &objprop);
                        break;
@@ -1657,8 +1679,7 @@ static KX_GameObject *gameobject_from_blenderobject(
                                                                Object *ob, 
                                                                KX_Scene *kxscene, 
                                                                RAS_IRenderTools *rendertools, 
-                                                               KX_BlenderSceneConverter *converter,
-                                                               Scene *blenderscene) 
+                                                               KX_BlenderSceneConverter *converter) 
 {
        KX_GameObject *gameobj = NULL;
        
@@ -1690,14 +1711,9 @@ static KX_GameObject *gameobject_from_blenderobject(
        case OB_MESH:
        {
                Mesh* mesh = static_cast<Mesh*>(ob->data);
-               RAS_MeshObject* meshobj = converter->FindGameMesh(mesh, ob->lay);
                float center[3], extents[3];
                float radius = my_boundbox_mesh((Mesh*) ob->data, center, extents);
-               
-               if (!meshobj) {
-                       meshobj = BL_ConvertMesh(mesh,ob,rendertools,kxscene,converter);
-                       converter->RegisterGameMesh(meshobj, mesh);
-               }
+               RAS_MeshObject* meshobj = BL_ConvertMesh(mesh,ob,kxscene,converter);
                
                // needed for python scripting
                kxscene->GetLogicManager()->RegisterMeshName(meshobj->GetName(),meshobj);
@@ -1723,7 +1739,7 @@ static KX_GameObject *gameobject_from_blenderobject(
 
                if (bHasModifier) {
                        BL_ModifierDeformer *dcont = new BL_ModifierDeformer((BL_DeformableGameObject *)gameobj,
-                                                                                                                               ob,     (BL_SkinMeshObject *)meshobj);
+                                                                                                                               kxscene->GetBlenderScene(), ob, (BL_SkinMeshObject *)meshobj);
                        ((BL_DeformableGameObject*)gameobj)->SetDeformer(dcont);
                        if (bHasShapeKey && bHasArmature)
                                dcont->LoadShapeDrivers(ob->parent);
@@ -1761,7 +1777,8 @@ static KX_GameObject *gameobject_from_blenderobject(
                gameobj = new BL_ArmatureObject(
                        kxscene,
                        KX_Scene::m_callbacks,
-                       ob // handle
+                       ob,
+                       kxscene->GetBlenderScene() // handle
                );
                /* Get the current pose from the armature object and apply it as the rest pose */
                break;
@@ -1776,7 +1793,6 @@ static KX_GameObject *gameobject_from_blenderobject(
        }
        if (gameobj) 
        {
-               gameobj->SetPhysicsEnvironment(kxscene->GetPhysicsEnvironment());
                gameobj->SetLayer(ob->lay);
                gameobj->SetBlenderObject(ob);
                /* set the visibility state based on the objects render option in the outliner */
@@ -1791,7 +1807,7 @@ struct parentChildLink {
 };
 
 #include "DNA_constraint_types.h"
-#include "BIF_editconstraint.h"
+//XXX #include "BIF_editconstraint.h"
 
 bPoseChannel *get_active_posechannel2 (Object *ob)
 {
@@ -1812,7 +1828,8 @@ ListBase *get_active_constraints2(Object *ob)
        if (!ob)
                return NULL;
 
-       if (ob->flag & OB_POSEMODE) {
+  // XXX - shouldnt we care about the pose data and not the mode???
+       if (ob->mode & OB_MODE_POSE) { 
                bPoseChannel *pchan;
 
                pchan = get_active_posechannel2(ob);
@@ -1875,12 +1892,10 @@ KX_GameObject* getGameOb(STR_String busc,CListValue* sumolist){
 
 // convert blender objects into ketsji gameobjects
 void BL_ConvertBlenderObjects(struct Main* maggie,
-                                                         const STR_String& scenename,
                                                          KX_Scene* kxscene,
                                                          KX_KetsjiEngine* ketsjiEngine,
                                                          e_PhysicsEngine       physics_engine,
                                                          PyObject* pythondictionary,
-                                                         SCA_IInputDevice* keydev,
                                                          RAS_IRenderTools* rendertools,
                                                          RAS_ICanvas* canvas,
                                                          KX_BlenderSceneConverter* converter,
@@ -1888,7 +1903,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                                                          )
 {      
 
-       Scene *blenderscene = converter->GetBlenderSceneForName(scenename);
+       Scene *blenderscene = kxscene->GetBlenderScene();
        // for SETLOOPER
        Scene *sce;
        Base *base;
@@ -1909,46 +1924,41 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                aspect_width = canvas->GetWidth();
                aspect_height = canvas->GetHeight();
        } else {
-               if (blenderscene->framing.type == SCE_GAMEFRAMING_BARS) {
+               if (blenderscene->gm.framing.type == SCE_GAMEFRAMING_BARS) {
                        frame_type = RAS_FrameSettings::e_frame_bars;
-               } else if (blenderscene->framing.type == SCE_GAMEFRAMING_EXTEND) {
+               } else if (blenderscene->gm.framing.type == SCE_GAMEFRAMING_EXTEND) {
                        frame_type = RAS_FrameSettings::e_frame_extend;
                } else {
                        frame_type = RAS_FrameSettings::e_frame_scale;
                }
                
-               aspect_width = blenderscene->r.xsch;
-               aspect_height = blenderscene->r.ysch;
+               aspect_width = blenderscene->gm.xsch;
+               aspect_height = blenderscene->gm.ysch;
        }
        
        RAS_FrameSettings frame_settings(
                frame_type,
-               blenderscene->framing.col[0],
-               blenderscene->framing.col[1],
-               blenderscene->framing.col[2],
+               blenderscene->gm.framing.col[0],
+               blenderscene->gm.framing.col[1],
+               blenderscene->gm.framing.col[2],
                aspect_width,
                aspect_height
        );
        kxscene->SetFramingType(frame_settings);
 
-       kxscene->SetGravity(MT_Vector3(0,0,(blenderscene->world != NULL) ? -blenderscene->world->gravity : -9.8));
+       kxscene->SetGravity(MT_Vector3(0,0, -blenderscene->gm.gravity));
        
        /* set activity culling parameters */
-       if (blenderscene->world) {
-               kxscene->SetActivityCulling( (blenderscene->world->mode & WO_ACTIVITY_CULLING) != 0);
-               kxscene->SetActivityCullingRadius(blenderscene->world->activityBoxRadius);
-               kxscene->SetDbvtCulling((blenderscene->world->mode & WO_DBVT_CULLING) != 0);
-       } else {
-               kxscene->SetActivityCulling(false);
-               kxscene->SetDbvtCulling(false);
-       }
+       kxscene->SetActivityCulling( (blenderscene->gm.mode & WO_ACTIVITY_CULLING) != 0);
+       kxscene->SetActivityCullingRadius(blenderscene->gm.activityBoxRadius);
+       kxscene->SetDbvtCulling((blenderscene->gm.mode & WO_DBVT_CULLING) != 0);
+       
        // no occlusion culling by default
        kxscene->SetDbvtOcclusionRes(0);
 
        int activeLayerBitInfo = blenderscene->lay;
        
-       // templist to find Root Parents (object with no parents)
-       CListValue* templist = new CListValue();
+       // list of all object converted, active and inactive
        CListValue*     sumolist = new CListValue();
        
        vector<parentChildLink> vec_parent_child;
@@ -1985,8 +1995,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                                                                                base->object, 
                                                                                kxscene, 
                                                                                rendertools, 
-                                                                               converter,
-                                                                               blenderscene);
+                                                                               converter);
                                                                                
                bool isInActiveLayer = (blenderobject->lay & activeLayerBitInfo) !=0;
                bool addobj=true;
@@ -2012,7 +2021,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                        if (converter->addInitFromFrame){//rcruiz
                                float eulxyzPrev[3];
                                blenderscene->r.cfra=blenderscene->r.sfra-1;
-                               update_for_newframe();
+                               //XXX update_for_newframe();
                                MT_Vector3 tmp=pos-MT_Point3(blenderobject->loc[0]+blenderobject->dloc[0],
                                                                                        blenderobject->loc[1]+blenderobject->dloc[1],
                                                                                        blenderobject->loc[2]+blenderobject->dloc[2]
@@ -2030,7 +2039,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                                tmp.scale(fps, fps, fps);
                                iniang.push_back(tmp);
                                blenderscene->r.cfra=blenderscene->r.sfra;
-                               update_for_newframe();
+                               //XXX update_for_newframe();
                        }               
                                                
                        gameobj->NodeSetLocalPosition(pos);
@@ -2048,9 +2057,6 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
        
                        gameobj->SetName(blenderobject->id.name);
        
-                       // templist to find Root Parents (object with no parents)
-                       templist->Add(gameobj->AddRef());
-                       
                        // update children/parent hierarchy
                        if ((blenderobject->parent != 0)&&(!converter->addInitFromFrame))
                        {
@@ -2121,8 +2127,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                                //tf.Add(gameobj->GetSGNode());
                                
                                gameobj->NodeUpdateGS(0);
-                               //move to after finishing everything so that soft body deformer is included
-                               //gameobj->AddMeshUser();
+                               gameobj->AddMeshUser();
                
                        }
                        else
@@ -2179,8 +2184,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                                                                                                                blenderobject, 
                                                                                                                kxscene, 
                                                                                                                rendertools, 
-                                                                                                               converter,
-                                                                                                               blenderscene);
+                                                                                                               converter);
                                                                                
                                                // this code is copied from above except that
                                                // object from groups are never in active layer
@@ -2208,7 +2212,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                                                        if (converter->addInitFromFrame){//rcruiz
                                                                float eulxyzPrev[3];
                                                                blenderscene->r.cfra=blenderscene->r.sfra-1;
-                                                               update_for_newframe();
+                                                               //XXX update_for_newframe();
                                                                MT_Vector3 tmp=pos-MT_Point3(blenderobject->loc[0]+blenderobject->dloc[0],
                                                                                                                        blenderobject->loc[1]+blenderobject->dloc[1],
                                                                                                                        blenderobject->loc[2]+blenderobject->dloc[2]
@@ -2226,7 +2230,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                                                                tmp.scale(fps, fps, fps);
                                                                iniang.push_back(tmp);
                                                                blenderscene->r.cfra=blenderscene->r.sfra;
-                                                               update_for_newframe();
+                                                               //XXX update_for_newframe();
                                                        }               
                                                                                
                                                        gameobj->NodeSetLocalPosition(pos);
@@ -2244,9 +2248,6 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                                        
                                                        gameobj->SetName(blenderobject->id.name);
                                        
-                                                       // templist to find Root Parents (object with no parents)
-                                                       templist->Add(gameobj->AddRef());
-                                                       
                                                        // update children/parent hierarchy
                                                        if ((blenderobject->parent != 0)&&(!converter->addInitFromFrame))
                                                        {
@@ -2312,8 +2313,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                                                                //tf.Add(gameobj->GetSGNode());
                                                                
                                                                gameobj->NodeUpdateGS(0);
-                                                               //move to after finishing everything so that soft body deformer is included
-                                                               //gameobj->AddMeshUser();
+                                                               gameobj->AddMeshUser();
                                                        }
                                                        else
                                                        {
@@ -2400,16 +2400,17 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                        for ( i=0;i<childrenlist->GetCount();i++)
                        {
                                KX_GameObject* obj = static_cast<KX_GameObject*>(childrenlist->GetValue(i));
-                               if (templist->RemoveValue(obj))
-                                       obj->Release();
                                if (sumolist->RemoveValue(obj))
                                        obj->Release();
                                if (logicbrick_conversionlist->RemoveValue(obj))
                                        obj->Release();
                        }
                        childrenlist->Release();
+                       
                        // now destroy recursively
+                       converter->UnregisterGameObject(childobj); // removing objects during conversion make sure this runs too
                        kxscene->RemoveObject(childobj);
+                       
                        continue;
                }
 
@@ -2432,7 +2433,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                        case PARBONE:
                        {
                                // parent this to a bone
-                               Bone *parent_bone = get_named_bone(get_armature(blenderchild->parent), blenderchild->parsubstr);
+                               Bone *parent_bone = get_named_bone( (bArmature *)(blenderchild->parent)->data, blenderchild->parsubstr);
 
                                if(parent_bone) {
                                        KX_BoneParentRelation *bone_parent_relation = KX_BoneParentRelation::New(parent_bone);
@@ -2457,23 +2458,56 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
        vec_parent_child.clear();
        
        // find 'root' parents (object that has not parents in SceneGraph)
-       for (i=0;i<templist->GetCount();++i)
+       for (i=0;i<sumolist->GetCount();++i)
        {
-               KX_GameObject* gameobj = (KX_GameObject*) templist->GetValue(i);
+               KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
                if (gameobj->GetSGNode()->GetSGParent() == 0)
                {
                        parentlist->Add(gameobj->AddRef());
                        gameobj->NodeUpdateGS(0);
                }
        }
-       
+
+       // create graphic controller for culling
+       if (kxscene->GetDbvtCulling())
+       {
+               bool occlusion = false;
+               for (i=0; i<sumolist->GetCount();i++)
+               {
+                       KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
+                       if (gameobj->GetMeshCount() > 0) 
+                       {
+                               MT_Point3 box[2];
+                               gameobj->GetSGNode()->BBox().getmm(box, MT_Transform::Identity());
+                               // box[0] is the min, box[1] is the max
+                               bool isactive = objectlist->SearchValue(gameobj);
+                               BL_CreateGraphicObjectNew(gameobj,box[0],box[1],kxscene,isactive,physics_engine);
+                               if (gameobj->GetOccluder())
+                                       occlusion = true;
+                       }
+               }
+               if (occlusion)
+                       kxscene->SetDbvtOcclusionRes(blenderscene->gm.occlusionRes);
+       }
+       if (blenderscene->world)
+               kxscene->GetPhysicsEnvironment()->setNumTimeSubSteps(blenderscene->gm.physubstep);
+
+       // now that the scenegraph is complete, let's instantiate the deformers.
+       // We need that to create reusable derived mesh and physic shapes
+       for (i=0;i<sumolist->GetCount();++i)
+       {
+               KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
+               if (gameobj->GetDeformer())
+                       gameobj->GetDeformer()->UpdateBuckets();
+       }
+
        bool processCompoundChildren = false;
 
        // create physics information
        for (i=0;i<sumolist->GetCount();i++)
        {
                KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
-               struct Object* blenderobject = converter->FindBlenderObject(gameobj);
+               struct Object* blenderobject = gameobj->GetBlenderObject();
                int nummeshes = gameobj->GetMeshCount();
                RAS_MeshObject* meshobj = 0;
                if (nummeshes > 0)
@@ -2489,7 +2523,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
        for (i=0;i<sumolist->GetCount();i++)
        {
                KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
-               struct Object* blenderobject = converter->FindBlenderObject(gameobj);
+               struct Object* blenderobject = gameobj->GetBlenderObject();
                int nummeshes = gameobj->GetMeshCount();
                RAS_MeshObject* meshobj = 0;
                if (nummeshes > 0)
@@ -2500,28 +2534,6 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                BL_CreatePhysicsObjectNew(gameobj,blenderobject,meshobj,kxscene,layerMask,physics_engine,converter,processCompoundChildren);
        }
        
-       // create graphic controller for culling
-       if (kxscene->GetDbvtCulling())
-       {
-               bool occlusion = false;
-               for (i=0; i<sumolist->GetCount();i++)
-               {
-                       KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
-                       if (gameobj->GetMeshCount() > 0) 
-                       {
-                               MT_Point3 box[2];
-                               gameobj->GetSGNode()->BBox().getmm(box, MT_Transform::Identity());
-                               // box[0] is the min, box[1] is the max
-                               bool isactive = objectlist->SearchValue(gameobj);
-                               BL_CreateGraphicObjectNew(gameobj,box[0],box[1],kxscene,isactive,physics_engine);
-                               if (gameobj->GetOccluder())
-                                       occlusion = true;
-                       }
-               }
-               if (occlusion)
-                       kxscene->SetDbvtOcclusionRes(blenderscene->world->occlusionRes);
-       }
-       
        //set ini linearVel and int angularVel //rcruiz
        if (converter->addInitFromFrame)
                for (i=0;i<sumolist->GetCount();i++)
@@ -2539,7 +2551,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
        for (i=0;i<sumolist->GetCount();i++)
        {
                KX_GameObject* gameobj = (KX_GameObject*) sumolist->GetValue(i);
-               struct Object* blenderobject = converter->FindBlenderObject(gameobj);
+               struct Object* blenderobject = gameobj->GetBlenderObject();
                ListBase *conlist;
                bConstraint *curcon;
                conlist = get_active_constraints2(blenderobject);
@@ -2607,26 +2619,7 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                }
        }
 
-       templist->Release();
-       sumolist->Release();    
-
-       int executePriority=0; /* incremented by converter routines */
-       
-       // convert global sound stuff
-
-       /* XXX, glob is the very very wrong place for this
-        * to be, re-enable once the listener has been moved into
-        * the scene. */
-#if 1
-       SND_Scene* soundscene = kxscene->GetSoundScene();
-       SND_SoundListener* listener = soundscene->GetListener();
-       if (listener && G.listener)
-       {
-               listener->SetDopplerFactor(G.listener->dopplerfactor);
-               listener->SetDopplerVelocity(G.listener->dopplervelocity);
-               listener->SetGain(G.listener->gain);
-       }
-#endif
+       sumolist->Release();
 
        // convert world
        KX_WorldInfo* worldinfo = new BlenderWorldInfo(blenderscene->world);
@@ -2639,26 +2632,26 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
        for (i=0;i<logicbrick_conversionlist->GetCount();i++)
        {
                KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
-               struct Object* blenderobj = converter->FindBlenderObject(gameobj);
+               struct Object* blenderobj = gameobj->GetBlenderObject();
                int layerMask = (groupobj.find(blenderobj) == groupobj.end()) ? activeLayerBitInfo : 0;
                bool isInActiveLayer = (blenderobj->lay & layerMask)!=0;
-               BL_ConvertActuators(maggie->name, blenderobj,gameobj,logicmgr,kxscene,ketsjiEngine,executePriority, layerMask,isInActiveLayer,rendertools,converter);
+               BL_ConvertActuators(maggie->name, blenderobj,gameobj,logicmgr,kxscene,ketsjiEngine,layerMask,isInActiveLayer,rendertools,converter);
        }
        for ( i=0;i<logicbrick_conversionlist->GetCount();i++)
        {
                KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
-               struct Object* blenderobj = converter->FindBlenderObject(gameobj);
+               struct Object* blenderobj = gameobj->GetBlenderObject();
                int layerMask = (groupobj.find(blenderobj) == groupobj.end()) ? activeLayerBitInfo : 0;
                bool isInActiveLayer = (blenderobj->lay & layerMask)!=0;
-               BL_ConvertControllers(blenderobj,gameobj,logicmgr,pythondictionary,executePriority,layerMask,isInActiveLayer,converter);
+               BL_ConvertControllers(blenderobj,gameobj,logicmgr,pythondictionary,layerMask,isInActiveLayer,converter);
        }
        for ( i=0;i<logicbrick_conversionlist->GetCount();i++)
        {
                KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
-               struct Object* blenderobj = converter->FindBlenderObject(gameobj);
+               struct Object* blenderobj = gameobj->GetBlenderObject();
                int layerMask = (groupobj.find(blenderobj) == groupobj.end()) ? activeLayerBitInfo : 0;
                bool isInActiveLayer = (blenderobj->lay & layerMask)!=0;
-               BL_ConvertSensors(blenderobj,gameobj,logicmgr,kxscene,ketsjiEngine,keydev,executePriority,layerMask,isInActiveLayer,canvas,converter);
+               BL_ConvertSensors(blenderobj,gameobj,logicmgr,kxscene,ketsjiEngine,layerMask,isInActiveLayer,canvas,converter);
                // set the init state to all objects
                gameobj->SetInitState((blenderobj->init_state)?blenderobj->init_state:blenderobj->state);
        }
@@ -2667,7 +2660,6 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
        {
                KX_GameObject* gameobj = static_cast<KX_GameObject*>(objectlist->GetValue(i));
                gameobj->ResetState();
-               gameobj->AddMeshUser();
        }
 
 #endif //CONVERT_LOGIC
@@ -2697,4 +2689,3 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
        RAS_BucketManager *bucketmanager = kxscene->GetBucketManager();
        bucketmanager->OptimizeBuckets(distance);
 }
-