* workaround for PySys_SetArgv() in python3 needing wchar_t
authorCampbell Barton <ideasman42@gmail.com>
Wed, 8 Jul 2009 09:23:49 +0000 (09:23 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 8 Jul 2009 09:23:49 +0000 (09:23 +0000)
* PyRNA - id_struct.keyframe_insert("path", index, frame)

source/blender/python/intern/bpy_interface.c
source/blender/python/intern/bpy_rna.c
source/gameengine/Ketsji/KX_PythonInit.cpp

index 62e374953b0b71d379ce0175cd167cb5642a9fea..bc422e5a20cb607afd498fc1e245f9059bf3c84c 100644 (file)
@@ -187,7 +187,21 @@ void BPY_start_python( int argc, char **argv )
 
        Py_Initialize(  );
        
-       //PySys_SetArgv( argc_copy, argv_copy );
+#if (PY_VERSION_HEX < 0x03000000)
+       PySys_SetArgv( argc, argv);
+#else
+       /* sigh, why do python guys not have a char** version anymore? :( */
+       {
+               int i;
+               PyObject *py_argv= PyList_New(argc);
+
+               for (i=0; i<argc; i++)
+                       PyList_SET_ITEM(py_argv, i, PyUnicode_FromString(argv[i]));
+
+               PySys_SetObject("argv", py_argv);
+               Py_DECREF(py_argv);
+       }
+#endif
        
        /* Initialize thread support (also acquires lock) */
        PyEval_InitThreads();
index eff34b895da7c00763b2d1648f237daa110f8ae4..5c8499b5280ac9e887496ca32ed4f247bf79f465 100644 (file)
 #include "BKE_global.h" /* evil G.* */
 #include "BKE_report.h"
 
+/* only for keyframing */
+#include "DNA_scene_types.h"
+#include "ED_keyframing.h"
+
 #define USE_MATHUTILS
 
 #ifdef USE_MATHUTILS
@@ -312,10 +316,17 @@ PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
                        /* don't throw error here, can't trust blender 100% to give the
                         * right values, python code should not generate error for that */
                        RNA_property_enum_items(ptr, prop, &item, NULL);
-                       if(item[0].identifier)
-                               ret = PyUnicode_FromString( item[0].identifier );
-                       else
+                       if(item->identifier) {
+                               ret = PyUnicode_FromString( item->identifier );
+                       }
+                       else {
+                               /* prefer not fail silently incase of api errors, maybe disable it later */
+                               char error_str[128];
+                               sprintf(error_str, "RNA Warning: Current value \"%d\" matches no enum", val);
+                               PyErr_Warn(PyExc_RuntimeWarning, error_str);
+
                                ret = PyUnicode_FromString( "" );
+                       }
 
                        /*PyErr_Format(PyExc_AttributeError, "RNA Error: Current value \"%d\" matches no enum", val);
                        ret = NULL;*/
@@ -913,8 +924,12 @@ static PyObject *prop_subscript_collection(BPy_PropertyRNA * self, PyObject *key
        if (PyUnicode_Check(key)) {
                return prop_subscript_collection_str(self, _PyUnicode_AsString(key));
        }
-       else if (PyLong_Check(key)) {
-               return prop_subscript_collection_int(self, PyLong_AsSsize_t(key));
+       else if (PyIndex_Check(key)) {
+               Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
+               if (i == -1 && PyErr_Occurred())
+                       return NULL;
+
+               return prop_subscript_collection_int(self, i);
        }
 #if PY_VERSION_HEX >= 0x03000000
        else if (PySlice_Check(key)) {
@@ -947,7 +962,10 @@ static PyObject *prop_subscript_array(BPy_PropertyRNA * self, PyObject *key)
        /*if (PyUnicode_Check(key)) {
                return prop_subscript_array_str(self, _PyUnicode_AsString(key));
        } else*/
-       if (PyLong_Check(key)) {
+       if (PyIndex_Check(key)) {
+               Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
+               if (i == -1 && PyErr_Occurred())
+                       return NULL;
                return prop_subscript_array_int(self, PyLong_AsSsize_t(key));
        }
 #if PY_VERSION_HEX >= 0x03000000
@@ -1037,8 +1055,12 @@ static int pyrna_prop_ass_subscript( BPy_PropertyRNA * self, PyObject *key, PyOb
                return -1;
        }
 
-       if (PyLong_Check(key)) {
-               return prop_subscript_ass_array_int(self, PyLong_AsSsize_t(key), value);
+       if (PyIndex_Check(key)) {
+               Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError);
+               if (i == -1 && PyErr_Occurred())
+                       return NULL;
+
+               return prop_subscript_ass_array_int(self, i, value);
        }
 #if PY_VERSION_HEX >= 0x03000000
        else if (PySlice_Check(key)) {
@@ -1106,6 +1128,25 @@ static PySequenceMethods pyrna_prop_as_sequence = {
        (objobjproc)pyrna_prop_contains,        /* sq_contains */
 };
 
+
+static PyObject *pyrna_struct_keyframe_insert(BPy_StructRNA * self, PyObject *args)
+{
+       char *path;
+       int index= 0;
+       float cfra = CTX_data_scene(BPy_GetContext())->r.cfra;
+
+       if(!RNA_struct_is_ID(self->ptr.type)) {
+               PyErr_SetString( PyExc_TypeError, "StructRNA - keyframe_insert only for ID type");
+               return NULL;
+       }
+
+       if (!PyArg_ParseTuple(args, "s|if:keyframe_insert", &path, &index, &cfra))
+               return NULL;
+
+       return PyBool_FromLong( insert_keyframe((ID *)self->ptr.data, NULL, NULL, path, index, cfra, 0));
+}
+
+
 static PyObject *pyrna_struct_dir(BPy_StructRNA * self)
 {
        PyObject *ret, *dict;
@@ -1645,6 +1686,10 @@ PyObject *pyrna_prop_iter(BPy_PropertyRNA *self)
 }
 
 static struct PyMethodDef pyrna_struct_methods[] = {
+
+       /* maybe this become and ID function */
+       {"keyframe_insert", (PyCFunction)pyrna_struct_keyframe_insert, METH_VARARGS, NULL},
+
        {"__dir__", (PyCFunction)pyrna_struct_dir, METH_NOARGS, NULL},
        {NULL, NULL, 0, NULL}
 };
index eead7a51885fdd0092b1c64b4e51071b90b7a98c..ac49b49c13041cd6cc1c19fbb7eda50d8c0528e5 100644 (file)
@@ -1622,10 +1622,20 @@ PyObject* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecur
        Py_FrozenFlag=1;
        Py_Initialize();
        
+       if(argv && first_time) { /* browser plugins dont currently set this */
 #if (PY_VERSION_HEX < 0x03000000)      
-       if(argv && first_time) /* browser plugins dont currently set this */
                PySys_SetArgv(argc, argv);
+#else
+               int i;
+               PyObject *py_argv= PyList_New(argc);
+
+               for (i=0; i<argc; i++)
+                       PyList_SET_ITEM(py_argv, i, PyUnicode_FromString(argv[i]));
+
+               PySys_SetObject("argv", py_argv);
+               Py_DECREF(py_argv);
 #endif
+       }
        //importBlenderModules()
        
        setSandbox(level);