Revision: 20426
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20426
Author:   campbellbarton
Date:     2009-05-26 18:15:40 +0200 (Tue, 26 May 2009)

Log Message:
-----------
BGE Py API
- Deprecation warnings for using attribute access

- Added dictionary functions to KX_GameObject and ListValue
    ob.get(key, default=None)
    ob.has_key(key)
 ob.has_key is important since there was no way to do something like 
hasattr(ob, "attr") which can be replaced by ob.has_key("attr") - (both still 
work of course).
 ob.get is just useful in many cases where you want a property if it exists but 
can fallback to a default.

- CListValue::FindValue was adding a reference but the ~3 places it was used 
were releasing the reference. added a FindValue that accepts a const char* type 
to avoid converting python strings to STR_String.

Modified Paths:
--------------
    trunk/blender/source/gameengine/Expressions/ListValue.cpp
    trunk/blender/source/gameengine/Expressions/ListValue.h
    trunk/blender/source/gameengine/Expressions/Value.cpp
    trunk/blender/source/gameengine/Ketsji/KX_BlenderMaterial.cpp
    trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp
    trunk/blender/source/gameengine/Ketsji/KX_GameObject.h
    trunk/blender/source/gameengine/PyDoc/GameTypes.py

Modified: trunk/blender/source/gameengine/Expressions/ListValue.cpp
===================================================================
--- trunk/blender/source/gameengine/Expressions/ListValue.cpp   2009-05-26 
15:01:06 UTC (rev 20425)
+++ trunk/blender/source/gameengine/Expressions/ListValue.cpp   2009-05-26 
16:15:40 UTC (rev 20426)
@@ -76,13 +76,9 @@
        
        if (PyString_Check(pyindex))
        {
-               const char *index = PyString_AsString(pyindex);
-               CValue *item = ((CListValue*) list)->FindValue(index);
+               CValue *item = ((CListValue*) 
list)->FindValue(PyString_AsString(pyindex));
                if (item)
-               {
-                       item->Release(); /* FindValue() AddRef's */ 
                        return item->GetProxy();
-               }
        }
        else if (PyInt_Check(pyindex))
        {
@@ -279,10 +275,17 @@
 
 
 PyMethodDef CListValue::Methods[] = {
+       /* List style access */
        {"append", (PyCFunction)CListValue::sPyappend,METH_O},
        {"reverse", (PyCFunction)CListValue::sPyreverse,METH_NOARGS},
        {"index", (PyCFunction)CListValue::sPyindex,METH_O},
        {"count", (PyCFunction)CListValue::sPycount,METH_O},
+       
+       /* Dict style access */
+       {"get", (PyCFunction)CListValue::sPyget,METH_VARARGS},
+       {"has_key", (PyCFunction)CListValue::sPyhas_key,METH_O},
+       
+       /* Own cvalue funcs */
        {"from_id", (PyCFunction)CListValue::sPyfrom_id,METH_O},
        
        {NULL,NULL} //Sentinel
@@ -395,26 +398,24 @@
 
 
 
+CValue* CListValue::FindValue(const STR_String & name)
+{
+       for (int i=0; i < GetCount(); i++)
+               if (GetValue(i)->GetName() == name)
+                       return GetValue(i);
+       
+       return NULL;
+}
+
 CValue* CListValue::FindValue(const char * name)
 {
-       CValue* resultval = NULL;
-       int i=0;
+       for (int i=0; i < GetCount(); i++)
+               if (GetValue(i)->GetName() == name)
+                       return GetValue(i);
        
-       while (!resultval && i < GetCount())
-       {
-               CValue* myval = GetValue(i);
-                               
-               if (myval->GetName() == name)
-                       resultval = GetValue(i)->AddRef(); // add referencecount
-               else
-                       i++;
-               
-       }
-       return resultval;
+       return NULL;
 }
 
-
-
 bool CListValue::SearchValue(CValue *val)
 {
        for (int i=0;i<GetCount();i++)
@@ -564,8 +565,32 @@
        return PyInt_FromLong(numfound);
 }
 
+/* Matches python dict.get(key, [default]) */
+PyObject* CListValue::Pyget(PyObject *args)
+{
+       char *key;
+       PyObject* def = Py_None;
 
+       if (!PyArg_ParseTuple(args, "s|O:get", &key, &def))
+               return NULL;
+       
+       CValue *item = FindValue((const char *)key);
+       if (item)
+               return item->GetProxy();
+       
+       Py_INCREF(def);
+       return def;
+}
 
+/* Matches python dict.has_key() */
+PyObject* CListValue::Pyhas_key(PyObject* value)
+{
+       if (PyString_Check(value) && FindValue((const char 
*)PyString_AsString(value)))
+               Py_RETURN_TRUE;
+       
+       Py_RETURN_FALSE;
+}
+
 PyObject* CListValue::Pyfrom_id(PyObject* value)
 {
        uintptr_t id= (uintptr_t)PyLong_AsVoidPtr(value);

Modified: trunk/blender/source/gameengine/Expressions/ListValue.h
===================================================================
--- trunk/blender/source/gameengine/Expressions/ListValue.h     2009-05-26 
15:01:06 UTC (rev 20425)
+++ trunk/blender/source/gameengine/Expressions/ListValue.h     2009-05-26 
16:15:40 UTC (rev 20426)
@@ -45,6 +45,7 @@
        void SetReleaseOnDestruct(bool bReleaseContents);
        bool SearchValue(CValue* val);
        
+       CValue* FindValue(const STR_String & name);
        CValue* FindValue(const char *name);
 
        void ReleaseAndRemoveAll();
@@ -74,6 +75,8 @@
        KX_PYMETHOD_NOARGS(CListValue,reverse);
        KX_PYMETHOD_O(CListValue,index);
        KX_PYMETHOD_O(CListValue,count);
+       KX_PYMETHOD_VARARGS(CListValue,get);
+       KX_PYMETHOD_O(CListValue,has_key);
        KX_PYMETHOD_O(CListValue,from_id);
 
        

Modified: trunk/blender/source/gameengine/Expressions/Value.cpp
===================================================================
--- trunk/blender/source/gameengine/Expressions/Value.cpp       2009-05-26 
15:01:06 UTC (rev 20425)
+++ trunk/blender/source/gameengine/Expressions/Value.cpp       2009-05-26 
16:15:40 UTC (rev 20426)
@@ -556,6 +556,8 @@
 
 PyObject*      CValue::py_getattro(PyObject *attr)
 {
+       ShowDeprecationWarning("val = ob.attr", "val = ob['attr']");
+       
        char *attr_str= PyString_AsString(attr);
        CValue* resultattr = GetProperty(attr_str);
        if (resultattr)
@@ -655,6 +657,8 @@
 
 int    CValue::py_delattro(PyObject *attr)
 {
+       ShowDeprecationWarning("del ob.attr", "del ob['attr']");
+       
        char *attr_str= PyString_AsString(attr);
        if (RemoveProperty(attr_str))
                return 0;
@@ -665,6 +669,8 @@
 
 int    CValue::py_setattro(PyObject *attr, PyObject* pyobj)
 {
+       ShowDeprecationWarning("ob.attr = val", "ob['attr'] = val");
+       
        char *attr_str= PyString_AsString(attr);
        CValue* oldprop = GetProperty(attr_str);        
        CValue* vallie;

Modified: trunk/blender/source/gameengine/Ketsji/KX_BlenderMaterial.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_BlenderMaterial.cpp       
2009-05-26 15:01:06 UTC (rev 20425)
+++ trunk/blender/source/gameengine/Ketsji/KX_BlenderMaterial.cpp       
2009-05-26 16:15:40 UTC (rev 20426)
@@ -714,7 +714,6 @@
                
mScene->GetObjectList()->FindValue(mMaterial->mapping[i].objconame);
 
        if(!obj) return;
-       obj->Release(); /* FindValue() AddRef's */
 
        obj->Release();
 

Modified: trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp    2009-05-26 
15:01:06 UTC (rev 20425)
+++ trunk/blender/source/gameengine/Ketsji/KX_GameObject.cpp    2009-05-26 
16:15:40 UTC (rev 20426)
@@ -1190,7 +1190,11 @@
        KX_PYMETHODTABLE_O(KX_GameObject, getDistanceTo),
        KX_PYMETHODTABLE_O(KX_GameObject, getVectTo),
        KX_PYMETHODTABLE(KX_GameObject, sendMessage),
-
+       
+       // dict style access for props
+       {"has_key",(PyCFunction) KX_GameObject::sPyhas_key, METH_O},
+       {"get",(PyCFunction) KX_GameObject::sPyget, METH_VARARGS},
+       
        // deprecated
        {"getPosition", (PyCFunction) KX_GameObject::sPyGetPosition, 
METH_NOARGS},
        {"setPosition", (PyCFunction) KX_GameObject::sPySetPosition, METH_O},
@@ -1899,6 +1903,8 @@
 
 int    KX_GameObject::py_delattro(PyObject *attr)
 {
+       ShowDeprecationWarning("del ob.attr", "del ob['attr'] for user defined 
properties");
+       
        char *attr_str= PyString_AsString(attr); 
        
        if (RemoveProperty(attr_str)) // XXX - should call CValues instead but 
its only 2 lines here
@@ -2737,6 +2743,48 @@
        Py_RETURN_NONE;
 }
 
+/* dict style access */
+
+
+/* Matches python dict.get(key, [default]) */
+PyObject* KX_GameObject::Pyget(PyObject *args)
+{
+       PyObject *key;
+       PyObject* def = Py_None;
+       PyObject* ret;
+
+       if (!PyArg_ParseTuple(args, "O|O:get", &key, &def))
+               return NULL;
+       
+       
+       if(PyString_Check(key)) {
+               CValue *item = GetProperty(PyString_AsString(key));
+               if (item)
+                       return item->GetProxy();
+       }
+       
+       if (m_attr_dict && (ret=PyDict_GetItem(m_attr_dict, key))) {
+               Py_INCREF(ret);
+               return ret;
+       }
+       
+       Py_INCREF(def);
+       return def;
+}
+
+/* Matches python dict.has_key() */
+PyObject* KX_GameObject::Pyhas_key(PyObject* value)
+{
+       if(PyString_Check(value) && GetProperty(PyString_AsString(value)))
+               Py_RETURN_TRUE;
+       
+       if (m_attr_dict && PyDict_GetItem(m_attr_dict, value))
+               Py_RETURN_TRUE;
+       
+       Py_RETURN_FALSE;
+}
+
+
 /* --------------------------------------------------------------------- 
  * Some stuff taken from the header
  * --------------------------------------------------------------------- */

Modified: trunk/blender/source/gameengine/Ketsji/KX_GameObject.h
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_GameObject.h      2009-05-26 
15:01:06 UTC (rev 20425)
+++ trunk/blender/source/gameengine/Ketsji/KX_GameObject.h      2009-05-26 
16:15:40 UTC (rev 20426)
@@ -872,6 +872,11 @@
        KX_PYMETHOD_DOC_O(KX_GameObject,getDistanceTo);
        KX_PYMETHOD_DOC_O(KX_GameObject,getVectTo);
        KX_PYMETHOD_DOC_VARARGS(KX_GameObject, sendMessage);
+       
+       /* Dict access */
+       KX_PYMETHOD_VARARGS(KX_GameObject,get);
+       KX_PYMETHOD_O(KX_GameObject,has_key);
+       
        /* attributes */
        static PyObject*        pyattr_get_name(void* self_v, const 
KX_PYATTRIBUTE_DEF *attrdef);
        static PyObject*        pyattr_get_parent(void *self_v, const 
KX_PYATTRIBUTE_DEF *attrdef);

Modified: trunk/blender/source/gameengine/PyDoc/GameTypes.py
===================================================================
--- trunk/blender/source/gameengine/PyDoc/GameTypes.py  2009-05-26 15:01:06 UTC 
(rev 20425)
+++ trunk/blender/source/gameengine/PyDoc/GameTypes.py  2009-05-26 16:15:40 UTC 
(rev 20426)
@@ -1008,6 +1008,17 @@
                """
                Reverse the order of the list.
                """
+       def get(key, default=None):
+               """
+               Return the value matching key, or the default value if its not 
found.
+               @return: The key value or a default.
+               """
+       def has_key(key):
+               """
+               Return True if the key is found.
+               @rtype: boolean
+               @return: The key value or a default.
+               """
        def from_id(id):
                """
                This is a funtion especially for the game engine to return a 
value with a spesific id.
@@ -1614,6 +1625,7 @@
        @ivar childrenRecursive: all children of this object including 
childrens children, (read-only).
        @type childrenRecursive: L{CListValue} of L{KX_GameObject}'s
        @group Deprecated: getPosition, setPosition, setWorldPosition, 
getOrientation, setOrientation, getState, setState, getParent, getVisible, 
getMass, getMesh, getChildren, getChildrenRecursive
+       @group Property Access: get, has_key, attrDict, getPropertyNames
        """
        def endObject():
                """
@@ -2056,7 +2068,19 @@
                @param to: The name of the object to send the message to 
(optional)
                @type to: string
                """
+       def get(key, default=None):
+               """
+               Return the value matching key, or the default value if its not 
found.
+               @return: The key value or a default.
+               """
+       def has_key(key):
+               """
+               Return True if the key is found.
+               @rtype: boolean
+               @return: The key value or a default.
+               """
 
+
 class KX_IpoActuator(SCA_IActuator):
        """
        IPO actuator activates an animation.


_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to