2008-11-29 13:36:08 +00:00
/**
* $ 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"
2009-04-07 00:49:39 +00:00
# include "bpy_util.h"
2008-11-30 14:00:14 +00:00
//#include "blendef.h"
# include "BLI_dynstr.h"
2009-03-19 19:03:38 +00:00
# include "BLI_listbase.h"
2009-04-19 13:37:59 +00:00
# include "BLI_string.h"
2009-03-16 15:54:43 +00:00
# include "float.h" /* FLT_MIN/MAX */
2009-04-19 13:37:59 +00:00
# include "RNA_access.h"
2009-03-16 15:54:43 +00:00
# include "RNA_define.h" /* for defining our own rna */
2008-11-29 13:36:08 +00:00
# include "MEM_guardedalloc.h"
2009-03-19 19:03:38 +00:00
# include "BKE_context.h"
2008-11-29 13:36:08 +00:00
# include "BKE_global.h" /* evil G.* */
2009-04-19 13:37:59 +00:00
# include "BKE_report.h"
2008-11-29 13:36:08 +00:00
2009-06-22 04:26:48 +00:00
# define USE_MATHUTILS
# ifdef USE_MATHUTILS
# include "../generic/Mathutils.h" /* so we can have mathutils callbacks */
static int mathutils_rna_vector_cb_index = - 1 ; /* index for our callbacks */
static int mathutils_rna_vector_check ( PyObject * user )
{
return ( ( BPy_PropertyRNA * ) user ) - > prop ? 1 : 0 ;
}
static int mathutils_rna_vector_get ( BPy_PropertyRNA * self , int subtype , float * vec_from )
{
if ( self - > prop = = NULL )
return 0 ;
RNA_property_float_get_array ( & self - > ptr , self - > prop , vec_from ) ;
return 1 ;
}
static int mathutils_rna_vector_set ( BPy_PropertyRNA * self , int subtype , float * vec_to )
{
if ( self - > prop = = NULL )
return 0 ;
RNA_property_float_set_array ( & self - > ptr , self - > prop , vec_to ) ;
return 1 ;
}
static int mathutils_rna_vector_get_index ( BPy_PropertyRNA * self , int subtype , float * vec_from , int index )
{
if ( self - > prop = = NULL )
return 0 ;
vec_from [ index ] = RNA_property_float_get_index ( & self - > ptr , self - > prop , index ) ;
return 1 ;
}
static int mathutils_rna_vector_set_index ( BPy_PropertyRNA * self , int subtype , float * vec_to , int index )
{
if ( self - > prop = = NULL )
return 0 ;
RNA_property_float_set_index ( & self - > ptr , self - > prop , index , vec_to [ index ] ) ;
return 1 ;
}
Mathutils_Callback mathutils_rna_vector_cb = {
mathutils_rna_vector_check ,
mathutils_rna_vector_get ,
mathutils_rna_vector_set ,
mathutils_rna_vector_get_index ,
mathutils_rna_vector_set_index
} ;
# endif
2008-11-29 13:36:08 +00:00
static int pyrna_struct_compare ( BPy_StructRNA * a , BPy_StructRNA * b )
2009-01-29 09:38:52 +00:00
{
2008-11-29 13:36:08 +00:00
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 ;
}
2009-01-29 09:38:52 +00:00
/* For some reason python3 needs these :/ */
static PyObject * pyrna_struct_richcmp ( BPy_StructRNA * a , BPy_StructRNA * b , int op )
{
int cmp_result = - 1 ; /* assume false */
if ( BPy_StructRNA_Check ( a ) & & BPy_StructRNA_Check ( b ) ) {
cmp_result = pyrna_struct_compare ( a , b ) ;
}
return Py_CmpToRich ( op , cmp_result ) ;
}
static PyObject * pyrna_prop_richcmp ( BPy_PropertyRNA * a , BPy_PropertyRNA * b , int op )
{
int cmp_result = - 1 ; /* assume false */
if ( BPy_PropertyRNA_Check ( a ) & & BPy_PropertyRNA_Check ( b ) ) {
cmp_result = pyrna_prop_compare ( a , b ) ;
}
return Py_CmpToRich ( op , cmp_result ) ;
}
2009-04-07 00:49:39 +00:00
2008-11-29 13:36:08 +00:00
/*----------------------repr--------------------------------------------*/
static PyObject * pyrna_struct_repr ( BPy_StructRNA * self )
{
2009-03-23 13:28:42 +00:00
PropertyRNA * prop ;
2009-03-16 15:54:43 +00:00
char str [ 512 ] ;
2009-03-23 13:28:42 +00:00
/* print name if available */
2009-04-19 13:37:59 +00:00
prop = RNA_struct_name_property ( self - > ptr . type ) ;
2009-03-23 13:28:42 +00:00
if ( prop ) {
RNA_property_string_get ( & self - > ptr , prop , str ) ;
2009-04-19 13:37:59 +00:00
return PyUnicode_FromFormat ( " [BPy_StructRNA \" %s \" -> \" %s \" ] " , RNA_struct_identifier ( self - > ptr . type ) , str ) ;
2009-03-23 13:28:42 +00:00
}
2009-04-19 13:37:59 +00:00
return PyUnicode_FromFormat ( " [BPy_StructRNA \" %s \" ] " , RNA_struct_identifier ( self - > ptr . type ) ) ;
2008-11-29 13:36:08 +00:00
}
static PyObject * pyrna_prop_repr ( BPy_PropertyRNA * self )
{
2009-03-23 13:28:42 +00:00
PropertyRNA * prop ;
PointerRNA ptr ;
2009-03-16 15:54:43 +00:00
char str [ 512 ] ;
2009-03-23 13:28:42 +00:00
/* if a pointer, try to print name of pointer target too */
2009-04-19 13:37:59 +00:00
if ( RNA_property_type ( self - > prop ) = = PROP_POINTER ) {
2009-03-23 13:28:42 +00:00
ptr = RNA_property_pointer_get ( & self - > ptr , self - > prop ) ;
if ( ptr . data ) {
2009-04-19 13:37:59 +00:00
prop = RNA_struct_name_property ( ptr . type ) ;
2009-03-23 13:28:42 +00:00
if ( prop ) {
RNA_property_string_get ( & ptr , prop , str ) ;
2009-04-19 13:37:59 +00:00
return PyUnicode_FromFormat ( " [BPy_PropertyRNA \" %s \" -> \" %s \" -> \" %s \" ] " , RNA_struct_identifier ( self - > ptr . type ) , RNA_property_identifier ( self - > prop ) , str ) ;
2009-03-23 13:28:42 +00:00
}
}
}
2009-04-19 13:37:59 +00:00
return PyUnicode_FromFormat ( " [BPy_PropertyRNA \" %s \" -> \" %s \" ] " , RNA_struct_identifier ( self - > ptr . type ) , RNA_property_identifier ( self - > prop ) ) ;
2008-11-29 13:36:08 +00:00
}
2009-04-09 17:31:23 +00:00
2008-12-02 14:36:35 +00:00
static long pyrna_struct_hash ( BPy_StructRNA * self )
{
return ( long ) self - > ptr . data ;
}
2008-12-29 03:24:13 +00:00
/* use our own dealloc so we can free a property if we use one */
static void pyrna_struct_dealloc ( BPy_StructRNA * self )
{
/* Note!! for some weired reason calling PyObject_DEL() directly crashes blender! */
2009-01-08 15:29:09 +00:00
if ( self - > freeptr & & self - > ptr . data ) {
IDP_FreeProperty ( self - > ptr . data ) ;
MEM_freeN ( self - > ptr . data ) ;
self - > ptr . data = NULL ;
2008-12-29 03:24:13 +00:00
}
2009-03-16 15:54:43 +00:00
Py_TYPE ( self ) - > tp_free ( self ) ;
2008-12-29 03:24:13 +00:00
return ;
}
2009-04-07 00:49:39 +00:00
2008-12-25 10:48:36 +00:00
static char * pyrna_enum_as_string ( PointerRNA * ptr , PropertyRNA * prop )
{
const EnumPropertyItem * item ;
2009-04-07 00:49:39 +00:00
int totitem ;
2008-12-25 10:48:36 +00:00
2009-04-01 12:43:07 +00:00
RNA_property_enum_items ( ptr , prop , & item , & totitem ) ;
2009-04-07 00:49:39 +00:00
return ( char * ) BPy_enum_as_string ( ( EnumPropertyItem * ) item ) ;
2008-12-25 10:48:36 +00:00
}
2008-12-02 14:36:35 +00:00
python operators (in bpy_opwrapper.*)
This means you can define an operator in python that is called from C or Python - like any other operator.
Python functions for invoke and exec can be registered with an operator name.
keywords are read from the python exec() function, then used to create operator properties. The default python values are used to set the property type and defaults.
def exec(size=2.0, text="blah"): ...
is equivalent to...
prop = RNA_def_property(ot->srna, "size", PROP_FLOAT, PROP_NONE);
RNA_def_property_float_default(prop, 2.0f);
prop = RNA_def_property(ot->srna, "size", PROP_STRING, PROP_NONE);
RNA_def_property_string_default(prop, "blah");
TODO -
* make use of events
* return OPERATOR_CANCELLED/OPERATOR_FINISHED.. etc
* add support for array args
* more testing
2008-12-27 14:52:49 +00:00
PyObject * pyrna_prop_to_py ( PointerRNA * ptr , PropertyRNA * prop )
2008-11-29 13:36:08 +00:00
{
PyObject * ret ;
2009-04-19 13:37:59 +00:00
int type = RNA_property_type ( prop ) ;
int len = RNA_property_array_length ( prop ) ;
2009-03-05 08:53:29 +00:00
2008-11-29 13:36:08 +00:00
if ( len > 0 ) {
/* resolve the array from a new pytype */
2009-06-22 04:26:48 +00:00
PyObject * ret = pyrna_prop_CreatePyObject ( ptr , prop ) ;
# ifdef USE_MATHUTILS
/* return a mathutils vector where possible */
if ( RNA_property_type ( prop ) = = PROP_FLOAT & &
RNA_property_subtype ( prop ) = = PROP_VECTOR & &
len > = 2 & & len < = 4 )
{
PyObject * vec_cb = newVectorObject_cb ( ret , len , mathutils_rna_vector_cb_index , 0 ) ;
Py_DECREF ( ret ) ; /* the vector owns now */
ret = vec_cb ; /* return the vector instead */
}
# endif
return ret ;
2008-11-29 13:36:08 +00:00
}
/* see if we can coorce into a python type - PropertyType */
switch ( type ) {
case PROP_BOOLEAN :
ret = PyBool_FromLong ( RNA_property_boolean_get ( ptr , prop ) ) ;
break ;
case PROP_INT :
2009-03-29 04:34:20 +00:00
ret = PyLong_FromSsize_t ( ( Py_ssize_t ) RNA_property_int_get ( ptr , prop ) ) ;
2008-11-29 13:36:08 +00:00
break ;
case PROP_FLOAT :
ret = PyFloat_FromDouble ( RNA_property_float_get ( ptr , prop ) ) ;
break ;
case PROP_STRING :
{
char * buf ;
buf = RNA_property_string_get_alloc ( ptr , prop , NULL , - 1 ) ;
ret = PyUnicode_FromString ( buf ) ;
MEM_freeN ( buf ) ;
break ;
}
case PROP_ENUM :
2008-12-02 14:36:35 +00:00
{
const char * identifier ;
int val = RNA_property_enum_get ( ptr , prop ) ;
2008-11-29 17:58:17 +00:00
2008-12-02 14:36:35 +00:00
if ( RNA_property_enum_identifier ( ptr , prop , val , & identifier ) ) {
ret = PyUnicode_FromString ( identifier ) ;
2008-11-29 17:58:17 +00:00
} else {
2009-03-05 08:53:29 +00:00
PyErr_Format ( PyExc_AttributeError , " RNA Error: Current value \" %d \" matches no enum " , val ) ;
2008-11-29 17:58:17 +00:00
ret = NULL ;
2008-11-29 13:36:08 +00:00
}
2008-12-02 14:36:35 +00:00
2008-11-29 13:36:08 +00:00
break ;
}
case PROP_POINTER :
{
PointerRNA newptr ;
2009-02-02 19:57:57 +00:00
newptr = RNA_property_pointer_get ( ptr , prop ) ;
2008-11-29 13:36:08 +00:00
if ( newptr . data ) {
ret = pyrna_struct_CreatePyObject ( & newptr ) ;
} else {
ret = Py_None ;
Py_INCREF ( ret ) ;
}
break ;
}
case PROP_COLLECTION :
2009-03-05 12:09:30 +00:00
ret = pyrna_prop_CreatePyObject ( ptr , prop ) ;
2008-11-29 13:36:08 +00:00
break ;
default :
2009-03-05 08:53:29 +00:00
PyErr_Format ( PyExc_AttributeError , " RNA Error: unknown type \" %d \" (pyrna_prop_to_py) " , type ) ;
2008-11-29 13:36:08 +00:00
ret = NULL ;
break ;
}
return ret ;
}
PyRNA
- Support for python to convert a PyObject into a collection (uses a list of dicts - quite verbose :/)
- Operators can now take collection args when called from python.
- Support for printing operators that use collections (macro recording).
- Added RNA_pointer_as_string which prints all pointer prop values as a python dict.
Example that can run in the in test.py (F7 key)
bpy.ops.VIEW3D_OT_select_lasso(path=[{"loc":(0, 0), "time":0}, {"loc":(1000, 0), "time":0}, {"loc":(1000, 1000), "time":0}], type='SELECT')
for some reason lasso locations always print as 0,0. Need to look into why this is.
2009-06-05 12:48:58 +00:00
/* This function is only used by operators right now
* Its used for taking keyword args and filling in property values */
int pyrna_pydict_to_props ( PointerRNA * ptr , PyObject * kw , const char * error_prefix )
{
int error_val = 0 ;
int totkw ;
const char * arg_name = NULL ;
PyObject * item ;
PropertyRNA * prop , * iterprop ;
CollectionPropertyIterator iter ;
iterprop = RNA_struct_iterator_property ( ptr - > type ) ;
RNA_property_collection_begin ( ptr , iterprop , & iter ) ;
totkw = kw ? PyDict_Size ( kw ) : 0 ;
for ( ; iter . valid ; RNA_property_collection_next ( & iter ) ) {
prop = iter . ptr . data ;
arg_name = RNA_property_identifier ( prop ) ;
if ( strcmp ( arg_name , " rna_type " ) = = 0 ) continue ;
if ( kw = = NULL ) {
PyErr_Format ( PyExc_AttributeError , " %s: no keywords, expected \" %s \" " , error_prefix , arg_name ? arg_name : " <UNKNOWN> " ) ;
error_val = - 1 ;
break ;
}
item = PyDict_GetItemString ( kw , arg_name ) ;
if ( item = = NULL ) {
PyErr_Format ( PyExc_AttributeError , " %s: keyword \" %s \" missing " , error_prefix , arg_name ? arg_name : " <UNKNOWN> " ) ;
error_val = - 1 ; /* pyrna_py_to_prop sets the error */
break ;
}
if ( pyrna_py_to_prop ( ptr , prop , NULL , item ) ) {
error_val = - 1 ;
break ;
}
totkw - - ;
}
RNA_property_collection_end ( & iter ) ;
if ( error_val = = 0 & & totkw > 0 ) { /* some keywords were given that were not used :/ */
PyObject * key , * value ;
Py_ssize_t pos = 0 ;
while ( PyDict_Next ( kw , & pos , & key , & value ) ) {
arg_name = _PyUnicode_AsString ( key ) ;
if ( RNA_struct_find_property ( ptr , arg_name ) = = NULL ) break ;
arg_name = NULL ;
}
PyErr_Format ( PyExc_AttributeError , " %s: keyword \" %s \" unrecognized " , error_prefix , arg_name ? arg_name : " <UNKNOWN> " ) ;
error_val = - 1 ;
}
return error_val ;
}
2009-04-09 13:20:48 +00:00
static PyObject * pyrna_func_call ( PyObject * self , PyObject * args , PyObject * kw ) ;
2009-04-07 00:49:39 +00:00
PyObject * pyrna_func_to_py ( PointerRNA * ptr , FunctionRNA * func )
2009-04-09 17:31:23 +00:00
{
2009-04-09 13:20:48 +00:00
static PyMethodDef func_meth = { " <generic rna function> " , ( PyCFunction ) pyrna_func_call , METH_VARARGS | METH_KEYWORDS , " python rna function " } ;
PyObject * self = PyTuple_New ( 2 ) ;
PyObject * ret ;
PyTuple_SET_ITEM ( self , 0 , pyrna_struct_CreatePyObject ( ptr ) ) ;
PyTuple_SET_ITEM ( self , 1 , PyCObject_FromVoidPtr ( ( void * ) func , NULL ) ) ;
ret = PyCFunction_New ( & func_meth , self ) ;
Py_DECREF ( self ) ;
return ret ;
2009-04-07 00:49:39 +00:00
}
2008-11-29 17:58:17 +00:00
2009-04-16 13:21:18 +00:00
int pyrna_py_to_prop ( PointerRNA * ptr , PropertyRNA * prop , void * data , PyObject * value )
2008-11-29 17:58:17 +00:00
{
2009-04-16 13:21:18 +00:00
/* XXX hard limits should be checked here */
2009-04-19 13:37:59 +00:00
int type = RNA_property_type ( prop ) ;
int len = RNA_property_array_length ( prop ) ;
2008-11-29 17:58:17 +00:00
if ( len > 0 ) {
2008-11-30 03:52:07 +00:00
PyObject * item ;
int i ;
2008-11-29 17:58:17 +00:00
2008-11-30 03:52:07 +00:00
if ( ! PySequence_Check ( value ) ) {
PyErr_SetString ( PyExc_TypeError , " expected a python sequence type assigned to an RNA array. " ) ;
return - 1 ;
2008-11-29 17:58:17 +00:00
}
2008-11-30 03:52:07 +00:00
if ( ( int ) PySequence_Length ( value ) ! = len ) {
PyErr_SetString ( PyExc_AttributeError , " python sequence length did not match the RNA array. " ) ;
return - 1 ;
2008-11-29 17:58:17 +00:00
}
2008-11-30 03:52:07 +00:00
/* for arrays we have a limited number of types */
switch ( type ) {
case PROP_BOOLEAN :
{
2009-04-16 13:21:18 +00:00
int * param_arr ;
if ( data ) param_arr = ( int * ) data ;
else param_arr = MEM_mallocN ( sizeof ( char ) * len , " pyrna bool array " ) ;
2008-11-30 03:52:07 +00:00
/* collect the variables before assigning, incase one of them is incorrect */
for ( i = 0 ; i < len ; i + + ) {
item = PySequence_GetItem ( value , i ) ;
param_arr [ i ] = PyObject_IsTrue ( item ) ;
Py_DECREF ( item ) ;
if ( param_arr [ i ] < 0 ) {
2009-04-16 13:21:18 +00:00
if ( data = = NULL )
MEM_freeN ( param_arr ) ;
2008-11-30 03:52:07 +00:00
PyErr_SetString ( PyExc_AttributeError , " one or more of the values in the sequence is not a boolean " ) ;
return - 1 ;
}
}
2009-04-16 13:21:18 +00:00
if ( data = = NULL ) {
RNA_property_boolean_set_array ( ptr , prop , param_arr ) ;
MEM_freeN ( param_arr ) ;
}
2008-11-30 03:52:07 +00:00
break ;
2008-11-29 17:58:17 +00:00
}
2008-11-30 03:52:07 +00:00
case PROP_INT :
{
2009-04-16 13:21:18 +00:00
int * param_arr ;
if ( data ) param_arr = ( int * ) data ;
else param_arr = MEM_mallocN ( sizeof ( int ) * len , " pyrna int array " ) ;
2008-11-30 03:52:07 +00:00
2009-04-16 13:21:18 +00:00
/* collect the variables */
2008-11-30 03:52:07 +00:00
for ( i = 0 ; i < len ; i + + ) {
item = PySequence_GetItem ( value , i ) ;
param_arr [ i ] = ( int ) PyLong_AsSsize_t ( item ) ; /* deal with any errors later */
Py_DECREF ( item ) ;
}
if ( PyErr_Occurred ( ) ) {
2009-04-16 13:21:18 +00:00
if ( data = = NULL )
MEM_freeN ( param_arr ) ;
2008-11-30 03:52:07 +00:00
PyErr_SetString ( PyExc_AttributeError , " one or more of the values in the sequence could not be used as an int " ) ;
return - 1 ;
}
2009-04-16 13:21:18 +00:00
if ( data = = NULL ) {
RNA_property_int_set_array ( ptr , prop , param_arr ) ;
MEM_freeN ( param_arr ) ;
}
2008-11-30 03:52:07 +00:00
break ;
}
case PROP_FLOAT :
{
2009-04-16 13:21:18 +00:00
float * param_arr ;
if ( data ) param_arr = ( float * ) data ;
else param_arr = MEM_mallocN ( sizeof ( float ) * len , " pyrna float array " ) ;
2008-11-30 03:52:07 +00:00
2009-04-16 13:21:18 +00:00
/* collect the variables */
2008-11-30 03:52:07 +00:00
for ( i = 0 ; i < len ; i + + ) {
item = PySequence_GetItem ( value , i ) ;
param_arr [ i ] = ( float ) PyFloat_AsDouble ( item ) ; /* deal with any errors later */
Py_DECREF ( item ) ;
}
if ( PyErr_Occurred ( ) ) {
2009-04-16 13:21:18 +00:00
if ( data = = NULL )
MEM_freeN ( param_arr ) ;
2008-11-30 03:52:07 +00:00
PyErr_SetString ( PyExc_AttributeError , " one or more of the values in the sequence could not be used as a float " ) ;
return - 1 ;
}
2009-04-16 13:21:18 +00:00
if ( data = = NULL ) {
RNA_property_float_set_array ( ptr , prop , param_arr ) ;
MEM_freeN ( param_arr ) ;
}
2008-11-30 03:52:07 +00:00
break ;
}
}
} else {
/* Normal Property (not an array) */
2008-11-29 17:58:17 +00:00
2008-11-30 03:52:07 +00:00
/* see if we can coorce into a python type - PropertyType */
switch ( type ) {
case PROP_BOOLEAN :
{
int param = PyObject_IsTrue ( value ) ;
2008-11-29 17:58:17 +00:00
2008-11-30 03:52:07 +00:00
if ( param < 0 ) {
PyErr_SetString ( PyExc_TypeError , " expected True/False or 0/1 " ) ;
return - 1 ;
} else {
2009-04-16 13:21:18 +00:00
if ( data ) * ( ( int * ) data ) = param ;
else RNA_property_boolean_set ( ptr , prop , param ) ;
2008-11-30 03:52:07 +00:00
}
break ;
}
case PROP_INT :
{
int param = PyLong_AsSsize_t ( value ) ;
if ( PyErr_Occurred ( ) ) {
PyErr_SetString ( PyExc_TypeError , " expected an int type " ) ;
return - 1 ;
} else {
2009-04-16 13:21:18 +00:00
if ( data ) * ( ( int * ) data ) = param ;
else RNA_property_int_set ( ptr , prop , param ) ;
2008-11-30 03:52:07 +00:00
}
break ;
}
case PROP_FLOAT :
{
float param = PyFloat_AsDouble ( value ) ;
if ( PyErr_Occurred ( ) ) {
PyErr_SetString ( PyExc_TypeError , " expected a float type " ) ;
return - 1 ;
} else {
2009-04-16 13:21:18 +00:00
if ( data ) * ( ( float * ) data ) = param ;
else RNA_property_float_set ( ptr , prop , param ) ;
2008-11-30 03:52:07 +00:00
}
break ;
}
case PROP_STRING :
{
char * param = _PyUnicode_AsString ( value ) ;
2008-11-29 17:58:17 +00:00
2008-11-30 03:52:07 +00:00
if ( param = = NULL ) {
PyErr_SetString ( PyExc_TypeError , " expected a string type " ) ;
return - 1 ;
} else {
2009-04-16 13:21:18 +00:00
if ( data ) * ( ( char * * ) data ) = param ;
else RNA_property_string_set ( ptr , prop , param ) ;
2008-11-29 17:58:17 +00:00
}
2008-11-30 03:52:07 +00:00
break ;
}
case PROP_ENUM :
{
char * param = _PyUnicode_AsString ( value ) ;
2008-11-29 17:58:17 +00:00
2008-11-30 03:52:07 +00:00
if ( param = = NULL ) {
2008-12-25 10:48:36 +00:00
char * enum_str = pyrna_enum_as_string ( ptr , prop ) ;
PyErr_Format ( PyExc_TypeError , " expected a string enum type in (%s) " , enum_str ) ;
MEM_freeN ( enum_str ) ;
2008-11-30 03:52:07 +00:00
return - 1 ;
2008-11-29 17:58:17 +00:00
} else {
2008-12-02 14:36:35 +00:00
int val ;
if ( RNA_property_enum_value ( ptr , prop , param , & val ) ) {
2009-04-16 13:21:18 +00:00
if ( data ) * ( ( int * ) data ) = val ;
else RNA_property_enum_set ( ptr , prop , val ) ;
2008-11-30 03:52:07 +00:00
} else {
2008-12-25 10:48:36 +00:00
char * enum_str = pyrna_enum_as_string ( ptr , prop ) ;
PyErr_Format ( PyExc_AttributeError , " enum \" %s \" not found in (%s) " , param , enum_str ) ;
MEM_freeN ( enum_str ) ;
2008-11-30 03:52:07 +00:00
return - 1 ;
}
2008-11-29 17:58:17 +00:00
}
2008-11-30 03:52:07 +00:00
break ;
}
case PROP_POINTER :
{
2009-06-07 13:09:18 +00:00
StructRNA * ptype = RNA_property_pointer_type ( ptr , prop ) ;
2009-03-23 13:28:42 +00:00
2009-06-07 13:09:18 +00:00
if ( ! BPy_StructRNA_Check ( value ) & & value ! = Py_None ) {
2009-03-23 13:28:42 +00:00
PointerRNA tmp ;
RNA_pointer_create ( NULL , ptype , NULL , & tmp ) ;
2009-04-19 13:37:59 +00:00
PyErr_Format ( PyExc_TypeError , " expected a %s type " , RNA_struct_identifier ( tmp . type ) ) ;
2009-03-23 13:28:42 +00:00
return - 1 ;
} else {
BPy_StructRNA * param = ( BPy_StructRNA * ) value ;
2009-04-16 13:21:18 +00:00
int raise_error = 0 ;
if ( data ) {
2009-06-16 00:52:21 +00:00
int flag = RNA_property_flag ( prop ) ;
if ( flag & PROP_RNAPTR ) {
2009-06-07 13:09:18 +00:00
if ( value = = Py_None )
memset ( data , 0 , sizeof ( PointerRNA ) ) ;
else
* ( ( PointerRNA * ) data ) = param - > ptr ;
}
else if ( value = = Py_None ) {
* ( ( void * * ) data ) = NULL ;
2009-04-16 13:21:18 +00:00
}
2009-04-19 13:37:59 +00:00
else if ( RNA_struct_is_a ( param - > ptr . type , ptype ) ) {
2009-04-16 13:21:18 +00:00
* ( ( void * * ) data ) = param - > ptr . data ;
2009-06-07 13:09:18 +00:00
}
else {
2009-04-16 13:21:18 +00:00
raise_error = 1 ;
}
}
else {
/* data==NULL, assign to RNA */
2009-06-07 13:09:18 +00:00
if ( value = = Py_None ) {
PointerRNA valueptr ;
memset ( & valueptr , 0 , sizeof ( valueptr ) ) ;
RNA_property_pointer_set ( ptr , prop , valueptr ) ;
}
else if ( RNA_struct_is_a ( param - > ptr . type , ptype ) ) {
2009-04-16 13:21:18 +00:00
RNA_property_pointer_set ( ptr , prop , param - > ptr ) ;
2009-06-07 13:09:18 +00:00
}
else {
2009-04-16 13:21:18 +00:00
PointerRNA tmp ;
RNA_pointer_create ( NULL , ptype , NULL , & tmp ) ;
2009-04-19 13:37:59 +00:00
PyErr_Format ( PyExc_TypeError , " expected a %s type " , RNA_struct_identifier ( tmp . type ) ) ;
2009-04-16 13:21:18 +00:00
return - 1 ;
}
}
if ( raise_error ) {
2009-03-23 13:28:42 +00:00
PointerRNA tmp ;
RNA_pointer_create ( NULL , ptype , NULL , & tmp ) ;
2009-04-19 13:37:59 +00:00
PyErr_Format ( PyExc_TypeError , " expected a %s type " , RNA_struct_identifier ( tmp . type ) ) ;
2009-03-23 13:28:42 +00:00
return - 1 ;
}
}
2008-11-30 03:52:07 +00:00
break ;
}
case PROP_COLLECTION :
PyRNA
- Support for python to convert a PyObject into a collection (uses a list of dicts - quite verbose :/)
- Operators can now take collection args when called from python.
- Support for printing operators that use collections (macro recording).
- Added RNA_pointer_as_string which prints all pointer prop values as a python dict.
Example that can run in the in test.py (F7 key)
bpy.ops.VIEW3D_OT_select_lasso(path=[{"loc":(0, 0), "time":0}, {"loc":(1000, 0), "time":0}, {"loc":(1000, 1000), "time":0}], type='SELECT')
for some reason lasso locations always print as 0,0. Need to look into why this is.
2009-06-05 12:48:58 +00:00
{
int seq_len , i ;
PyObject * item ;
PointerRNA itemptr ;
/* convert a sequence of dict's into a collection */
if ( ! PySequence_Check ( value ) ) {
PyErr_SetString ( PyExc_TypeError , " expected a sequence of dicts for an RNA collection " ) ;
return - 1 ;
}
seq_len = PySequence_Length ( value ) ;
for ( i = 0 ; i < seq_len ; i + + ) {
item = PySequence_GetItem ( value , i ) ;
if ( item = = NULL | | PyDict_Check ( item ) = = 0 ) {
PyErr_SetString ( PyExc_TypeError , " expected a sequence of dicts for an RNA collection " ) ;
Py_XDECREF ( item ) ;
return - 1 ;
}
RNA_property_collection_add ( ptr , prop , & itemptr ) ;
if ( pyrna_pydict_to_props ( & itemptr , item , " Converting a python list to an RNA collection " ) = = - 1 ) {
Py_DECREF ( item ) ;
return - 1 ;
}
Py_DECREF ( item ) ;
}
2008-11-30 03:52:07 +00:00
break ;
PyRNA
- Support for python to convert a PyObject into a collection (uses a list of dicts - quite verbose :/)
- Operators can now take collection args when called from python.
- Support for printing operators that use collections (macro recording).
- Added RNA_pointer_as_string which prints all pointer prop values as a python dict.
Example that can run in the in test.py (F7 key)
bpy.ops.VIEW3D_OT_select_lasso(path=[{"loc":(0, 0), "time":0}, {"loc":(1000, 0), "time":0}, {"loc":(1000, 1000), "time":0}], type='SELECT')
for some reason lasso locations always print as 0,0. Need to look into why this is.
2009-06-05 12:48:58 +00:00
}
2008-11-30 03:52:07 +00:00
default :
PyErr_SetString ( PyExc_AttributeError , " unknown property type (pyrna_py_to_prop) " ) ;
return - 1 ;
break ;
2008-11-29 17:58:17 +00:00
}
}
2008-11-30 03:52:07 +00:00
return 0 ;
2008-11-29 17:58:17 +00:00
}
2008-11-29 13:36:08 +00:00
static PyObject * pyrna_prop_to_py_index ( PointerRNA * ptr , PropertyRNA * prop , int index )
{
PyObject * ret ;
2009-04-19 13:37:59 +00:00
int type = RNA_property_type ( prop ) ;
2008-11-29 13:36:08 +00:00
/* see if we can coorce into a python type - PropertyType */
switch ( type ) {
case PROP_BOOLEAN :
2009-02-02 19:57:57 +00:00
ret = PyBool_FromLong ( RNA_property_boolean_get_index ( ptr , prop , index ) ) ;
2008-11-29 13:36:08 +00:00
break ;
case PROP_INT :
2009-03-29 04:34:20 +00:00
ret = PyLong_FromSsize_t ( ( Py_ssize_t ) RNA_property_int_get_index ( ptr , prop , index ) ) ;
2008-11-29 13:36:08 +00:00
break ;
case PROP_FLOAT :
2009-02-02 19:57:57 +00:00
ret = PyFloat_FromDouble ( RNA_property_float_get_index ( ptr , prop , index ) ) ;
2008-11-29 13:36:08 +00:00
break ;
default :
PyErr_SetString ( PyExc_AttributeError , " not an array type " ) ;
ret = NULL ;
break ;
}
return ret ;
}
2008-11-29 17:58:17 +00:00
static int pyrna_py_to_prop_index ( PointerRNA * ptr , PropertyRNA * prop , int index , PyObject * value )
{
int ret = 0 ;
2009-04-19 13:37:59 +00:00
int type = RNA_property_type ( prop ) ;
2008-11-29 17:58:17 +00:00
/* see if we can coorce into a python type - PropertyType */
switch ( type ) {
case PROP_BOOLEAN :
{
int param = PyObject_IsTrue ( value ) ;
2008-11-30 03:52:07 +00:00
if ( param < 0 ) {
2008-11-29 17:58:17 +00:00
PyErr_SetString ( PyExc_TypeError , " expected True/False or 0/1 " ) ;
ret = - 1 ;
} else {
2009-02-02 19:57:57 +00:00
RNA_property_boolean_set_index ( ptr , prop , index , param ) ;
2008-11-29 17:58:17 +00:00
}
break ;
}
case PROP_INT :
{
int param = PyLong_AsSsize_t ( value ) ;
if ( PyErr_Occurred ( ) ) {
PyErr_SetString ( PyExc_TypeError , " expected an int type " ) ;
ret = - 1 ;
} else {
2009-02-02 19:57:57 +00:00
RNA_property_int_set_index ( ptr , prop , index , param ) ;
2008-11-29 17:58:17 +00:00
}
break ;
}
case PROP_FLOAT :
{
float param = PyFloat_AsDouble ( value ) ;
if ( PyErr_Occurred ( ) ) {
PyErr_SetString ( PyExc_TypeError , " expected a float type " ) ;
ret = - 1 ;
} else {
2009-02-02 19:57:57 +00:00
RNA_property_float_set_index ( ptr , prop , index , param ) ;
2008-11-29 17:58:17 +00:00
}
break ;
}
default :
PyErr_SetString ( PyExc_AttributeError , " not an array type " ) ;
ret = - 1 ;
break ;
}
return ret ;
}
2008-11-29 13:36:08 +00:00
//---------------sequence-------------------------------------------
static Py_ssize_t pyrna_prop_len ( BPy_PropertyRNA * self )
{
Py_ssize_t len ;
2009-04-19 13:37:59 +00:00
if ( RNA_property_type ( self - > prop ) = = PROP_COLLECTION ) {
2008-11-29 13:36:08 +00:00
len = RNA_property_collection_length ( & self - > ptr , self - > prop ) ;
} else {
2009-04-19 13:37:59 +00:00
len = RNA_property_array_length ( self - > prop ) ;
2008-11-29 13:36:08 +00:00
if ( len = = 0 ) { /* not an array*/
PyErr_SetString ( PyExc_AttributeError , " len() only available for collection RNA types " ) ;
return - 1 ;
}
}
return len ;
}
static PyObject * pyrna_prop_subscript ( BPy_PropertyRNA * self , PyObject * key )
{
PyObject * ret ;
PointerRNA newptr ;
2009-01-23 20:36:47 +00:00
int keynum = 0 ;
2008-11-29 13:36:08 +00:00
char * keyname = NULL ;
if ( PyUnicode_Check ( key ) ) {
keyname = _PyUnicode_AsString ( key ) ;
} else if ( PyLong_Check ( key ) ) {
keynum = PyLong_AsSsize_t ( key ) ;
} else {
PyErr_SetString ( PyExc_AttributeError , " invalid key, key must be a string or an int " ) ;
return NULL ;
}
2009-04-19 13:37:59 +00:00
if ( RNA_property_type ( self - > prop ) = = PROP_COLLECTION ) {
2008-11-29 13:36:08 +00:00
int ok ;
if ( keyname ) ok = RNA_property_collection_lookup_string ( & self - > ptr , self - > prop , keyname , & newptr ) ;
else ok = RNA_property_collection_lookup_int ( & self - > ptr , self - > prop , keynum , & newptr ) ;
if ( ok ) {
ret = pyrna_struct_CreatePyObject ( & newptr ) ;
} else {
PyErr_SetString ( PyExc_AttributeError , " out of range " ) ;
ret = NULL ;
}
} else if ( keyname ) {
PyErr_SetString ( PyExc_AttributeError , " string keys are only supported for collections " ) ;
ret = NULL ;
} else {
2009-04-19 13:37:59 +00:00
int len = RNA_property_array_length ( self - > prop ) ;
2008-11-29 13:36:08 +00:00
if ( len = = 0 ) { /* not an array*/
PyErr_Format ( PyExc_AttributeError , " not an array or collection %d " , keynum ) ;
ret = NULL ;
}
if ( keynum > = len ) {
PyErr_SetString ( PyExc_AttributeError , " index out of range " ) ;
ret = NULL ;
} else { /* not an array*/
ret = pyrna_prop_to_py_index ( & self - > ptr , self - > prop , keynum ) ;
}
}
return ret ;
}
2008-11-29 17:58:17 +00:00
static int pyrna_prop_assign_subscript ( BPy_PropertyRNA * self , PyObject * key , PyObject * value )
{
int ret = 0 ;
2009-01-23 20:36:47 +00:00
int keynum = 0 ;
2008-11-29 17:58:17 +00:00
char * keyname = NULL ;
2008-11-30 03:52:07 +00:00
if ( ! RNA_property_editable ( & self - > ptr , self - > prop ) ) {
2009-04-19 13:37:59 +00:00
PyErr_Format ( PyExc_AttributeError , " PropertyRNA - attribute \" %s \" from \" %s \" is read-only " , RNA_property_identifier ( self - > prop ) , RNA_struct_identifier ( self - > ptr . type ) ) ;
2008-11-30 03:52:07 +00:00
return - 1 ;
}
2008-11-29 17:58:17 +00:00
if ( PyUnicode_Check ( key ) ) {
keyname = _PyUnicode_AsString ( key ) ;
} else if ( PyLong_Check ( key ) ) {
keynum = PyLong_AsSsize_t ( key ) ;
} else {
2009-03-16 15:54:43 +00:00
PyErr_SetString ( PyExc_AttributeError , " PropertyRNA - invalid key, key must be a string or an int " ) ;
2008-11-29 17:58:17 +00:00
return - 1 ;
}
2009-04-19 13:37:59 +00:00
if ( RNA_property_type ( self - > prop ) = = PROP_COLLECTION ) {
2009-03-16 15:54:43 +00:00
PyErr_SetString ( PyExc_AttributeError , " PropertyRNA - assignment is not supported for collections (yet) " ) ;
2008-11-29 17:58:17 +00:00
ret = - 1 ;
} else if ( keyname ) {
2009-03-16 15:54:43 +00:00
PyErr_SetString ( PyExc_AttributeError , " PropertyRNA - string keys are only supported for collections " ) ;
2008-11-29 17:58:17 +00:00
ret = - 1 ;
} else {
2009-04-19 13:37:59 +00:00
int len = RNA_property_array_length ( self - > prop ) ;
2008-11-29 17:58:17 +00:00
if ( len = = 0 ) { /* not an array*/
2009-03-16 15:54:43 +00:00
PyErr_Format ( PyExc_AttributeError , " PropertyRNA - not an array or collection %d " , keynum ) ;
2008-11-29 17:58:17 +00:00
ret = - 1 ;
}
if ( keynum > = len ) {
2009-03-16 15:54:43 +00:00
PyErr_SetString ( PyExc_AttributeError , " PropertyRNA - index out of range " ) ;
2008-11-29 17:58:17 +00:00
ret = - 1 ;
2009-03-05 12:09:30 +00:00
} else {
2008-11-29 17:58:17 +00:00
ret = pyrna_py_to_prop_index ( & self - > ptr , self - > prop , keynum , value ) ;
}
}
return ret ;
}
2008-11-29 13:36:08 +00:00
static PyMappingMethods pyrna_prop_as_mapping = {
2009-04-07 15:20:12 +00:00
( lenfunc ) pyrna_prop_len , /* mp_length */
2008-11-29 13:36:08 +00:00
( binaryfunc ) pyrna_prop_subscript , /* mp_subscript */
2008-11-29 17:58:17 +00:00
( objobjargproc ) pyrna_prop_assign_subscript , /* mp_ass_subscript */
2008-11-29 13:36:08 +00:00
} ;
2009-06-13 08:04:43 +00:00
static int pyrna_prop_contains ( BPy_PropertyRNA * self , PyObject * value )
{
PointerRNA newptr ; /* not used, just so RNA_property_collection_lookup_string runs */
char * keyname = _PyUnicode_AsString ( value ) ;
if ( keyname = = NULL ) {
2009-06-20 02:44:57 +00:00
PyErr_SetString ( PyExc_TypeError , " PropertyRNA - key in prop, key must be a string type " ) ;
2009-06-13 08:04:43 +00:00
return - 1 ;
}
if ( RNA_property_type ( self - > prop ) ! = PROP_COLLECTION ) {
2009-06-20 02:44:57 +00:00
PyErr_SetString ( PyExc_TypeError , " PropertyRNA - key in prop, is only valid for collection types " ) ;
2009-06-13 08:04:43 +00:00
return - 1 ;
}
if ( RNA_property_collection_lookup_string ( & self - > ptr , self - > prop , keyname , & newptr ) )
return 1 ;
return 0 ;
}
static PySequenceMethods pyrna_prop_as_sequence = {
NULL , /* Cant set the len otherwise it can evaluate as false */
NULL , /* sq_concat */
NULL , /* sq_repeat */
NULL , /* sq_item */
NULL , /* sq_slice */
NULL , /* sq_ass_item */
NULL , /* sq_ass_slice */
( objobjproc ) pyrna_prop_contains , /* sq_contains */
} ;
2009-03-11 17:28:37 +00:00
static PyObject * pyrna_struct_dir ( BPy_StructRNA * self )
2008-11-30 14:00:14 +00:00
{
2009-03-11 17:28:37 +00:00
PyObject * ret , * dict ;
2009-03-21 06:55:30 +00:00
PyObject * pystring ;
2008-11-29 13:36:08 +00:00
2009-04-09 16:52:18 +00:00
/* for looping over attrs and funcs */
CollectionPropertyIterator iter ;
PropertyRNA * iterprop ;
2008-12-01 16:59:18 +00:00
/* Include this incase this instance is a subtype of a python class
* In these instances we may want to return a function or variable provided by the subtype
* */
2009-03-21 06:55:30 +00:00
if ( BPy_StructRNA_CheckExact ( self ) ) {
2009-03-11 17:28:37 +00:00
ret = PyList_New ( 0 ) ;
2009-03-21 06:55:30 +00:00
} else {
pystring = PyUnicode_FromString ( " __dict__ " ) ;
dict = PyObject_GenericGetAttr ( ( PyObject * ) self , pystring ) ;
Py_DECREF ( pystring ) ;
if ( dict = = NULL ) {
PyErr_Clear ( ) ;
ret = PyList_New ( 0 ) ;
}
else {
ret = PyDict_Keys ( dict ) ;
Py_DECREF ( dict ) ;
}
2009-03-05 12:09:30 +00:00
}
2009-03-11 17:28:37 +00:00
/* Collect RNA items*/
{
2009-04-09 16:52:18 +00:00
/*
* Collect RNA attributes
*/
PropertyRNA * nameprop ;
2009-03-05 12:09:30 +00:00
char name [ 256 ] , * nameptr ;
2009-04-19 13:37:59 +00:00
iterprop = RNA_struct_iterator_property ( self - > ptr . type ) ;
2009-03-05 12:09:30 +00:00
RNA_property_collection_begin ( & self - > ptr , iterprop , & iter ) ;
for ( ; iter . valid ; RNA_property_collection_next ( & iter ) ) {
2009-04-19 13:37:59 +00:00
if ( iter . ptr . data & & ( nameprop = RNA_struct_name_property ( iter . ptr . type ) ) ) {
2009-03-05 12:09:30 +00:00
nameptr = RNA_property_string_get_alloc ( & iter . ptr , nameprop , name , sizeof ( name ) ) ;
2009-03-11 17:28:37 +00:00
pystring = PyUnicode_FromString ( nameptr ) ;
PyList_Append ( ret , pystring ) ;
Py_DECREF ( pystring ) ;
2009-03-05 12:09:30 +00:00
if ( ( char * ) & name ! = nameptr )
MEM_freeN ( nameptr ) ;
}
2008-11-29 13:36:08 +00:00
}
2009-04-09 16:52:18 +00:00
RNA_property_collection_end ( & iter ) ;
}
{
/*
* Collect RNA function items
*/
PointerRNA tptr ;
RNA_pointer_create ( NULL , & RNA_Struct , self - > ptr . type , & tptr ) ;
iterprop = RNA_struct_find_property ( & tptr , " functions " ) ;
RNA_property_collection_begin ( & tptr , iterprop , & iter ) ;
for ( ; iter . valid ; RNA_property_collection_next ( & iter ) ) {
2009-04-19 13:37:59 +00:00
pystring = PyUnicode_FromString ( RNA_function_identifier ( iter . ptr . data ) ) ;
2009-04-09 16:52:18 +00:00
PyList_Append ( ret , pystring ) ;
Py_DECREF ( pystring ) ;
}
2009-03-05 12:09:30 +00:00
RNA_property_collection_end ( & iter ) ;
}
2009-06-20 14:55:28 +00:00
if ( self - > ptr . type = = & RNA_Context ) {
ListBase lb = CTX_data_dir_get ( self - > ptr . data ) ;
LinkData * link ;
for ( link = lb . first ; link ; link = link - > next ) {
pystring = PyUnicode_FromString ( link - > data ) ;
PyList_Append ( ret , pystring ) ;
Py_DECREF ( pystring ) ;
}
BLI_freelistN ( & lb ) ;
}
2009-03-11 17:28:37 +00:00
return ret ;
}
//---------------getattr--------------------------------------------
static PyObject * pyrna_struct_getattro ( BPy_StructRNA * self , PyObject * pyname )
{
char * name = _PyUnicode_AsString ( pyname ) ;
PyObject * ret ;
PropertyRNA * prop ;
2009-04-07 00:49:39 +00:00
FunctionRNA * func ;
2009-03-11 17:28:37 +00:00
/* Include this incase this instance is a subtype of a python class
* In these instances we may want to return a function or variable provided by the subtype
2009-03-21 06:55:30 +00:00
*
* Also needed to return methods when its not a subtype
2009-03-11 17:28:37 +00:00
* */
2009-03-21 06:55:30 +00:00
ret = PyObject_GenericGetAttr ( ( PyObject * ) self , pyname ) ;
if ( ret ) return ret ;
else PyErr_Clear ( ) ;
2009-03-11 17:28:37 +00:00
/* done with subtypes */
2009-04-07 00:49:39 +00:00
if ( ( prop = RNA_struct_find_property ( & self - > ptr , name ) ) ) {
ret = pyrna_prop_to_py ( & self - > ptr , prop ) ;
}
else if ( ( func = RNA_struct_find_function ( & self - > ptr , name ) ) ) {
ret = pyrna_func_to_py ( & self - > ptr , func ) ;
2009-03-11 17:28:37 +00:00
}
2009-04-07 00:49:39 +00:00
else if ( self - > ptr . type = = & RNA_Context ) {
2009-03-19 19:03:38 +00:00
PointerRNA newptr ;
ListBase newlb ;
CTX_data_get ( self - > ptr . data , name , & newptr , & newlb ) ;
if ( newptr . data ) {
ret = pyrna_struct_CreatePyObject ( & newptr ) ;
}
else if ( newlb . first ) {
CollectionPointerLink * link ;
PyObject * linkptr ;
ret = PyList_New ( 0 ) ;
for ( link = newlb . first ; link ; link = link - > next ) {
linkptr = pyrna_struct_CreatePyObject ( & link - > ptr ) ;
PyList_Append ( ret , linkptr ) ;
Py_DECREF ( linkptr ) ;
}
}
else {
ret = Py_None ;
Py_INCREF ( ret ) ;
}
BLI_freelistN ( & newlb ) ;
}
2009-03-05 12:09:30 +00:00
else {
2009-03-16 15:54:43 +00:00
PyErr_Format ( PyExc_AttributeError , " StructRNA - Attribute \" %s \" not found " , name ) ;
2009-03-05 12:09:30 +00:00
ret = NULL ;
2008-11-29 13:36:08 +00:00
}
2008-11-29 17:58:17 +00:00
return ret ;
}
2008-11-29 13:36:08 +00:00
2008-11-29 17:58:17 +00:00
//--------------- setattr-------------------------------------------
2008-12-01 16:59:18 +00:00
static int pyrna_struct_setattro ( BPy_StructRNA * self , PyObject * pyname , PyObject * value )
2008-11-29 17:58:17 +00:00
{
2008-12-01 16:59:18 +00:00
char * name = _PyUnicode_AsString ( pyname ) ;
2008-11-30 03:52:07 +00:00
PropertyRNA * prop = RNA_struct_find_property ( & self - > ptr , name ) ;
2008-11-29 17:58:17 +00:00
if ( prop = = NULL ) {
2009-03-16 15:54:43 +00:00
if ( ! BPy_StructRNA_CheckExact ( self ) & & PyObject_GenericSetAttr ( ( PyObject * ) self , pyname , value ) > = 0 ) {
return 0 ;
}
else {
PyErr_Format ( PyExc_AttributeError , " StructRNA - Attribute \" %s \" not found " , name ) ;
return - 1 ;
}
2008-11-30 03:52:07 +00:00
}
2008-11-29 13:36:08 +00:00
2008-11-30 03:52:07 +00:00
if ( ! RNA_property_editable ( & self - > ptr , prop ) ) {
2009-04-19 13:37:59 +00:00
PyErr_Format ( PyExc_AttributeError , " StructRNA - Attribute \" %s \" from \" %s \" is read-only " , RNA_property_identifier ( prop ) , RNA_struct_identifier ( self - > ptr . type ) ) ;
2008-11-30 03:52:07 +00:00
return - 1 ;
}
/* pyrna_py_to_prop sets its own exceptions */
2009-04-16 13:21:18 +00:00
return pyrna_py_to_prop ( & self - > ptr , prop , NULL , value ) ;
2008-11-29 13:36:08 +00:00
}
PyObject * pyrna_prop_keys ( BPy_PropertyRNA * self )
{
PyObject * ret ;
2009-04-19 13:37:59 +00:00
if ( RNA_property_type ( self - > prop ) ! = PROP_COLLECTION ) {
2008-11-29 13:36:08 +00:00
PyErr_SetString ( PyExc_TypeError , " keys() is only valid for collection types " ) ;
ret = NULL ;
} else {
PyObject * item ;
CollectionPropertyIterator iter ;
PropertyRNA * nameprop ;
char name [ 256 ] , * nameptr ;
ret = PyList_New ( 0 ) ;
RNA_property_collection_begin ( & self - > ptr , self - > prop , & iter ) ;
for ( ; iter . valid ; RNA_property_collection_next ( & iter ) ) {
2009-04-19 13:37:59 +00:00
if ( iter . ptr . data & & ( nameprop = RNA_struct_name_property ( iter . ptr . type ) ) ) {
2008-11-29 13:36:08 +00:00
nameptr = RNA_property_string_get_alloc ( & iter . ptr , nameprop , name , sizeof ( name ) ) ;
/* add to python list */
item = PyUnicode_FromString ( nameptr ) ;
PyList_Append ( ret , item ) ;
Py_DECREF ( item ) ;
/* done */
2008-11-30 02:30:34 +00:00
if ( ( char * ) & name ! = nameptr )
2008-11-29 13:36:08 +00:00
MEM_freeN ( nameptr ) ;
}
}
RNA_property_collection_end ( & iter ) ;
}
return ret ;
}
PyObject * pyrna_prop_items ( BPy_PropertyRNA * self )
{
PyObject * ret ;
2009-04-19 13:37:59 +00:00
if ( RNA_property_type ( self - > prop ) ! = PROP_COLLECTION ) {
2008-12-01 22:20:18 +00:00
PyErr_SetString ( PyExc_TypeError , " items() is only valid for collection types " ) ;
2008-11-29 13:36:08 +00:00
ret = NULL ;
} else {
PyObject * item ;
CollectionPropertyIterator iter ;
PropertyRNA * nameprop ;
char name [ 256 ] , * nameptr ;
2009-06-20 13:53:14 +00:00
int i = 0 ;
2008-11-29 13:36:08 +00:00
ret = PyList_New ( 0 ) ;
RNA_property_collection_begin ( & self - > ptr , self - > prop , & iter ) ;
for ( ; iter . valid ; RNA_property_collection_next ( & iter ) ) {
2009-06-20 13:53:14 +00:00
if ( iter . ptr . data ) {
2008-11-29 13:36:08 +00:00
/* add to python list */
2009-06-20 13:53:14 +00:00
item = PyTuple_New ( 2 ) ;
if ( nameprop = RNA_struct_name_property ( iter . ptr . type ) ) {
nameptr = RNA_property_string_get_alloc ( & iter . ptr , nameprop , name , sizeof ( name ) ) ;
PyTuple_SET_ITEM ( item , 0 , PyUnicode_FromString ( nameptr ) ) ;
if ( ( char * ) & name ! = nameptr )
MEM_freeN ( nameptr ) ;
}
else {
PyTuple_SET_ITEM ( item , 0 , PyLong_FromSsize_t ( i ) ) ; /* a bit strange but better then returning an empty list */
}
PyTuple_SET_ITEM ( item , 1 , pyrna_struct_CreatePyObject ( & iter . ptr ) ) ;
2008-11-29 13:36:08 +00:00
PyList_Append ( ret , item ) ;
Py_DECREF ( item ) ;
2009-06-20 13:53:14 +00:00
i + + ;
2008-11-29 13:36:08 +00:00
}
}
RNA_property_collection_end ( & iter ) ;
}
return ret ;
}
PyObject * pyrna_prop_values ( BPy_PropertyRNA * self )
{
PyObject * ret ;
2009-06-20 13:53:14 +00:00
2009-04-19 13:37:59 +00:00
if ( RNA_property_type ( self - > prop ) ! = PROP_COLLECTION ) {
2008-12-02 15:27:10 +00:00
PyErr_SetString ( PyExc_TypeError , " values() is only valid for collection types " ) ;
2008-11-29 13:36:08 +00:00
ret = NULL ;
} else {
PyObject * item ;
CollectionPropertyIterator iter ;
2009-06-20 13:53:14 +00:00
PropertyRNA * iterprop ;
2008-11-29 13:36:08 +00:00
ret = PyList_New ( 0 ) ;
2009-06-20 13:53:14 +00:00
//iterprop= RNA_struct_iterator_property(self->ptr.type);
2008-11-29 13:36:08 +00:00
RNA_property_collection_begin ( & self - > ptr , self - > prop , & iter ) ;
for ( ; iter . valid ; RNA_property_collection_next ( & iter ) ) {
2009-06-20 13:53:14 +00:00
item = pyrna_struct_CreatePyObject ( & iter . ptr ) ;
PyList_Append ( ret , item ) ;
Py_DECREF ( item ) ;
2008-11-29 13:36:08 +00:00
}
RNA_property_collection_end ( & iter ) ;
}
return ret ;
}
2008-12-02 15:27:10 +00:00
/* A bit of a kludge, make a list out of a collection or array,
* then return the lists iter function , not especially fast but convenient for now */
PyObject * pyrna_prop_iter ( BPy_PropertyRNA * self )
{
/* Try get values from a collection */
PyObject * ret = pyrna_prop_values ( self ) ;
if ( ret = = NULL ) {
/* collection did not work, try array */
2009-04-19 13:37:59 +00:00
int len = RNA_property_array_length ( self - > prop ) ;
2008-12-02 15:27:10 +00:00
if ( len ) {
int i ;
PyErr_Clear ( ) ;
ret = PyList_New ( len ) ;
for ( i = 0 ; i < len ; i + + ) {
PyList_SET_ITEM ( ret , i , pyrna_prop_to_py_index ( & self - > ptr , self - > prop , i ) ) ;
}
}
}
if ( ret ) {
/* we know this is a list so no need to PyIter_Check */
PyObject * iter = PyObject_GetIter ( ret ) ;
Py_DECREF ( ret ) ;
return iter ;
}
PyErr_SetString ( PyExc_TypeError , " this BPy_PropertyRNA object is not iterable " ) ;
return NULL ;
}
2009-03-11 17:28:37 +00:00
static struct PyMethodDef pyrna_struct_methods [ ] = {
2008-12-29 12:04:25 +00:00
{ " __dir__ " , ( PyCFunction ) pyrna_struct_dir , METH_NOARGS , " " } ,
{ NULL , NULL , 0 , NULL }
2009-03-11 17:28:37 +00:00
} ;
2008-11-29 13:36:08 +00:00
static struct PyMethodDef pyrna_prop_methods [ ] = {
{ " keys " , ( PyCFunction ) pyrna_prop_keys , METH_NOARGS , " " } ,
{ " items " , ( PyCFunction ) pyrna_prop_items , METH_NOARGS , " " } ,
{ " values " , ( PyCFunction ) pyrna_prop_values , METH_NOARGS , " " } ,
{ NULL , NULL , 0 , NULL }
} ;
2008-12-01 16:59:18 +00:00
/* only needed for subtyping, so a new class gets a valid BPy_StructRNA
* todo - also accept useful args */
static PyObject * pyrna_struct_new ( PyTypeObject * type , PyObject * args , PyObject * kwds ) {
BPy_StructRNA * base = NULL ;
if ( ! PyArg_ParseTuple ( args , " O!:Base BPy_StructRNA " , & pyrna_struct_Type , & base ) )
return NULL ;
if ( type = = & pyrna_struct_Type ) {
return pyrna_struct_CreatePyObject ( & base - > ptr ) ;
} else {
BPy_StructRNA * ret = ( BPy_StructRNA * ) type - > tp_alloc ( type , 0 ) ;
ret - > ptr = base - > ptr ;
return ( PyObject * ) ret ;
}
}
/* only needed for subtyping, so a new class gets a valid BPy_StructRNA
* todo - also accept useful args */
static PyObject * pyrna_prop_new ( PyTypeObject * type , PyObject * args , PyObject * kwds ) {
BPy_PropertyRNA * base = NULL ;
if ( ! PyArg_ParseTuple ( args , " O!:Base BPy_PropertyRNA " , & pyrna_prop_Type , & base ) )
return NULL ;
if ( type = = & pyrna_prop_Type ) {
return pyrna_prop_CreatePyObject ( & base - > ptr , base - > prop ) ;
} else {
BPy_PropertyRNA * ret = ( BPy_PropertyRNA * ) type - > tp_alloc ( type , 0 ) ;
ret - > ptr = base - > ptr ;
ret - > prop = base - > prop ;
return ( PyObject * ) ret ;
}
}
2009-04-07 00:49:39 +00:00
PyObject * pyrna_param_to_py ( PointerRNA * ptr , PropertyRNA * prop , void * data )
{
PyObject * ret ;
2009-04-19 13:37:59 +00:00
int type = RNA_property_type ( prop ) ;
int len = RNA_property_array_length ( prop ) ;
2009-04-19 20:09:31 +00:00
2009-04-07 00:49:39 +00:00
int a ;
if ( len > 0 ) {
/* resolve the array from a new pytype */
ret = PyTuple_New ( len ) ;
switch ( type ) {
case PROP_BOOLEAN :
for ( a = 0 ; a < len ; a + + )
PyTuple_SET_ITEM ( ret , a , PyBool_FromLong ( ( ( int * ) data ) [ a ] ) ) ;
break ;
case PROP_INT :
for ( a = 0 ; a < len ; a + + )
PyTuple_SET_ITEM ( ret , a , PyLong_FromSsize_t ( ( Py_ssize_t ) ( ( int * ) data ) [ a ] ) ) ;
break ;
case PROP_FLOAT :
for ( a = 0 ; a < len ; a + + )
PyTuple_SET_ITEM ( ret , a , PyFloat_FromDouble ( ( ( float * ) data ) [ a ] ) ) ;
break ;
default :
PyErr_Format ( PyExc_AttributeError , " RNA Error: unknown array type \" %d \" (pyrna_param_to_py) " , type ) ;
ret = NULL ;
break ;
}
}
else {
/* see if we can coorce into a python type - PropertyType */
switch ( type ) {
case PROP_BOOLEAN :
ret = PyBool_FromLong ( * ( int * ) data ) ;
break ;
case PROP_INT :
ret = PyLong_FromSsize_t ( ( Py_ssize_t ) * ( int * ) data ) ;
break ;
case PROP_FLOAT :
ret = PyFloat_FromDouble ( * ( float * ) data ) ;
break ;
case PROP_STRING :
{
ret = PyUnicode_FromString ( * ( char * * ) data ) ;
break ;
}
case PROP_ENUM :
{
const char * identifier ;
int val = * ( int * ) data ;
if ( RNA_property_enum_identifier ( ptr , prop , val , & identifier ) ) {
ret = PyUnicode_FromString ( identifier ) ;
} else {
PyErr_Format ( PyExc_AttributeError , " RNA Error: Current value \" %d \" matches no enum " , val ) ;
ret = NULL ;
}
break ;
}
case PROP_POINTER :
{
PointerRNA newptr ;
2009-06-07 13:09:18 +00:00
StructRNA * type = RNA_property_pointer_type ( ptr , prop ) ;
2009-06-16 00:52:21 +00:00
int flag = RNA_property_flag ( prop ) ;
2009-04-07 00:49:39 +00:00
2009-06-16 00:52:21 +00:00
if ( flag & PROP_RNAPTR ) {
2009-04-07 00:49:39 +00:00
/* in this case we get the full ptr */
newptr = * ( PointerRNA * ) data ;
}
else {
/* XXX this is missing the ID part! */
RNA_pointer_create ( NULL , type , * ( void * * ) data , & newptr ) ;
}
if ( newptr . data ) {
ret = pyrna_struct_CreatePyObject ( & newptr ) ;
} else {
ret = Py_None ;
Py_INCREF ( ret ) ;
}
break ;
}
case PROP_COLLECTION :
/* XXX not supported yet
* ret = pyrna_prop_CreatePyObject ( ptr , prop ) ; */
2009-05-20 09:17:21 +00:00
ret = NULL ;
2009-04-07 00:49:39 +00:00
break ;
default :
PyErr_Format ( PyExc_AttributeError , " RNA Error: unknown type \" %d \" (pyrna_param_to_py) " , type ) ;
ret = NULL ;
break ;
}
}
return ret ;
}
2009-04-09 13:20:48 +00:00
static PyObject * pyrna_func_call ( PyObject * self , PyObject * args , PyObject * kw )
{
PointerRNA * self_ptr = & ( ( ( BPy_StructRNA * ) PyTuple_GET_ITEM ( self , 0 ) ) - > ptr ) ;
FunctionRNA * self_func = PyCObject_AsVoidPtr ( PyTuple_GET_ITEM ( self , 1 ) ) ;
2009-04-07 00:49:39 +00:00
PointerRNA funcptr ;
ParameterList * parms ;
ParameterIterator iter ;
PropertyRNA * pret , * parm ;
PyObject * ret , * item ;
2009-04-11 01:45:05 +00:00
int i , tlen , flag , err = 0 ;
2009-04-07 00:49:39 +00:00
const char * tid , * fid , * pid ;
void * retdata = NULL ;
/* setup */
2009-04-09 13:20:48 +00:00
RNA_pointer_create ( NULL , & RNA_Function , self_func , & funcptr ) ;
2009-04-07 00:49:39 +00:00
2009-04-19 13:37:59 +00:00
pret = RNA_function_return ( self_func ) ;
2009-04-07 00:49:39 +00:00
tlen = PyTuple_GET_SIZE ( args ) ;
2009-04-09 13:20:48 +00:00
parms = RNA_parameter_list_create ( self_ptr , self_func ) ;
2009-04-07 00:49:39 +00:00
RNA_parameter_list_begin ( parms , & iter ) ;
/* parse function parameters */
2009-04-11 01:45:05 +00:00
for ( i = 0 ; iter . valid ; RNA_parameter_list_next ( & iter ) ) {
2009-04-07 00:49:39 +00:00
parm = iter . parm ;
if ( parm = = pret ) {
retdata = iter . data ;
continue ;
}
2009-04-19 13:37:59 +00:00
pid = RNA_property_identifier ( parm ) ;
flag = RNA_property_flag ( parm ) ;
2009-04-07 00:49:39 +00:00
item = NULL ;
2009-04-11 01:45:05 +00:00
if ( ( i < tlen ) & & ( flag & PROP_REQUIRED ) ) {
2009-04-07 00:49:39 +00:00
item = PyTuple_GET_ITEM ( args , i ) ;
2009-04-11 01:45:05 +00:00
i + + ;
}
else if ( kw ! = NULL )
2009-04-23 09:15:42 +00:00
item = PyDict_GetItemString ( kw , pid ) ; /* borrow ref */
2009-04-07 00:49:39 +00:00
if ( item = = NULL ) {
2009-04-11 01:45:05 +00:00
if ( flag & PROP_REQUIRED ) {
2009-04-19 13:37:59 +00:00
tid = RNA_struct_identifier ( self_ptr - > type ) ;
fid = RNA_function_identifier ( self_func ) ;
2009-04-07 00:49:39 +00:00
2009-04-11 01:45:05 +00:00
PyErr_Format ( PyExc_AttributeError , " %s.%s(): required parameter \" %s \" not specified " , tid , fid , pid ) ;
err = - 1 ;
break ;
}
else
continue ;
2009-04-07 00:49:39 +00:00
}
2009-04-16 13:21:18 +00:00
err = pyrna_py_to_prop ( & funcptr , parm , iter . data , item ) ;
2009-04-07 00:49:39 +00:00
if ( err ! = 0 )
break ;
}
ret = NULL ;
if ( err = = 0 ) {
/* call function */
2009-06-18 19:48:55 +00:00
ReportList reports ;
bContext * C = BPy_GetContext ( ) ;
BKE_reports_init ( & reports , RPT_STORE ) ;
RNA_function_call ( C , & reports , self_ptr , self_func , parms ) ;
err = ( BPy_reports_to_error ( & reports ) ) ? - 1 : 0 ;
BKE_reports_clear ( & reports ) ;
2009-04-07 00:49:39 +00:00
/* return value */
2009-06-18 19:48:55 +00:00
if ( err = = 0 )
if ( pret )
ret = pyrna_param_to_py ( & funcptr , pret , retdata ) ;
2009-04-07 00:49:39 +00:00
}
/* cleanup */
RNA_parameter_list_end ( & iter ) ;
RNA_parameter_list_free ( parms ) ;
if ( ret )
return ret ;
if ( err = = - 1 )
return NULL ;
Py_RETURN_NONE ;
}
2008-11-29 13:36:08 +00:00
/*-----------------------BPy_StructRNA method def------------------------------*/
PyTypeObject pyrna_struct_Type = {
# if (PY_VERSION_HEX >= 0x02060000)
2009-01-29 09:38:52 +00:00
PyVarObject_HEAD_INIT ( NULL , 0 )
2008-11-29 13:36:08 +00:00
# else
/* python 2.5 and below */
PyObject_HEAD_INIT ( NULL ) /* required py macro */
0 , /* ob_size */
# endif
" StructRNA " , /* tp_name */
sizeof ( BPy_StructRNA ) , /* tp_basicsize */
0 , /* tp_itemsize */
/* methods */
2008-12-29 03:24:13 +00:00
( destructor ) pyrna_struct_dealloc , /* tp_dealloc */
2008-11-29 13:36:08 +00:00
NULL , /* printfunc tp_print; */
2008-12-01 16:59:18 +00:00
NULL , /* getattrfunc tp_getattr; */
NULL , /* setattrfunc tp_setattr; */
2009-02-26 05:50:19 +00:00
NULL , /* tp_compare */ /* DEPRECATED in python 3.0! */
2008-11-29 13:36:08 +00:00
( reprfunc ) pyrna_struct_repr , /* tp_repr */
/* Method suites for standard classes */
NULL , /* PyNumberMethods *tp_as_number; */
NULL , /* PySequenceMethods *tp_as_sequence; */
NULL , /* PyMappingMethods *tp_as_mapping; */
/* More standard operations (here for binary compatibility) */
2008-12-02 14:36:35 +00:00
( hashfunc ) pyrna_struct_hash , /* hashfunc tp_hash; */
2008-12-01 16:59:18 +00:00
NULL , /* ternaryfunc tp_call; */
2008-11-29 13:36:08 +00:00
NULL , /* reprfunc tp_str; */
2008-12-01 16:59:18 +00:00
( getattrofunc ) pyrna_struct_getattro , /* getattrofunc tp_getattro; */
( setattrofunc ) pyrna_struct_setattro , /* setattrofunc tp_setattro; */
2008-11-29 13:36:08 +00:00
/* Functions to access object as input/output buffer */
NULL , /* PyBufferProcs *tp_as_buffer; */
/*** Flags to define presence of optional/expanded features ***/
2008-12-01 16:59:18 +00:00
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE , /* long tp_flags; */
2008-11-29 13:36:08 +00:00
NULL , /* char *tp_doc; Documentation string */
/*** Assigned meaning in release 2.0 ***/
/* call function for all accessible objects */
NULL , /* traverseproc tp_traverse; */
/* delete references to contained objects */
NULL , /* inquiry tp_clear; */
/*** Assigned meaning in release 2.1 ***/
/*** rich comparisons ***/
2009-01-29 09:38:52 +00:00
( richcmpfunc ) pyrna_struct_richcmp , /* richcmpfunc tp_richcompare; */
2008-11-29 13:36:08 +00:00
/*** weak reference enabler ***/
0 , /* long tp_weaklistoffset; */
/*** Added in release 2.2 ***/
/* Iterators */
NULL , /* getiterfunc tp_iter; */
NULL , /* iternextfunc tp_iternext; */
/*** Attribute descriptor and subclassing stuff ***/
2009-03-11 17:28:37 +00:00
pyrna_struct_methods , /* struct PyMethodDef *tp_methods; */
2008-11-29 13:36:08 +00:00
NULL , /* struct PyMemberDef *tp_members; */
NULL , /* struct PyGetSetDef *tp_getset; */
NULL , /* struct _typeobject *tp_base; */
NULL , /* PyObject *tp_dict; */
NULL , /* descrgetfunc tp_descr_get; */
NULL , /* descrsetfunc tp_descr_set; */
0 , /* long tp_dictoffset; */
NULL , /* initproc tp_init; */
NULL , /* allocfunc tp_alloc; */
2008-12-01 16:59:18 +00:00
pyrna_struct_new , /* newfunc tp_new; */
2008-11-29 13:36:08 +00:00
/* Low-level free-memory routine */
NULL , /* freefunc tp_free; */
/* For PyObject_IS_GC */
NULL , /* inquiry tp_is_gc; */
NULL , /* PyObject *tp_bases; */
/* method resolution order */
NULL , /* PyObject *tp_mro; */
NULL , /* PyObject *tp_cache; */
NULL , /* PyObject *tp_subclasses; */
NULL , /* PyObject *tp_weaklist; */
NULL
} ;
/*-----------------------BPy_PropertyRNA method def------------------------------*/
PyTypeObject pyrna_prop_Type = {
# if (PY_VERSION_HEX >= 0x02060000)
2009-01-29 09:38:52 +00:00
PyVarObject_HEAD_INIT ( NULL , 0 )
2008-11-29 13:36:08 +00:00
# else
/* python 2.5 and below */
PyObject_HEAD_INIT ( NULL ) /* required py macro */
0 , /* ob_size */
# endif
" PropertyRNA " , /* tp_name */
sizeof ( BPy_PropertyRNA ) , /* tp_basicsize */
0 , /* tp_itemsize */
/* methods */
NULL , /* tp_dealloc */
NULL , /* printfunc tp_print; */
2008-11-30 14:00:14 +00:00
NULL , /* getattrfunc tp_getattr; */
2008-11-29 13:36:08 +00:00
NULL , /* setattrfunc tp_setattr; */
2009-02-26 05:50:19 +00:00
NULL , /* tp_compare */ /* DEPRECATED in python 3.0! */
2008-11-29 13:36:08 +00:00
( reprfunc ) pyrna_prop_repr , /* tp_repr */
/* Method suites for standard classes */
NULL , /* PyNumberMethods *tp_as_number; */
2009-06-13 08:04:43 +00:00
& pyrna_prop_as_sequence , /* PySequenceMethods *tp_as_sequence; */
2008-11-29 13:36:08 +00:00
& pyrna_prop_as_mapping , /* PyMappingMethods *tp_as_mapping; */
/* More standard operations (here for binary compatibility) */
NULL , /* hashfunc tp_hash; */
NULL , /* ternaryfunc tp_call; */
NULL , /* reprfunc tp_str; */
2008-12-02 09:35:29 +00:00
NULL , /*PyObject_GenericGetAttr - MINGW Complains, assign later */ /* getattrofunc tp_getattro; */ /* will only use these if this is a subtype of a py class */
NULL , /*PyObject_GenericSetAttr - MINGW Complains, assign later */ /* setattrofunc tp_setattro; */
2008-11-29 13:36:08 +00:00
/* Functions to access object as input/output buffer */
NULL , /* PyBufferProcs *tp_as_buffer; */
/*** Flags to define presence of optional/expanded features ***/
2008-12-01 16:59:18 +00:00
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE , /* long tp_flags; */
2008-11-29 13:36:08 +00:00
NULL , /* char *tp_doc; Documentation string */
/*** Assigned meaning in release 2.0 ***/
/* call function for all accessible objects */
NULL , /* traverseproc tp_traverse; */
/* delete references to contained objects */
NULL , /* inquiry tp_clear; */
/*** Assigned meaning in release 2.1 ***/
/*** rich comparisons ***/
2009-01-29 09:38:52 +00:00
( richcmpfunc ) pyrna_prop_richcmp , /* richcmpfunc tp_richcompare; */
2008-11-29 13:36:08 +00:00
/*** weak reference enabler ***/
0 , /* long tp_weaklistoffset; */
/*** Added in release 2.2 ***/
/* Iterators */
2008-12-02 15:27:10 +00:00
( getiterfunc ) pyrna_prop_iter , /* getiterfunc tp_iter; */
2008-11-29 13:36:08 +00:00
NULL , /* iternextfunc tp_iternext; */
/*** Attribute descriptor and subclassing stuff ***/
pyrna_prop_methods , /* struct PyMethodDef *tp_methods; */
NULL , /* struct PyMemberDef *tp_members; */
NULL , /* struct PyGetSetDef *tp_getset; */
NULL , /* struct _typeobject *tp_base; */
NULL , /* PyObject *tp_dict; */
NULL , /* descrgetfunc tp_descr_get; */
NULL , /* descrsetfunc tp_descr_set; */
0 , /* long tp_dictoffset; */
NULL , /* initproc tp_init; */
NULL , /* allocfunc tp_alloc; */
2008-12-01 16:59:18 +00:00
pyrna_prop_new , /* newfunc tp_new; */
2008-11-29 13:36:08 +00:00
/* Low-level free-memory routine */
NULL , /* freefunc tp_free; */
/* For PyObject_IS_GC */
NULL , /* inquiry tp_is_gc; */
NULL , /* PyObject *tp_bases; */
/* method resolution order */
NULL , /* PyObject *tp_mro; */
NULL , /* PyObject *tp_cache; */
NULL , /* PyObject *tp_subclasses; */
NULL , /* PyObject *tp_weaklist; */
NULL
} ;
2009-04-19 13:37:59 +00:00
static void pyrna_subtype_set_rna ( PyObject * newclass , StructRNA * srna )
{
PointerRNA ptr ;
PyObject * item ;
2009-05-26 06:29:15 +00:00
Py_INCREF ( newclass ) ;
2009-05-28 02:03:48 +00:00
2009-05-26 06:29:15 +00:00
if ( RNA_struct_py_type_get ( srna ) )
2009-05-28 02:03:48 +00:00
PyObSpit ( " RNA WAS SET - " , RNA_struct_py_type_get ( srna ) ) ;
2009-05-28 10:31:56 +00:00
Py_XDECREF ( ( ( PyObject * ) RNA_struct_py_type_get ( srna ) ) ) ;
2009-05-26 06:29:15 +00:00
2009-04-19 13:37:59 +00:00
RNA_struct_py_type_set ( srna , ( void * ) newclass ) ; /* Store for later use */
/* Not 100% needed but useful,
* having an instance within a type looks wrong however this instance IS an rna type */
RNA_pointer_create ( NULL , & RNA_Struct , srna , & ptr ) ;
item = pyrna_struct_CreatePyObject ( & ptr ) ;
PyDict_SetItemString ( ( ( PyTypeObject * ) newclass ) - > tp_dict , " __rna__ " , item ) ;
Py_DECREF ( item ) ;
/* done with rna instance */
}
2009-04-07 00:49:39 +00:00
2009-03-13 07:50:07 +00:00
PyObject * pyrna_struct_Subtype ( PointerRNA * ptr )
{
PyObject * newclass = NULL ;
PropertyRNA * nameprop ;
if ( ptr - > type = = NULL ) {
newclass = NULL ; /* Nothing to do */
2009-04-07 00:49:39 +00:00
} else if ( ( newclass = RNA_struct_py_type_get ( ptr - > data ) ) ) {
2009-03-21 06:55:30 +00:00
Py_INCREF ( newclass ) ;
2009-04-19 13:37:59 +00:00
} else if ( ( nameprop = RNA_struct_name_property ( ptr - > type ) ) ) {
2009-03-13 07:50:07 +00:00
/* for now, return the base RNA type rather then a real module */
2009-04-07 00:49:39 +00:00
/* Assume RNA_struct_py_type_get(ptr->data) was alredy checked */
2009-03-13 07:50:07 +00:00
/* subclass equivelents
- class myClass ( myBase ) :
some = ' value ' # or . . .
- myClass = type ( name = ' myClass ' , bases = ( myBase , ) , dict = { ' some ' : ' value ' } )
*/
char name [ 256 ] , * nameptr ;
2009-04-19 13:37:59 +00:00
const char * descr = RNA_struct_ui_description ( ptr - > type ) ;
2009-03-13 07:50:07 +00:00
PyObject * args = PyTuple_New ( 3 ) ;
PyObject * bases = PyTuple_New ( 1 ) ;
PyObject * dict = PyDict_New ( ) ;
2009-04-11 05:46:40 +00:00
PyObject * item ;
2009-03-13 07:50:07 +00:00
nameptr = RNA_property_string_get_alloc ( ptr , nameprop , name , sizeof ( name ) ) ;
// arg 1
//PyTuple_SET_ITEM(args, 0, PyUnicode_FromString(tp_name));
PyTuple_SET_ITEM ( args , 0 , PyUnicode_FromString ( nameptr ) ) ;
// arg 2
PyTuple_SET_ITEM ( bases , 0 , ( PyObject * ) & pyrna_struct_Type ) ;
Py_INCREF ( & pyrna_struct_Type ) ;
2009-03-21 06:55:30 +00:00
2009-03-13 07:50:07 +00:00
PyTuple_SET_ITEM ( args , 1 , bases ) ;
// arg 3 - add an instance of the rna
2009-04-11 05:46:40 +00:00
if ( descr ) {
item = PyUnicode_FromString ( descr ) ;
PyDict_SetItemString ( dict , " __doc__ " , item ) ;
Py_DECREF ( item ) ;
}
2009-03-13 07:50:07 +00:00
PyTuple_SET_ITEM ( args , 2 , dict ) ; // fill with useful subclass things!
if ( PyErr_Occurred ( ) ) {
PyErr_Print ( ) ;
PyErr_Clear ( ) ;
}
newclass = PyObject_CallObject ( ( PyObject * ) & PyType_Type , args ) ;
2009-04-11 05:46:40 +00:00
Py_DECREF ( args ) ;
2009-03-21 06:55:30 +00:00
2009-04-19 13:37:59 +00:00
if ( newclass )
pyrna_subtype_set_rna ( newclass , ptr - > data ) ;
2009-03-21 06:55:30 +00:00
2009-04-07 00:49:39 +00:00
if ( name ! = nameptr )
2009-03-13 07:50:07 +00:00
MEM_freeN ( nameptr ) ;
}
return newclass ;
}
2008-11-29 13:36:08 +00:00
/*-----------------------CreatePyObject---------------------------------*/
PyObject * pyrna_struct_CreatePyObject ( PointerRNA * ptr )
{
2009-03-21 06:55:30 +00:00
BPy_StructRNA * pyrna = NULL ;
2009-03-05 16:24:30 +00:00
2009-03-14 13:43:30 +00:00
if ( ptr - > data = = NULL & & ptr - > type = = NULL ) { /* Operator RNA has NULL data */
2009-03-05 16:24:30 +00:00
Py_RETURN_NONE ;
}
2009-03-11 17:28:37 +00:00
2009-03-21 06:55:30 +00:00
if ( ptr - > type = = & RNA_Struct ) { /* always return a python subtype from rna struct types */
2009-03-21 16:03:26 +00:00
PyTypeObject * tp = ( PyTypeObject * ) pyrna_struct_Subtype ( ptr ) ;
2009-03-21 06:55:30 +00:00
if ( tp ) {
pyrna = ( BPy_StructRNA * ) tp - > tp_alloc ( tp , 0 ) ;
}
else {
fprintf ( stderr , " Could not make type \n " ) ;
pyrna = ( BPy_StructRNA * ) PyObject_NEW ( BPy_StructRNA , & pyrna_struct_Type ) ;
}
2009-03-11 17:28:37 +00:00
}
else {
pyrna = ( BPy_StructRNA * ) PyObject_NEW ( BPy_StructRNA , & pyrna_struct_Type ) ;
}
2008-11-29 13:36:08 +00:00
if ( ! pyrna ) {
PyErr_SetString ( PyExc_MemoryError , " couldn't create BPy_StructRNA object " ) ;
return NULL ;
}
2008-12-29 03:24:13 +00:00
pyrna - > ptr = * ptr ;
2009-01-08 15:29:09 +00:00
pyrna - > freeptr = 0 ;
2008-11-29 13:36:08 +00:00
return ( PyObject * ) pyrna ;
}
PyObject * pyrna_prop_CreatePyObject ( PointerRNA * ptr , PropertyRNA * prop )
{
BPy_PropertyRNA * pyrna ;
pyrna = ( BPy_PropertyRNA * ) PyObject_NEW ( BPy_PropertyRNA , & pyrna_prop_Type ) ;
if ( ! pyrna ) {
PyErr_SetString ( PyExc_MemoryError , " couldn't create BPy_rna object " ) ;
return NULL ;
}
pyrna - > ptr = * ptr ;
pyrna - > prop = prop ;
return ( PyObject * ) pyrna ;
}
PyObject * BPY_rna_module ( void )
{
PointerRNA ptr ;
2009-06-22 04:26:48 +00:00
# ifdef USE_MATHUTILS // register mathutils callbacks, ok to run more then once.
mathutils_rna_vector_cb_index = Mathutils_RegisterCallback ( & mathutils_rna_vector_cb ) ;
# endif
2009-03-13 07:50:07 +00:00
/* This can't be set in the pytype struct because some compilers complain */
pyrna_prop_Type . tp_getattro = PyObject_GenericGetAttr ;
pyrna_prop_Type . tp_setattro = PyObject_GenericSetAttr ;
if ( PyType_Ready ( & pyrna_struct_Type ) < 0 )
return NULL ;
if ( PyType_Ready ( & pyrna_prop_Type ) < 0 )
return NULL ;
2008-11-29 13:36:08 +00:00
/* for now, return the base RNA type rather then a real module */
RNA_main_pointer_create ( G . main , & ptr ) ;
return pyrna_struct_CreatePyObject ( & ptr ) ;
}
2008-12-16 16:32:48 +00:00
2009-03-13 07:50:07 +00:00
#if 0
2008-12-16 16:32:48 +00:00
/* This is a way we can access docstrings for RNA types
* without having the datatypes in blender */
PyObject * BPY_rna_doc ( void )
{
PointerRNA ptr ;
/* for now, return the base RNA type rather then a real module */
RNA_blender_rna_pointer_create ( & ptr ) ;
return pyrna_struct_CreatePyObject ( & ptr ) ;
}
2009-03-13 07:50:07 +00:00
# endif
2008-12-16 16:32:48 +00:00
2009-03-11 17:28:37 +00:00
2009-03-21 06:55:30 +00:00
/* pyrna_basetype_* - BPy_BaseTypeRNA is just a BPy_PropertyRNA struct with a differnt type
* the self - > ptr and self - > prop are always set to the " structs " collection */
//---------------getattr--------------------------------------------
static PyObject * pyrna_basetype_getattro ( BPy_BaseTypeRNA * self , PyObject * pyname )
{
PointerRNA newptr ;
PyObject * ret ;
2009-03-11 17:28:37 +00:00
2009-03-21 06:55:30 +00:00
ret = PyObject_GenericGetAttr ( ( PyObject * ) self , pyname ) ;
if ( ret ) return ret ;
else PyErr_Clear ( ) ;
2009-03-11 17:28:37 +00:00
2009-03-21 06:55:30 +00:00
if ( RNA_property_collection_lookup_string ( & self - > ptr , self - > prop , _PyUnicode_AsString ( pyname ) , & newptr ) ) {
2009-04-11 15:05:42 +00:00
ret = pyrna_struct_Subtype ( & newptr ) ;
if ( ret = = NULL ) {
PyErr_Format ( PyExc_SystemError , " bpy.types.%s subtype could not be generated, this is a bug! " , _PyUnicode_AsString ( pyname ) ) ;
}
return ret ;
2009-03-11 17:28:37 +00:00
}
2009-03-21 06:55:30 +00:00
else { /* Override the error */
PyErr_Format ( PyExc_AttributeError , " bpy.types.%s not a valid RNA_Struct " , _PyUnicode_AsString ( pyname ) ) ;
return NULL ;
}
}
static PyObject * pyrna_basetype_dir ( BPy_BaseTypeRNA * self ) ;
static struct PyMethodDef pyrna_basetype_methods [ ] = {
{ " __dir__ " , ( PyCFunction ) pyrna_basetype_dir , METH_NOARGS , " " } ,
2009-04-19 13:37:59 +00:00
{ " register " , ( PyCFunction ) pyrna_basetype_register , METH_VARARGS , " " } ,
{ " unregister " , ( PyCFunction ) pyrna_basetype_unregister , METH_VARARGS , " " } ,
2009-03-21 06:55:30 +00:00
{ NULL , NULL , 0 , NULL }
} ;
static PyObject * pyrna_basetype_dir ( BPy_BaseTypeRNA * self )
{
PyObject * list , * name ;
PyMethodDef * meth ;
list = pyrna_prop_keys ( self ) ; /* like calling structs.keys(), avoids looping here */
for ( meth = pyrna_basetype_methods ; meth - > ml_name ; meth + + ) {
name = PyUnicode_FromString ( meth - > ml_name ) ;
PyList_Append ( list , name ) ;
Py_DECREF ( name ) ;
}
return list ;
}
2009-04-11 16:17:39 +00:00
PyTypeObject pyrna_basetype_Type = BLANK_PYTHON_TYPE ;
2009-03-21 06:55:30 +00:00
PyObject * BPY_rna_types ( void )
{
BPy_BaseTypeRNA * self ;
2009-04-07 15:20:12 +00:00
2009-04-11 16:17:39 +00:00
if ( ( pyrna_basetype_Type . tp_flags & Py_TPFLAGS_READY ) = = 0 ) {
pyrna_basetype_Type . tp_name = " RNA_Types " ;
pyrna_basetype_Type . tp_basicsize = sizeof ( BPy_BaseTypeRNA ) ;
pyrna_basetype_Type . tp_getattro = ( getattrofunc ) pyrna_basetype_getattro ;
pyrna_basetype_Type . tp_flags = Py_TPFLAGS_DEFAULT ;
pyrna_basetype_Type . tp_methods = pyrna_basetype_methods ;
if ( PyType_Ready ( & pyrna_basetype_Type ) < 0 )
return NULL ;
}
2009-03-13 07:50:07 +00:00
2009-03-21 06:55:30 +00:00
self = ( BPy_BaseTypeRNA * ) PyObject_NEW ( BPy_BaseTypeRNA , & pyrna_basetype_Type ) ;
/* avoid doing this lookup for every getattr */
RNA_blender_rna_pointer_create ( & self - > ptr ) ;
self - > prop = RNA_struct_find_property ( & self - > ptr , " structs " ) ;
return ( PyObject * ) self ;
2009-03-11 17:28:37 +00:00
}
2009-03-16 15:54:43 +00:00
2009-06-18 19:51:22 +00:00
static struct PyMethodDef props_methods [ ] = {
{ " FloatProperty " , ( PyCFunction ) BPy_FloatProperty , METH_VARARGS | METH_KEYWORDS , " " } ,
{ " IntProperty " , ( PyCFunction ) BPy_IntProperty , METH_VARARGS | METH_KEYWORDS , " " } ,
{ " BoolProperty " , ( PyCFunction ) BPy_BoolProperty , METH_VARARGS | METH_KEYWORDS , " " } ,
{ " StringProperty " , ( PyCFunction ) BPy_StringProperty , METH_VARARGS | METH_KEYWORDS , " " } ,
{ NULL , NULL , 0 , NULL }
} ;
2009-03-16 15:54:43 +00:00
2009-06-18 19:51:22 +00:00
# if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef props_module = {
PyModuleDef_HEAD_INIT ,
" bpyprops " ,
" " ,
- 1 , /* multiple "initialization" just copies the module dict. */
props_methods ,
NULL , NULL , NULL , NULL
} ;
# endif
PyObject * BPY_rna_props ( void )
{
PyObject * submodule , * mod ;
# if PY_VERSION_HEX >= 0x03000000
submodule = PyModule_Create ( & props_module ) ;
# else /* Py2.x */
submodule = Py_InitModule3 ( " bpy.props " , props_methods , " " ) ;
# endif
mod = PyModule_New ( " props " ) ;
PyModule_AddObject ( submodule , " props " , mod ) ;
/* INCREF since its its assumed that all these functions return the
* module with a new ref like PyDict_New , since they are passed to
* PyModule_AddObject which steals a ref */
Py_INCREF ( submodule ) ;
return submodule ;
}
2009-03-21 06:55:30 +00:00
2009-03-16 15:54:43 +00:00
/* Orphan functions, not sure where they should go */
/* Function that sets RNA, NOTE - self is NULL when called from python, but being abused from C so we can pass the srna allong
* This isnt incorrect since its a python object - but be careful */
PyObject * BPy_FloatProperty ( PyObject * self , PyObject * args , PyObject * kw )
{
2009-03-18 22:22:58 +00:00
static char * kwlist [ ] = { " attr " , " name " , " description " , " min " , " max " , " soft_min " , " soft_max " , " default " , NULL } ;
2009-03-16 15:54:43 +00:00
char * id , * name = " " , * description = " " ;
float min = FLT_MIN , max = FLT_MAX , soft_min = FLT_MIN , soft_max = FLT_MAX , def = 0.0f ;
if ( ! PyArg_ParseTupleAndKeywords ( args , kw , " s|ssfffff:FloatProperty " , kwlist , & id , & name , & description , & min , & max , & soft_min , & soft_max , & def ) )
return NULL ;
if ( PyTuple_Size ( args ) > 0 ) {
PyErr_SetString ( PyExc_ValueError , " all args must be keywors " ) ; // TODO - py3 can enforce this.
return NULL ;
}
2009-06-18 19:51:22 +00:00
if ( self & & PyCObject_Check ( self ) ) {
2009-03-16 15:54:43 +00:00
StructRNA * srna = PyCObject_AsVoidPtr ( self ) ;
RNA_def_float ( srna , id , def , min , max , name , description , soft_min , soft_max ) ;
Py_RETURN_NONE ;
} else {
PyObject * ret = PyTuple_New ( 2 ) ;
PyTuple_SET_ITEM ( ret , 0 , PyCObject_FromVoidPtr ( ( void * ) BPy_FloatProperty , NULL ) ) ;
PyTuple_SET_ITEM ( ret , 1 , kw ) ;
Py_INCREF ( kw ) ;
return ret ;
}
}
PyObject * BPy_IntProperty ( PyObject * self , PyObject * args , PyObject * kw )
{
2009-03-18 22:22:58 +00:00
static char * kwlist [ ] = { " attr " , " name " , " description " , " min " , " max " , " soft_min " , " soft_max " , " default " , NULL } ;
2009-03-16 15:54:43 +00:00
char * id , * name = " " , * description = " " ;
int min = INT_MIN , max = INT_MAX , soft_min = INT_MIN , soft_max = INT_MAX , def = 0 ;
if ( ! PyArg_ParseTupleAndKeywords ( args , kw , " s|ssiiiii:IntProperty " , kwlist , & id , & name , & description , & min , & max , & soft_min , & soft_max , & def ) )
return NULL ;
if ( PyTuple_Size ( args ) > 0 ) {
PyErr_SetString ( PyExc_ValueError , " all args must be keywors " ) ; // TODO - py3 can enforce this.
return NULL ;
}
2009-06-18 19:51:22 +00:00
if ( self & & PyCObject_Check ( self ) ) {
2009-03-16 15:54:43 +00:00
StructRNA * srna = PyCObject_AsVoidPtr ( self ) ;
RNA_def_int ( srna , id , def , min , max , name , description , soft_min , soft_max ) ;
Py_RETURN_NONE ;
} else {
PyObject * ret = PyTuple_New ( 2 ) ;
PyTuple_SET_ITEM ( ret , 0 , PyCObject_FromVoidPtr ( ( void * ) BPy_IntProperty , NULL ) ) ;
PyTuple_SET_ITEM ( ret , 1 , kw ) ;
Py_INCREF ( kw ) ;
return ret ;
}
}
PyObject * BPy_BoolProperty ( PyObject * self , PyObject * args , PyObject * kw )
{
2009-03-18 22:22:58 +00:00
static char * kwlist [ ] = { " attr " , " name " , " description " , " default " , NULL } ;
2009-03-16 15:54:43 +00:00
char * id , * name = " " , * description = " " ;
int def = 0 ;
if ( ! PyArg_ParseTupleAndKeywords ( args , kw , " s|ssi:IntProperty " , kwlist , & id , & name , & description , & def ) )
return NULL ;
if ( PyTuple_Size ( args ) > 0 ) {
PyErr_SetString ( PyExc_ValueError , " all args must be keywors " ) ; // TODO - py3 can enforce this.
return NULL ;
}
2009-06-18 19:51:22 +00:00
if ( self & & PyCObject_Check ( self ) ) {
2009-03-16 15:54:43 +00:00
StructRNA * srna = PyCObject_AsVoidPtr ( self ) ;
RNA_def_boolean ( srna , id , def , name , description ) ;
Py_RETURN_NONE ;
} else {
PyObject * ret = PyTuple_New ( 2 ) ;
PyTuple_SET_ITEM ( ret , 0 , PyCObject_FromVoidPtr ( ( void * ) BPy_IntProperty , NULL ) ) ;
PyTuple_SET_ITEM ( ret , 1 , kw ) ;
Py_INCREF ( kw ) ;
return ret ;
}
}
2009-04-19 13:37:59 +00:00
2009-06-18 19:51:22 +00:00
PyObject * BPy_StringProperty ( PyObject * self , PyObject * args , PyObject * kw )
{
static char * kwlist [ ] = { " attr " , " name " , " description " , " maxlen " , " default " , NULL } ;
char * id , * name = " " , * description = " " , * def = " " ;
int maxlen = 0 ;
if ( ! PyArg_ParseTupleAndKeywords ( args , kw , " s|ssis:StringProperty " , kwlist , & id , & name , & description , & maxlen , & def ) )
return NULL ;
if ( PyTuple_Size ( args ) > 0 ) {
PyErr_SetString ( PyExc_ValueError , " all args must be keywors " ) ; // TODO - py3 can enforce this.
return NULL ;
}
if ( self & & PyCObject_Check ( self ) ) {
StructRNA * srna = PyCObject_AsVoidPtr ( self ) ;
RNA_def_string ( srna , id , def , maxlen , name , description ) ;
Py_RETURN_NONE ;
} else {
PyObject * ret = PyTuple_New ( 2 ) ;
PyTuple_SET_ITEM ( ret , 0 , PyCObject_FromVoidPtr ( ( void * ) BPy_StringProperty , NULL ) ) ;
PyTuple_SET_ITEM ( ret , 1 , kw ) ;
Py_INCREF ( kw ) ;
return ret ;
}
}
2009-04-19 13:37:59 +00:00
/*-------------------- Type Registration ------------------------*/
static int rna_function_arg_count ( FunctionRNA * func )
{
const ListBase * lb = RNA_function_defined_parameters ( func ) ;
PropertyRNA * parm ;
Link * link ;
int count = 1 ;
for ( link = lb - > first ; link ; link = link - > next ) {
parm = ( PropertyRNA * ) link ;
if ( ! ( RNA_property_flag ( parm ) & PROP_RETURN ) )
count + + ;
}
return count ;
}
static int bpy_class_validate ( PointerRNA * dummyptr , void * py_data , int * have_function )
{
const ListBase * lb ;
Link * link ;
FunctionRNA * func ;
PropertyRNA * prop ;
StructRNA * srna = dummyptr - > type ;
const char * class_type = RNA_struct_identifier ( srna ) ;
PyObject * py_class = ( PyObject * ) py_data ;
PyObject * base_class = RNA_struct_py_type_get ( srna ) ;
PyObject * item , * fitem ;
PyObject * py_arg_count ;
int i , flag , arg_count , func_arg_count ;
char identifier [ 128 ] ;
if ( base_class ) {
if ( ! PyObject_IsSubclass ( py_class , base_class ) ) {
PyObject * name = PyObject_GetAttrString ( base_class , " __name__ " ) ;
PyErr_Format ( PyExc_AttributeError , " expected %s subclass of class \" %s \" " , class_type , name ? _PyUnicode_AsString ( name ) : " <UNKNOWN> " ) ;
Py_XDECREF ( name ) ;
return - 1 ;
}
}
/* verify callback functions */
lb = RNA_struct_defined_functions ( srna ) ;
i = 0 ;
for ( link = lb - > first ; link ; link = link - > next ) {
func = ( FunctionRNA * ) link ;
flag = RNA_function_flag ( func ) ;
if ( ! ( flag & FUNC_REGISTER ) )
continue ;
item = PyObject_GetAttrString ( py_class , RNA_function_identifier ( func ) ) ;
have_function [ i ] = ( item ! = NULL ) ;
i + + ;
if ( item = = NULL ) {
if ( ( flag & FUNC_REGISTER_OPTIONAL ) = = 0 ) {
PyErr_Format ( PyExc_AttributeError , " expected %s class to have an \" %s \" attribute " , class_type , RNA_function_identifier ( func ) ) ;
return - 1 ;
}
PyErr_Clear ( ) ;
}
else {
Py_DECREF ( item ) ; /* no need to keep a ref, the class owns it */
if ( PyMethod_Check ( item ) )
fitem = PyMethod_Function ( item ) ; /* py 2.x */
else
fitem = item ; /* py 3.x */
if ( PyFunction_Check ( fitem ) = = 0 ) {
PyErr_Format ( PyExc_AttributeError , " expected %s class \" %s \" attribute to be a function " , class_type , RNA_function_identifier ( func ) ) ;
return - 1 ;
}
func_arg_count = rna_function_arg_count ( func ) ;
if ( func_arg_count > = 0 ) { /* -1 if we dont care*/
py_arg_count = PyObject_GetAttrString ( PyFunction_GET_CODE ( fitem ) , " co_argcount " ) ;
arg_count = PyLong_AsSsize_t ( py_arg_count ) ;
Py_DECREF ( py_arg_count ) ;
if ( arg_count ! = func_arg_count ) {
PyErr_Format ( PyExc_AttributeError , " expected %s class \" %s \" function to have %d args " , class_type , RNA_function_identifier ( func ) , func_arg_count ) ;
return - 1 ;
}
}
}
}
/* verify properties */
lb = RNA_struct_defined_properties ( srna ) ;
for ( link = lb - > first ; link ; link = link - > next ) {
prop = ( PropertyRNA * ) link ;
flag = RNA_property_flag ( prop ) ;
if ( ! ( flag & PROP_REGISTER ) )
continue ;
BLI_snprintf ( identifier , sizeof ( identifier ) , " __%s__ " , RNA_property_identifier ( prop ) ) ;
item = PyObject_GetAttrString ( py_class , identifier ) ;
if ( item = = NULL ) {
if ( strcmp ( identifier , " __idname__ " ) = = 0 ) {
item = PyObject_GetAttrString ( py_class , " __name__ " ) ;
if ( item ) {
Py_DECREF ( item ) ; /* no need to keep a ref, the class owns it */
if ( pyrna_py_to_prop ( dummyptr , prop , NULL , item ) ! = 0 )
return - 1 ;
}
}
if ( item = = NULL & & ( flag & PROP_REGISTER_OPTIONAL ) = = 0 ) {
PyErr_Format ( PyExc_AttributeError , " expected %s class to have an \" %s \" attribute " , class_type , identifier ) ;
return - 1 ;
}
PyErr_Clear ( ) ;
}
else {
Py_DECREF ( item ) ; /* no need to keep a ref, the class owns it */
if ( pyrna_py_to_prop ( dummyptr , prop , NULL , item ) ! = 0 )
return - 1 ;
}
}
return 0 ;
}
extern void BPY_update_modules ( void ) ; //XXX temp solution
static int bpy_class_call ( PointerRNA * ptr , FunctionRNA * func , ParameterList * parms )
{
PyObject * args ;
PyObject * ret = NULL , * py_class , * py_class_instance , * item , * parmitem ;
PropertyRNA * pret = NULL , * parm ;
ParameterIterator iter ;
PointerRNA funcptr ;
void * retdata = NULL ;
int err = 0 , i , flag ;
PyGILState_STATE gilstate = PyGILState_Ensure ( ) ;
BPY_update_modules ( ) ; // XXX - the RNA pointers can change so update before running, would like a nicer solution for this.
py_class = RNA_struct_py_type_get ( ptr - > type ) ;
2009-05-20 05:35:53 +00:00
item = pyrna_struct_CreatePyObject ( ptr ) ;
if ( item = = NULL ) {
py_class_instance = NULL ;
}
else if ( item = = Py_None ) { /* probably wont ever happen but possible */
Py_DECREF ( item ) ;
py_class_instance = NULL ;
}
else {
args = PyTuple_New ( 1 ) ;
PyTuple_SET_ITEM ( args , 0 , item ) ;
py_class_instance = PyObject_Call ( py_class , args , NULL ) ;
Py_DECREF ( args ) ;
}
2009-04-19 13:37:59 +00:00
if ( py_class_instance ) { /* Initializing the class worked, now run its invoke function */
item = PyObject_GetAttrString ( py_class , RNA_function_identifier ( func ) ) ;
flag = RNA_function_flag ( func ) ;
if ( item ) {
pret = RNA_function_return ( func ) ;
RNA_pointer_create ( NULL , & RNA_Function , func , & funcptr ) ;
args = PyTuple_New ( rna_function_arg_count ( func ) ) ;
PyTuple_SET_ITEM ( args , 0 , py_class_instance ) ;
RNA_parameter_list_begin ( parms , & iter ) ;
/* parse function parameters */
for ( i = 1 ; iter . valid ; RNA_parameter_list_next ( & iter ) ) {
parm = iter . parm ;
if ( parm = = pret ) {
retdata = iter . data ;
continue ;
}
parmitem = pyrna_param_to_py ( & funcptr , parm , iter . data ) ;
PyTuple_SET_ITEM ( args , i , parmitem ) ;
i + + ;
}
ret = PyObject_Call ( item , args , NULL ) ;
Py_DECREF ( item ) ;
2009-05-20 05:35:53 +00:00
Py_DECREF ( args ) ;
2009-04-19 13:37:59 +00:00
}
else {
Py_DECREF ( py_class_instance ) ;
PyErr_Format ( PyExc_AttributeError , " could not find function %s in %s to execute callback. " , RNA_function_identifier ( func ) , RNA_struct_identifier ( ptr - > type ) ) ;
err = - 1 ;
}
}
else {
PyErr_Format ( PyExc_AttributeError , " could not create instance of %s to call callback function %s. " , RNA_struct_identifier ( ptr - > type ) , RNA_function_identifier ( func ) ) ;
err = - 1 ;
}
if ( ret = = NULL ) { /* covers py_class_instance failing too */
PyErr_Print ( ) ; /* XXX use reporting api? */
err = - 1 ;
}
else {
if ( retdata )
err = pyrna_py_to_prop ( & funcptr , pret , retdata , ret ) ;
Py_DECREF ( ret ) ;
}
PyGILState_Release ( gilstate ) ;
return err ;
}
static void bpy_class_free ( void * pyob_ptr )
{
2009-05-28 02:03:48 +00:00
if ( G . f & G_DEBUG ) {
if ( ( ( PyObject * ) pyob_ptr ) - > ob_refcnt > 1 )
PyObSpit ( " zombie class - ref should be 1 " , ( PyObject * ) pyob_ptr ) ;
}
2009-04-19 13:37:59 +00:00
Py_DECREF ( ( PyObject * ) pyob_ptr ) ;
}
PyObject * pyrna_basetype_register ( PyObject * self , PyObject * args )
{
bContext * C = NULL ;
PyObject * py_class , * item ;
ReportList reports ;
StructRegisterFunc reg ;
BPy_StructRNA * py_srna ;
StructRNA * srna ;
if ( ! PyArg_ParseTuple ( args , " O:register " , & py_class ) )
return NULL ;
if ( ! PyType_Check ( py_class ) ) {
PyErr_SetString ( PyExc_AttributeError , " expected a Type subclassed from a registerable rna type (no a Type object). " ) ;
return NULL ;
}
/* check we got an __rna__ attribute */
item = PyObject_GetAttrString ( py_class , " __rna__ " ) ;
if ( ! item | | ! BPy_StructRNA_Check ( item ) ) {
2009-06-07 14:53:08 +00:00
Py_XDECREF ( item ) ;
2009-04-19 13:37:59 +00:00
PyErr_SetString ( PyExc_AttributeError , " expected a Type subclassed from a registerable rna type (no __rna__ property). " ) ;
return NULL ;
}
/* check the __rna__ attribute has the right type */
Py_DECREF ( item ) ;
py_srna = ( BPy_StructRNA * ) item ;
if ( py_srna - > ptr . type ! = & RNA_Struct ) {
PyErr_SetString ( PyExc_AttributeError , " expected a Type subclassed from a registerable rna type (not a Struct). " ) ;
return NULL ;
}
/* check that we have a register callback for this type */
reg = RNA_struct_register ( py_srna - > ptr . data ) ;
if ( ! reg ) {
PyErr_SetString ( PyExc_AttributeError , " expected a Type subclassed from a registerable rna type (no register supported). " ) ;
return NULL ;
}
/* get the context, so register callback can do necessary refreshes */
2009-05-25 13:48:44 +00:00
C = BPy_GetContext ( ) ;
2009-04-19 13:37:59 +00:00
/* call the register callback */
2009-06-18 19:48:55 +00:00
BKE_reports_init ( & reports , RPT_STORE ) ;
2009-04-19 13:37:59 +00:00
srna = reg ( C , & reports , py_class , bpy_class_validate , bpy_class_call , bpy_class_free ) ;
if ( ! srna ) {
BPy_reports_to_error ( & reports ) ;
BKE_reports_clear ( & reports ) ;
return NULL ;
}
BKE_reports_clear ( & reports ) ;
2009-05-26 06:29:15 +00:00
pyrna_subtype_set_rna ( py_class , srna ) ; /* takes a ref to py_class */
2009-04-19 13:37:59 +00:00
Py_RETURN_NONE ;
}
PyObject * pyrna_basetype_unregister ( PyObject * self , PyObject * args )
{
bContext * C = NULL ;
PyObject * py_class , * item ;
BPy_StructRNA * py_srna ;
StructUnregisterFunc unreg ;
2009-04-23 05:56:09 +00:00
if ( ! PyArg_ParseTuple ( args , " O:unregister " , & py_class ) )
2009-04-19 13:37:59 +00:00
return NULL ;
if ( ! PyType_Check ( py_class ) ) {
PyErr_SetString ( PyExc_AttributeError , " expected a Type subclassed from a registerable rna type (no a Type object). " ) ;
return NULL ;
}
/* check we got an __rna__ attribute */
2009-04-23 09:15:42 +00:00
item = PyDict_GetItemString ( ( ( PyTypeObject * ) py_class ) - > tp_dict , " __rna__ " ) ; /* borrow ref */
2009-04-19 13:37:59 +00:00
if ( ! item | | ! BPy_StructRNA_Check ( item ) ) {
PyErr_SetString ( PyExc_AttributeError , " expected a Type subclassed from a registerable rna type (no __rna__ property). " ) ;
return NULL ;
}
/* check the __rna__ attribute has the right type */
py_srna = ( BPy_StructRNA * ) item ;
if ( py_srna - > ptr . type ! = & RNA_Struct ) {
PyErr_SetString ( PyExc_AttributeError , " expected a Type subclassed from a registerable rna type (not a Struct). " ) ;
return NULL ;
}
/* check that we have a unregister callback for this type */
unreg = RNA_struct_unregister ( py_srna - > ptr . data ) ;
if ( ! unreg ) {
PyErr_SetString ( PyExc_AttributeError , " expected a Type subclassed from a registerable rna type (no unregister supported). " ) ;
return NULL ;
}
/* get the context, so register callback can do necessary refreshes */
2009-05-25 13:48:44 +00:00
C = BPy_GetContext ( ) ;
2009-04-19 13:37:59 +00:00
/* call unregister */
unreg ( C , py_srna - > ptr . data ) ;
/* remove reference to old type */
Py_DECREF ( py_class ) ;
Py_RETURN_NONE ;
}