This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/python/intern/bpy_interface.c
Campbell Barton f23894c365 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.
2008-11-29 13:36:08 +00:00

86 lines
1.8 KiB
C

#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();
}