[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [17616] branches/blender2.5/blender/source : Python RNA API
Campbell Barton
ideasman42 at gmail.com
Sat Nov 29 14:36:09 CET 2008
Revision: 17616
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17616
Author: campbellbarton
Date: 2008-11-29 14:36:08 +0100 (Sat, 29 Nov 2008)
Log Message:
-----------
Python RNA API
* Matches the C/RNA api structure
* Thin wrapper ~(600 lines)
* No functions specific to any blender object type.
* Defines 2 types, BPy_StructRNA and BPy_PropertyRNA.
* Python 3.0 target (compatible with python 2.4,5,6)
* http://wiki.blender.org/index.php/BlenderDev/Blender2.5/PyRNA - continue docs/discussion here.
Todo
* Collection iterators
* Write access to data
* Define how constants should be accessed (as strings or some special type)
* Solve the "Python keeping invalid blender pointers" problem.
This cant just be solved in the py api - we need blender to notify when ID's are removed
Examples
Here are some examples that work with the current implementation of the api.
rna.lamps["Lamp.006"].energy -> (1.0)
rna.lamps["Lamp.007"].shadow -> ("NOSHADOW")
rna.materials.keys() -> ['flyingsquirrel_eye', 'frankie_skin', 'frankie_theeth']
rna.scenes["hud"].objects["num_text_p2_4"].data.novnormalflip -> False
rna.meshes["mymesh"].uv_layers.keys() -> ['UVTex', 'UVTex']
rna.meshes.items()
For a dump of yo-frankie level see - http://pasteall.org/3294/python
Notes
* Added python back, can only execute scripts from the command line with -P script.py
* bpy_interface.c is just enough functionality to run a python file.
Modified Paths:
--------------
branches/blender2.5/blender/source/blender/editors/screen/stubs.c
branches/blender2.5/blender/source/blender/python/SConscript
branches/blender2.5/blender/source/creator/creator.c
Added Paths:
-----------
branches/blender2.5/blender/source/blender/python/intern/bpy_compat.h
branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
branches/blender2.5/blender/source/blender/python/intern/bpy_rna.h
branches/blender2.5/blender/source/blender/python/intern/bpy_scriptlink.c
Removed Paths:
-------------
branches/blender2.5/blender/source/blender/python/intern/BPY_scriptlink.c
Modified: branches/blender2.5/blender/source/blender/editors/screen/stubs.c
===================================================================
--- branches/blender2.5/blender/source/blender/editors/screen/stubs.c 2008-11-29 12:12:33 UTC (rev 17615)
+++ branches/blender2.5/blender/source/blender/editors/screen/stubs.c 2008-11-29 13:36:08 UTC (rev 17616)
@@ -46,7 +46,7 @@
/* python, will come back */
void BPY_post_start_python() {}
-void BPY_run_python_script() {}
+//void BPY_run_python_script() {}
void BPY_start_python() {}
void BPY_do_all_scripts() {}
void BPY_call_importloader() {}
Modified: branches/blender2.5/blender/source/blender/python/SConscript
===================================================================
--- branches/blender2.5/blender/source/blender/python/SConscript 2008-11-29 12:12:33 UTC (rev 17615)
+++ branches/blender2.5/blender/source/blender/python/SConscript 2008-11-29 13:36:08 UTC (rev 17616)
@@ -3,7 +3,7 @@
sources = env.Glob('intern/*.c')
-incs = '. ../editors/include ../makesdna ../blenlib ../blenkernel ../nodes'
+incs = '. ../editors/include ../makesdna ../makesrna ../blenlib ../blenkernel ../nodes'
incs += ' ../imbuf ../blenloader ../render/extern/include'
incs += ' #intern/guardedalloc #intern/memutil'
incs += ' ' + env['BF_PYTHON_INC']
Deleted: branches/blender2.5/blender/source/blender/python/intern/BPY_scriptlink.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/BPY_scriptlink.c 2008-11-29 12:12:33 UTC (rev 17615)
+++ branches/blender2.5/blender/source/blender/python/intern/BPY_scriptlink.c 2008-11-29 13:36:08 UTC (rev 17616)
@@ -1,73 +0,0 @@
-/**
- * $Id:
- *
- * ***** BEGIN GPL LICENSE BLOCK *****
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * The Original Code is Copyright (C) 2008 Blender Foundation.
- * All rights reserved.
- *
- *
- * ***** END GPL LICENSE BLOCK *****
- */
-
-#include <string.h>
-
-#include "MEM_guardedalloc.h"
-
-#include "DNA_lamp_types.h"
-#include "DNA_camera_types.h"
-#include "DNA_world_types.h"
-#include "DNA_scene_types.h"
-#include "DNA_material_types.h"
-
-#include "BLI_blenlib.h"
-
-#include "BKE_blender.h"
-#include "BKE_global.h"
-#include "BKE_library.h"
-#include "BKE_main.h"
-
-
-/* only copies internal pointers, scriptlink usually is integral part of a struct */
-void BPY_copy_scriptlink( struct ScriptLink *scriptlink )
-{
-
- if( scriptlink->totscript ) {
- scriptlink->scripts = MEM_dupallocN(scriptlink->scripts);
- scriptlink->flag = MEM_dupallocN(scriptlink->flag);
- }
-
- return;
-}
-
-/* not free slink itself */
-void BPY_free_scriptlink( struct ScriptLink *slink )
-{
- if( slink->totscript ) {
- if( slink->flag ) {
- MEM_freeN( slink->flag );
- slink->flag= NULL;
- }
- if( slink->scripts ) {
- MEM_freeN( slink->scripts );
- slink->scripts= NULL;
- }
- }
-
- return;
-}
-
Added: branches/blender2.5/blender/source/blender/python/intern/bpy_compat.h
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_compat.h (rev 0)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_compat.h 2008-11-29 13:36:08 UTC (rev 17616)
@@ -0,0 +1,49 @@
+/**
+ * $Id$
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Contributor(s): Campbell Barton
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/* This file is only to contain definitions to functions that enable
+ * the python api to compile with different python versions.
+ * no utility functions please
+ */
+
+/* if you are NOT using python 3.0 - define these */
+#if PY_VERSION_HEX < 0x03000000
+#define _PyUnicode_AsString PyString_AsString
+#define PyUnicode_Check PyString_Check
+#define PyLong_FromSize_t PyInt_FromLong
+#define PyLong_AsSsize_t PyInt_AsLong
+#define PyLong_Check PyInt_Check
+#define PyUnicode_FromString PyString_FromString
+#define PyUnicode_FromFormat PyString_FromFormat
+
+#endif
+
+/* older then python 2.6 - define these */
+// #if (PY_VERSION_HEX < 0x02060000)
+// #endif
+
+/* older then python 2.5 - define these */
+#if (PY_VERSION_HEX < 0x02050000)
+#define Py_ssize_t ssize_t
+#endif
Added: branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c (rev 0)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c 2008-11-29 13:36:08 UTC (rev 17616)
@@ -0,0 +1,85 @@
+
+#include <Python.h>
+#include "compile.h" /* for the PyCodeObject */
+#include "eval.h" /* for PyEval_EvalCode */
+
+#include "bpy_compat.h"
+
+#include "bpy_rna.h"
+
+
+/*****************************************************************************
+* Description: This function creates a new Python dictionary object.
+*****************************************************************************/
+
+static PyObject *CreateGlobalDictionary( void )
+{
+ PyObject *dict = PyDict_New( );
+ PyObject *item = PyUnicode_FromString( "__main__" );
+ PyDict_SetItemString( dict, "__builtins__", PyEval_GetBuiltins( ) );
+ PyDict_SetItemString( dict, "__name__", item );
+ Py_DECREF(item);
+
+ /* Add Modules */
+ item = BPY_rna_module();
+ PyDict_SetItemString( dict, "rna", item );
+ Py_DECREF(item);
+
+
+ return dict;
+}
+
+static void BPY_start_python( void )
+{
+ PyThreadState *py_tstate = NULL;
+
+ Py_Initialize( );
+
+ //PySys_SetArgv( argc_copy, argv_copy );
+
+ /* Initialize thread support (also acquires lock) */
+ PyEval_InitThreads();
+
+ // todo - sys paths - our own imports
+
+ py_tstate = PyGILState_GetThisThreadState();
+ PyEval_ReleaseThread(py_tstate);
+
+}
+
+static void BPY_end_python( void )
+{
+ PyGILState_Ensure(); /* finalizing, no need to grab the state */
+
+ // free other python data.
+
+ Py_Finalize( );
+ return;
+}
+
+void BPY_run_python_script( const char *fn )
+{
+ PyObject *py_dict, *py_result;
+ char pystring[512];
+ PyGILState_STATE gilstate;
+
+ /* TODO - look into a better way to run a file */
+ sprintf(pystring, "exec(open(r'%s').read())", fn);
+
+ BPY_start_python();
+
+ gilstate = PyGILState_Ensure();
+
+ py_dict = CreateGlobalDictionary();
+
+ py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
+
+ if (!py_result)
+ PyErr_Print();
+ else
+ Py_DECREF( py_result );
+
+ PyGILState_Release(gilstate);
+
+ BPY_end_python();
+}
Added: branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c (rev 0)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c 2008-11-29 13:36:08 UTC (rev 17616)
@@ -0,0 +1,608 @@
+/**
+ * $Id$
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Contributor(s): Campbell Barton
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include "bpy_rna.h"
+#include "bpy_compat.h"
+
+#include "MEM_guardedalloc.h"
+#include "BKE_global.h" /* evil G.* */
+
+
+static int pyrna_struct_compare( BPy_StructRNA * a, BPy_StructRNA * b )
+{
+ return (a->ptr.data==b->ptr.data) ? 0 : -1;
+}
+
+static int pyrna_prop_compare( BPy_PropertyRNA * a, BPy_PropertyRNA * b )
+{
+ return (a->prop==b->prop && a->ptr.data==b->ptr.data ) ? 0 : -1;
+}
+
+/*----------------------repr--------------------------------------------*/
+static PyObject *pyrna_struct_repr( BPy_StructRNA * self )
+{
+ return PyUnicode_FromFormat( "[BPy_StructRNA \"%s\"]", RNA_struct_identifier(&self->ptr));
+}
+
+static PyObject *pyrna_prop_repr( BPy_PropertyRNA * self )
+{
+ return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%s\" -> \"%s\" ]", RNA_struct_identifier(&self->ptr), RNA_property_identifier(&self->ptr, self->prop) );
+}
+
+static PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
+{
+ PyObject *ret;
+ int type = RNA_property_type(ptr, prop);
+ int len = RNA_property_array_length(ptr, prop);
+ /* resolve path */
+
+ if (len > 0) {
+ /* resolve the array from a new pytype */
+ return pyrna_prop_CreatePyObject(ptr, prop);
+ }
+
+ /* see if we can coorce into a python type - PropertyType */
+ switch (type) {
+ case PROP_BOOLEAN:
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list