- new internal Property module
- Object support for add/remove/modify object Properties
This commit is contained in:
@@ -35,12 +35,16 @@
|
||||
|
||||
#include "Object.h"
|
||||
#include "NLA.h"
|
||||
#include "logic.h"
|
||||
#include <blendef.h>
|
||||
#include <DNA_scene_types.h>
|
||||
#include <DNA_property_types.h>
|
||||
#include <BSE_edit.h>
|
||||
#include <BKE_property.h>
|
||||
#include <BKE_mball.h>
|
||||
#include <BIF_editview.h>
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Python API function prototypes for the Blender module. */
|
||||
/*****************************************************************************/
|
||||
@@ -131,6 +135,12 @@ static PyObject *Object_setSize (BPy_Object *self, PyObject *args);
|
||||
static PyObject *Object_setTimeOffset (BPy_Object *self, PyObject *args);
|
||||
static PyObject *Object_shareFrom (BPy_Object *self, PyObject *args);
|
||||
static PyObject *Object_Select (BPy_Object *self, PyObject *args);
|
||||
static PyObject *Object_getAllProperties (BPy_Object *self);
|
||||
static PyObject *Object_addProperty(BPy_Object *self, PyObject *args);
|
||||
static PyObject *Object_removeProperty(BPy_Object *self, PyObject *args);
|
||||
static PyObject *Object_getProperty(BPy_Object *self, PyObject *args);
|
||||
static PyObject *Object_removeAllProperties(BPy_Object *self);
|
||||
static PyObject *Object_copyAllPropertiesTo(BPy_Object *self, PyObject *args);
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Python BPy_Object methods table: */
|
||||
@@ -237,6 +247,18 @@ works only if self and the object specified are of the same type."},
|
||||
"(Blender Ipo) - Sets the object's ipo"},
|
||||
{"clearIpo", (PyCFunction)Object_clearIpo, METH_NOARGS,
|
||||
"() - Unlink ipo from this object"},
|
||||
{"getAllProperties", (PyCFunction)Object_getAllProperties, METH_NOARGS,
|
||||
"() - Get all the properties from this object"},
|
||||
{"addProperty", (PyCFunction)Object_addProperty, METH_VARARGS,
|
||||
"() - Add a property to this object"},
|
||||
{"removeProperty", (PyCFunction)Object_removeProperty, METH_VARARGS,
|
||||
"() - Remove a property from this object"},
|
||||
{"getProperty", (PyCFunction)Object_getProperty, METH_VARARGS,
|
||||
"() - Get a property from this object by name"},
|
||||
{"removeAllProperties", (PyCFunction)Object_removeAllProperties, METH_NOARGS,
|
||||
"() - removeAll a properties from this object"},
|
||||
{"copyAllPropertiesTo", (PyCFunction)Object_copyAllPropertiesTo, METH_VARARGS,
|
||||
"() - copy all properties from this object to another object"},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
@@ -1665,6 +1687,175 @@ static PyObject *Object_Select (BPy_Object *self, PyObject *args)
|
||||
return (Py_None);
|
||||
}
|
||||
|
||||
static PyObject *Object_getAllProperties(BPy_Object *self)
|
||||
{
|
||||
PyObject *prop_list;
|
||||
bProperty *prop= NULL;
|
||||
|
||||
prop_list = PyList_New(0);
|
||||
|
||||
prop = self->object->prop.first;
|
||||
while(prop){
|
||||
PyList_Append(prop_list, Property_CreatePyObject(prop));
|
||||
prop = prop->next;
|
||||
}
|
||||
return prop_list;
|
||||
}
|
||||
|
||||
static PyObject *Object_getProperty(BPy_Object *self, PyObject *args)
|
||||
{
|
||||
char *prop_name = NULL;
|
||||
bProperty *prop= NULL;
|
||||
PyObject *py_prop = Py_None;
|
||||
|
||||
if(!PyArg_ParseTuple (args, "s", &prop_name)){
|
||||
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
|
||||
"expected a string"));
|
||||
}
|
||||
|
||||
prop = get_property(self->object, prop_name);
|
||||
if(prop){
|
||||
py_prop = Property_CreatePyObject(prop);
|
||||
}else{
|
||||
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
|
||||
"couldn't find the property...."));
|
||||
}
|
||||
return py_prop;
|
||||
}
|
||||
|
||||
static PyObject *Object_addProperty(BPy_Object *self, PyObject *args)
|
||||
{
|
||||
bProperty *prop = NULL;
|
||||
char *prop_name = NULL;
|
||||
PyObject *prop_data = Py_None;
|
||||
char *prop_type = NULL;
|
||||
short type = -1;
|
||||
BPy_Property *py_prop = NULL;
|
||||
|
||||
if (PyObject_Length (args) == 3 || PyObject_Length (args) == 2){
|
||||
if (!PyArg_ParseTuple (args, "sO|s", &prop_name, &prop_data, &prop_type)){
|
||||
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
|
||||
"unable to get string, data, and optional string"));
|
||||
}
|
||||
}else if (PyObject_Length (args) == 1){
|
||||
if (!PyArg_ParseTuple (args, "O!", &property_Type, &py_prop)){
|
||||
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
|
||||
"unable to get Property"));
|
||||
}
|
||||
if(py_prop->property != NULL){
|
||||
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
|
||||
"Property is already added to an object"));
|
||||
}
|
||||
}else{
|
||||
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
|
||||
"expected 1,2 or 3 arguments"));
|
||||
}
|
||||
|
||||
//parse property type
|
||||
if(!py_prop){
|
||||
if(prop_type){
|
||||
if(BLI_streq(prop_type, "BOOL")) type = PROP_BOOL;
|
||||
else if (BLI_streq(prop_type, "INT")) type = PROP_INT;
|
||||
else if (BLI_streq(prop_type, "FLOAT")) type = PROP_FLOAT;
|
||||
else if (BLI_streq(prop_type, "TIME")) type = PROP_TIME;
|
||||
else if (BLI_streq(prop_type, "STRING")) type = PROP_STRING;
|
||||
else return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
|
||||
"BOOL, INT, FLOAT, TIME or STRING expected"));
|
||||
}else{
|
||||
//use the default
|
||||
if(PyInt_Check(prop_data)) type = PROP_INT;
|
||||
else if (PyFloat_Check(prop_data)) type = PROP_FLOAT;
|
||||
else if (PyString_Check(prop_data)) type = PROP_STRING;
|
||||
}
|
||||
}else{
|
||||
type = py_prop->type;
|
||||
}
|
||||
|
||||
//initialize a new bProperty of the specified type
|
||||
prop = new_property(type);
|
||||
|
||||
//parse data
|
||||
if(!py_prop){
|
||||
BLI_strncpy(prop->name, prop_name, 32);
|
||||
if(PyInt_Check(prop_data)){
|
||||
*((int*)&prop->data) = (int)PyInt_AsLong(prop_data);
|
||||
}else if (PyFloat_Check(prop_data)){
|
||||
*((float *)&prop->data) = (float)PyFloat_AsDouble(prop_data);
|
||||
}else if (PyString_Check(prop_data)){
|
||||
BLI_strncpy(prop->poin, PyString_AsString(prop_data), MAX_PROPSTRING);
|
||||
}
|
||||
}else{
|
||||
py_prop->property = prop;
|
||||
if(!updateProperyData(py_prop)){
|
||||
return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
|
||||
"Could not update property data - error"));
|
||||
}
|
||||
}
|
||||
|
||||
//add to property listbase for the object
|
||||
BLI_addtail(&self->object->prop, prop);
|
||||
|
||||
return EXPP_incr_ret (Py_None);
|
||||
}
|
||||
|
||||
static PyObject *Object_removeProperty(BPy_Object *self, PyObject *args)
|
||||
{
|
||||
char *prop_name = NULL;
|
||||
BPy_Property *py_prop = NULL;
|
||||
bProperty *prop = NULL;
|
||||
|
||||
// we have property and no optional arg
|
||||
if (!PyArg_ParseTuple (args, "O!", &property_Type, &py_prop)){
|
||||
if(!PyArg_ParseTuple (args, "s", &prop_name)){
|
||||
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
|
||||
"expected a Property or a string"));
|
||||
}
|
||||
}
|
||||
|
||||
//remove the link, free the data, and update the py struct
|
||||
if(py_prop){
|
||||
BLI_remlink(&self->object->prop, py_prop->property);
|
||||
if(updatePyProperty(py_prop)){
|
||||
free_property(py_prop->property);
|
||||
py_prop->property = NULL;
|
||||
}
|
||||
}else{
|
||||
prop = get_property(self->object, prop_name);
|
||||
if(prop){
|
||||
BLI_remlink(&self->object->prop, prop);
|
||||
free_property(prop);
|
||||
}
|
||||
}
|
||||
return EXPP_incr_ret (Py_None);
|
||||
}
|
||||
|
||||
static PyObject *Object_removeAllProperties(BPy_Object *self)
|
||||
{
|
||||
free_properties(&self->object->prop);
|
||||
return EXPP_incr_ret (Py_None);
|
||||
}
|
||||
|
||||
static PyObject *Object_copyAllPropertiesTo(BPy_Object *self, PyObject *args)
|
||||
{
|
||||
PyObject *dest = Py_None;
|
||||
bProperty *prop = NULL;
|
||||
bProperty *propn = NULL;
|
||||
|
||||
if (!PyArg_ParseTuple (args, "O!", &Object_Type, &dest)){
|
||||
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
|
||||
"expected an Object"));
|
||||
}
|
||||
|
||||
//make a copy of all it's properties
|
||||
prop = self->object->prop.first;
|
||||
while(prop) {
|
||||
propn= copy_property(prop);
|
||||
BLI_addtail(&((BPy_Object*)dest)->object->prop, propn);
|
||||
prop= prop->next;
|
||||
}
|
||||
|
||||
return EXPP_incr_ret (Py_None);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Function: Object_CreatePyObject */
|
||||
@@ -2116,4 +2307,3 @@ static PyObject *Object_repr (BPy_Object *self)
|
||||
{
|
||||
return PyString_FromFormat("[Object \"%s\"]", self->object->id.name+2);
|
||||
}
|
||||
|
||||
|
||||
@@ -81,6 +81,7 @@ void types_InitAll(void)
|
||||
quaternion_Type.ob_type = &PyType_Type;
|
||||
rgbTuple_Type.ob_type = &PyType_Type;
|
||||
vector_Type.ob_type = &PyType_Type;
|
||||
property_Type.ob_type = &PyType_Type;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
@@ -139,6 +140,7 @@ PyObject *Types_Init (void)
|
||||
PyDict_SetItemString(dict, "quaternionType", (PyObject *)&quaternion_Type);
|
||||
PyDict_SetItemString(dict, "BezTripleType", (PyObject *)&BezTriple_Type);
|
||||
PyDict_SetItemString(dict, "ActionType", (PyObject *)&Action_Type);
|
||||
PyDict_SetItemString(dict, "propertyType", (PyObject *)&property_Type);
|
||||
|
||||
return submodule;
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ extern PyTypeObject Particle_Type;
|
||||
extern PyTypeObject Scene_Type, RenderData_Type;
|
||||
extern PyTypeObject Text_Type, Texture_Type;
|
||||
extern PyTypeObject Wave_Type, World_Type;
|
||||
|
||||
extern PyTypeObject property_Type;
|
||||
extern PyTypeObject buffer_Type, constant_Type, euler_Type;
|
||||
extern PyTypeObject matrix_Type, quaternion_Type, rgbTuple_Type, vector_Type;
|
||||
|
||||
|
||||
468
source/blender/python/api2_2x/logic.c
Normal file
468
source/blender/python/api2_2x/logic.c
Normal file
@@ -0,0 +1,468 @@
|
||||
/*
|
||||
*
|
||||
* ***** BEGIN GPL/BL DUAL 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. The Blender
|
||||
* Foundation also sells licenses for use in proprietary software under
|
||||
* the Blender License. See http://www.blender.org/BL/ for information
|
||||
* about this.
|
||||
*
|
||||
* 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) 2001-2002 by NaN Holding BV.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This is a new part of Blender.
|
||||
*
|
||||
* Contributor(s): Joseph Gilbert
|
||||
*
|
||||
* ***** END GPL/BL DUAL LICENSE BLOCK *****
|
||||
*/
|
||||
|
||||
#include "logic.h"
|
||||
#include "gen_utils.h"
|
||||
#include <MEM_guardedalloc.h>
|
||||
#include <BLI_blenlib.h>
|
||||
|
||||
//--------------- Python BPy_Property methods declarations:---------------------------------------------------
|
||||
static PyObject *Property_getName (BPy_Property * self);
|
||||
static PyObject *Property_setName (BPy_Property * self, PyObject * args);
|
||||
static PyObject *Property_getData (BPy_Property * self);
|
||||
static PyObject *Property_setData (BPy_Property * self, PyObject * args);
|
||||
static PyObject *Property_getType (BPy_Property * self);
|
||||
//--------------- Python BPy_Property methods table:-----------------------------------------------------------------
|
||||
static PyMethodDef BPy_Property_methods[] = {
|
||||
{"getName", (PyCFunction) Property_getName, METH_NOARGS,
|
||||
"() - return Property name"},
|
||||
{"setName", (PyCFunction)Property_setName, METH_VARARGS,
|
||||
"() - set the name of this Property"},
|
||||
{"getData", (PyCFunction) Property_getData, METH_NOARGS,
|
||||
"() - return Property data"},
|
||||
{"setData", (PyCFunction)Property_setData, METH_VARARGS,
|
||||
"() - set the data of this Property"},
|
||||
{"getType", (PyCFunction) Property_getType, METH_NOARGS,
|
||||
"() - return Property type"},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
//--------------- Python TypeProperty callback function prototypes----------------------------------------
|
||||
static void Property_dealloc (BPy_Property * Property);
|
||||
static PyObject *Property_getAttr (BPy_Property * Property, char *name);
|
||||
static int Property_setAttr (BPy_Property * Property, char *name, PyObject * v);
|
||||
static PyObject *Property_repr (BPy_Property * Property);
|
||||
static int Property_compare (BPy_Property * a1, BPy_Property * a2);
|
||||
//--------------- Python TypeProperty structure definition------------------------------------------------------
|
||||
PyTypeObject property_Type = {
|
||||
PyObject_HEAD_INIT (NULL) 0, /* ob_size */
|
||||
"Blender Property", /* tp_name */
|
||||
sizeof (BPy_Property), /* tp_basicsize */
|
||||
0, /* tp_itemsize */
|
||||
/* methods */
|
||||
(destructor) Property_dealloc, /* tp_dealloc */
|
||||
0, /* tp_print */
|
||||
(getattrfunc) Property_getAttr, /* tp_getattr */
|
||||
(setattrfunc) Property_setAttr, /* tp_setattr */
|
||||
(cmpfunc) Property_compare, /* tp_compare */
|
||||
(reprfunc) Property_repr, /* tp_repr */
|
||||
0, /* tp_as_number */
|
||||
0, /* tp_as_sequence */
|
||||
0, /* tp_as_mapping */
|
||||
0, /* tp_as_hash */
|
||||
0, 0, 0, 0, 0, 0,
|
||||
0, /* tp_doc */
|
||||
0, 0, 0, 0, 0, 0,
|
||||
BPy_Property_methods, /* tp_methods */
|
||||
0, /* tp_members */
|
||||
};
|
||||
//--------------- Property module internal callbacks-------------------------------------------------------------
|
||||
//--------------- updatePyProperty-------------------------------------------------------------------------------------
|
||||
int
|
||||
updatePyProperty(BPy_Property *self)
|
||||
{
|
||||
if(!self->property){
|
||||
return 0; //nothing to update - not linked
|
||||
}else{
|
||||
BLI_strncpy(self->name, self->property->name, 32);
|
||||
self->type = self->property->type;
|
||||
if(self->property->type == PROP_BOOL){
|
||||
if(*((int*)&self->property->poin)){
|
||||
self->data = EXPP_incr_ret(Py_True);
|
||||
}else{
|
||||
self->data = EXPP_incr_ret(Py_False);
|
||||
}
|
||||
}else if (self->property->type == PROP_INT){
|
||||
self->data = PyInt_FromLong(self->property->data);
|
||||
}else if (self->property->type == PROP_FLOAT){
|
||||
self->data = PyFloat_FromDouble(*((float*)&self->property->data));
|
||||
}else if (self->property->type == PROP_TIME){
|
||||
self->data = PyFloat_FromDouble(*((float*)&self->property->data));
|
||||
}else if (self->property->type == PROP_STRING){
|
||||
self->data = PyString_FromString(self->property->poin);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
//--------------- updatePropertyData-------------------------------------------------------------------------------------
|
||||
int
|
||||
updateProperyData(BPy_Property *self)
|
||||
{
|
||||
if(!self->property){
|
||||
//nothing to update - not linked
|
||||
return 0;
|
||||
}else{
|
||||
BLI_strncpy(self->property->name, self->name, 32);
|
||||
self->property->type = self->type;
|
||||
if(PyInt_Check(self->data)){
|
||||
*((int*)&self->property->data) = (int)PyInt_AsLong(self->data);
|
||||
}else if (PyFloat_Check(self->data)){
|
||||
*((float *)&self->property->data) = (float)PyFloat_AsDouble(self->data);
|
||||
}else if (PyString_Check(self->data)){
|
||||
BLI_strncpy(self->property->poin, PyString_AsString(self->data), MAX_PROPSTRING);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
//--------------- checkValidData_ptr-------------------------------------------------------------------------------------
|
||||
int
|
||||
checkValidData_ptr(BPy_Property *self)
|
||||
{
|
||||
int length;
|
||||
//test pointer to see if data was removed (oops)
|
||||
length = MEM_allocN_len(self->property);
|
||||
if(length != sizeof(bProperty)){ //data was freed
|
||||
self->property = NULL;
|
||||
return 0;
|
||||
}else{ //it's ok as far as we can tell
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
//---------------BPy_Property internal callbacks/methods---------------------------------------------
|
||||
//--------------- dealloc---------------------------------------------------------------------------------------
|
||||
static void
|
||||
Property_dealloc (BPy_Property * self)
|
||||
{
|
||||
PyMem_Free (self->name);
|
||||
PyObject_DEL (self);
|
||||
}
|
||||
//---------------getattr---------------------------------------------------------------------------------------
|
||||
static PyObject *
|
||||
Property_getAttr (BPy_Property * self, char *name)
|
||||
{
|
||||
PyObject *attr = Py_None;
|
||||
|
||||
checkValidData_ptr(self);
|
||||
if (strcmp (name, "name") == 0)
|
||||
attr = Property_getName (self);
|
||||
else if (strcmp (name, "data") == 0)
|
||||
attr = Property_getData (self);
|
||||
else if (strcmp (name, "type") == 0)
|
||||
attr = Property_getType (self);
|
||||
else if (strcmp (name, "__members__") == 0){
|
||||
attr = Py_BuildValue ("[s,s,s]", "name", "data","type");
|
||||
}
|
||||
|
||||
if (!attr)
|
||||
return (EXPP_ReturnPyObjError (PyExc_MemoryError,"couldn't create PyObject"));
|
||||
|
||||
if (attr != Py_None) return attr;
|
||||
|
||||
return Py_FindMethod (BPy_Property_methods, (PyObject *) self, name);
|
||||
}
|
||||
//--------------- setattr---------------------------------------------------------------------------------------
|
||||
static int
|
||||
Property_setAttr (BPy_Property * self, char *name, PyObject * value)
|
||||
{
|
||||
PyObject *valtuple;
|
||||
PyObject *error = NULL;
|
||||
|
||||
checkValidData_ptr(self);
|
||||
valtuple = Py_BuildValue ("(O)", value);
|
||||
if (!valtuple)
|
||||
return EXPP_ReturnIntError (PyExc_MemoryError,
|
||||
"PropertySetAttr: couldn't create tuple");
|
||||
|
||||
if (strcmp (name, "name") == 0)
|
||||
error = Property_setName (self, valtuple);
|
||||
else if (strcmp(name, "data") == 0)
|
||||
error = Property_setData (self, valtuple);
|
||||
else{
|
||||
Py_DECREF (valtuple);
|
||||
return (EXPP_ReturnIntError (PyExc_KeyError, "attribute not found"));
|
||||
}
|
||||
Py_DECREF (valtuple);
|
||||
|
||||
if (error != Py_None) return -1;
|
||||
|
||||
Py_DECREF (Py_None);
|
||||
return 0;
|
||||
}
|
||||
//--------------- repr---------------------------------------------------------------------------------------
|
||||
static PyObject *
|
||||
Property_repr (BPy_Property * self)
|
||||
{
|
||||
checkValidData_ptr(self);
|
||||
if (self->property){
|
||||
return PyString_FromFormat ("[Property \"%s\"]", self->property->name);
|
||||
}else{
|
||||
return PyString_FromFormat ("[Property \"%s\"]", self->name);
|
||||
}
|
||||
}
|
||||
//--------------- compare---------------------------------------------------------------------------------------
|
||||
//compares property.name and property.data
|
||||
static int
|
||||
Property_compare (BPy_Property * a, BPy_Property * b)
|
||||
{
|
||||
BPy_Property *py_propA, *py_propB;
|
||||
int retval = -1;
|
||||
|
||||
checkValidData_ptr(a);
|
||||
checkValidData_ptr(b);
|
||||
//2 python objects
|
||||
if (!a->property && !b->property) {
|
||||
if(a->type != b->type) retval = -1;
|
||||
if(BLI_streq(a->name, b->name)){
|
||||
retval = PyObject_Compare(a->data, b->data);
|
||||
}else retval = -1;
|
||||
}else if (a->property && b->property){ //2 real properties
|
||||
if(a->property->type != b->property->type) retval = -1;
|
||||
if(BLI_streq(a->property->name, b->property->name)){
|
||||
if(a->property->type == PROP_BOOL || a->property->type == PROP_INT){
|
||||
if(a->property->data == b->property->data) retval = 0;
|
||||
else retval = -1;
|
||||
}else if (a->property->type == PROP_FLOAT || a->property->type == PROP_TIME){
|
||||
if(*((float*)&a->property->data) == *((float*)&b->property->data)) retval = 0;
|
||||
else retval = -1;
|
||||
}else if (a->property->type == PROP_STRING){
|
||||
if(BLI_streq(a->property->poin, b->property->poin)) retval = 0;
|
||||
else retval = -1;
|
||||
}
|
||||
}else retval = -1;
|
||||
}else{ //1 real 1 python
|
||||
if(!a->property){
|
||||
py_propA = a; py_propB = b;
|
||||
}else {
|
||||
py_propA = b; py_propB = a;
|
||||
}
|
||||
if(py_propB->property->type != py_propA->type) retval = -1;
|
||||
if(BLI_streq(py_propB->property->name, py_propA->name)){
|
||||
if(py_propB->property->type == PROP_BOOL ||
|
||||
py_propB->property->type == PROP_INT){
|
||||
retval = PyObject_Compare(py_propA->data,
|
||||
PyInt_FromLong(py_propB->property->data));
|
||||
}else if (py_propB->property->type == PROP_FLOAT ||
|
||||
py_propB->property->type == PROP_TIME){
|
||||
retval = PyObject_Compare(py_propA->data,
|
||||
PyFloat_FromDouble(*((float*)&py_propB->property->data)));
|
||||
}else if (py_propB->property->type == PROP_STRING){
|
||||
retval = PyObject_Compare(py_propA->data,
|
||||
PyString_FromString(py_propB->property->poin));
|
||||
}
|
||||
}else retval = -1;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
//--------------- Property visible functions--------------------------------------------------------------------
|
||||
//--------------- Property_CreatePyObject--------------------------------------------------------------------
|
||||
PyObject *
|
||||
Property_CreatePyObject (struct bProperty * Property)
|
||||
{
|
||||
BPy_Property *py_property;
|
||||
|
||||
py_property = (BPy_Property *)PyObject_NEW (BPy_Property, &property_Type);
|
||||
|
||||
//set the struct flag
|
||||
py_property->property = Property;
|
||||
|
||||
//allocate space for python vars
|
||||
py_property->name= PyMem_Malloc (32);
|
||||
|
||||
if(!updatePyProperty(py_property))
|
||||
return (EXPP_ReturnPyObjError (PyExc_AttributeError , "Property struct empty"));
|
||||
|
||||
return ((PyObject *) py_property);
|
||||
}
|
||||
//--------------- Property_CheckPyObject--------------------------------------------------------------------
|
||||
int
|
||||
Property_CheckPyObject (PyObject * py_obj)
|
||||
{
|
||||
return (py_obj->ob_type == &property_Type);
|
||||
}
|
||||
//--------------- Property_FromPyObject--------------------------------------------------------------------
|
||||
struct bProperty *
|
||||
Property_FromPyObject (PyObject * py_obj)
|
||||
{
|
||||
BPy_Property *py_property;
|
||||
|
||||
py_property = (BPy_Property *) py_obj;
|
||||
if (!py_property->property) return NULL;
|
||||
else return (py_property->property);
|
||||
}
|
||||
//--------------- newPropertyObject()----------------------------------------------------------------------------
|
||||
PyObject *
|
||||
newPropertyObject (char *name, PyObject *data, int type)
|
||||
{
|
||||
BPy_Property *py_property;
|
||||
|
||||
py_property = (BPy_Property *)PyObject_NEW (BPy_Property, &property_Type);
|
||||
py_property->name= PyMem_Malloc (32);
|
||||
py_property->property = NULL;
|
||||
|
||||
BLI_strncpy(py_property->name, name, 32);
|
||||
py_property->data = data;
|
||||
py_property->type = type;
|
||||
|
||||
return (PyObject*)py_property;
|
||||
}
|
||||
//--------------- Python BPy_Property methods------------------------------------------------------------------
|
||||
//--------------- BPy_Property.getName()--------------------------------------------------------------------------
|
||||
static PyObject *
|
||||
Property_getName (BPy_Property * self)
|
||||
{
|
||||
PyObject *attr = NULL;
|
||||
|
||||
if (!self->property) attr = PyString_FromString (self->name);
|
||||
else attr = PyString_FromString (self->property->name);
|
||||
|
||||
if (attr) return attr;
|
||||
|
||||
return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
|
||||
"couldn't get Property.name attribute"));
|
||||
}
|
||||
//--------------- BPy_Property.setName()--------------------------------------------------------------------------
|
||||
static PyObject *
|
||||
Property_setName (BPy_Property * self, PyObject * args)
|
||||
{
|
||||
char *name;
|
||||
|
||||
if (!PyArg_ParseTuple (args, "s", &name))
|
||||
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
|
||||
"expected string argument"));
|
||||
|
||||
if (!self->property){
|
||||
BLI_strncpy(self->name, name, 32);
|
||||
}else{
|
||||
BLI_strncpy(self->property->name, name, 32);
|
||||
updatePyProperty(self);
|
||||
}
|
||||
|
||||
return EXPP_incr_ret (Py_None);
|
||||
}
|
||||
//--------------- BPy_Property.getData()--------------------------------------------------------------------------
|
||||
static PyObject *
|
||||
Property_getData (BPy_Property * self)
|
||||
{
|
||||
PyObject *attr = NULL;
|
||||
|
||||
if (!self->property) {
|
||||
attr = EXPP_incr_ret(self->data);
|
||||
}else{
|
||||
if(self->property->type == PROP_BOOL){
|
||||
if(*((int*)&self->property->poin))
|
||||
attr = EXPP_incr_ret(Py_True);
|
||||
else
|
||||
attr = EXPP_incr_ret(Py_False);
|
||||
}else if (self->property->type == PROP_INT){
|
||||
attr = PyInt_FromLong(self->property->data);
|
||||
}else if (self->property->type == PROP_FLOAT ||
|
||||
self->property->type == PROP_TIME){
|
||||
attr = PyFloat_FromDouble(*((float*)&self->property->data));
|
||||
}else if (self->property->type == PROP_STRING){
|
||||
attr = PyString_FromString(self->property->poin);
|
||||
}
|
||||
}
|
||||
if (attr) return attr;
|
||||
|
||||
return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
|
||||
"couldn't get Property.name attribute"));
|
||||
}
|
||||
//--------------- BPy_Property.setData()--------------------------------------------------------------------------
|
||||
static PyObject *
|
||||
Property_setData (BPy_Property * self, PyObject * args)
|
||||
{
|
||||
PyObject *data;
|
||||
char *type_str = NULL;
|
||||
int type = -1;
|
||||
short *p_type = NULL;
|
||||
|
||||
if (!PyArg_ParseTuple (args, "O|s", &data, &type_str))
|
||||
return (EXPP_ReturnPyObjError (PyExc_AttributeError,
|
||||
"expected object and optional string argument"));
|
||||
|
||||
if(!PyInt_Check(data) && !PyFloat_Check(data) && !PyString_Check(data))
|
||||
return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
|
||||
"float, int, or string expected as data"));
|
||||
|
||||
//parse property name
|
||||
if(type_str){
|
||||
if(BLI_streq(type_str, "BOOL")) type = PROP_BOOL;
|
||||
else if (BLI_streq(type_str, "INT")) type = PROP_INT;
|
||||
else if (BLI_streq(type_str, "FLOAT")) type = PROP_FLOAT;
|
||||
else if (BLI_streq(type_str, "TIME")) type = PROP_TIME;
|
||||
else if (BLI_streq(type_str, "STRING")) type = PROP_STRING;
|
||||
else return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
|
||||
"BOOL, INT, FLOAT, TIME or STRING expected"));
|
||||
}
|
||||
|
||||
//get pointer to type
|
||||
if(self->property) p_type = &self->property->type;
|
||||
else p_type = &self->type;
|
||||
|
||||
//set the type
|
||||
if(PyInt_Check(data)){
|
||||
if(type == -1 || type == PROP_INT) *p_type = PROP_INT;
|
||||
else *p_type = PROP_BOOL;
|
||||
}else if(PyFloat_Check(data)){
|
||||
if(type == -1 || type == PROP_FLOAT) *p_type = PROP_FLOAT;
|
||||
else *p_type = PROP_TIME;
|
||||
}else if(PyString_Check(data)){
|
||||
if(type == -1 || type == PROP_STRING) *p_type = PROP_STRING;
|
||||
}else{
|
||||
return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
|
||||
"cant set unknown data type"));
|
||||
}
|
||||
|
||||
//set the data
|
||||
if(self->property){
|
||||
if(PyInt_Check(data)){
|
||||
*((int*)&self->property->data) = (int)PyInt_AsLong(data);
|
||||
}else if (PyFloat_Check(data)){
|
||||
*((float *)&self->property->data) = (float)PyFloat_AsDouble(data);
|
||||
}else if (PyString_Check(data)){
|
||||
BLI_strncpy(self->property->poin, PyString_AsString(data), MAX_PROPSTRING);
|
||||
}
|
||||
updatePyProperty(self);
|
||||
}else{
|
||||
self->data = data;
|
||||
}
|
||||
return EXPP_incr_ret (Py_None);
|
||||
}
|
||||
//--------------- BPy_Property.getType()--------------------------------------------------------------------------
|
||||
static PyObject *
|
||||
Property_getType (BPy_Property * self)
|
||||
{
|
||||
PyObject *attr = Py_None;
|
||||
int type;
|
||||
|
||||
if(self->property) type = self->property->type;
|
||||
else type = self->type;
|
||||
|
||||
if(type == PROP_BOOL) attr = Py_BuildValue("s", "BOOL");
|
||||
else if (type == PROP_INT) attr = Py_BuildValue("s", "INT");
|
||||
else if (type == PROP_FLOAT) attr = Py_BuildValue("s", "FLOAT");
|
||||
else if (type == PROP_STRING) attr = Py_BuildValue("s", "STRING");
|
||||
else if (type == PROP_TIME) attr = Py_BuildValue("s", "TIME");
|
||||
|
||||
return attr;
|
||||
}
|
||||
|
||||
57
source/blender/python/api2_2x/logic.h
Normal file
57
source/blender/python/api2_2x/logic.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
*
|
||||
* ***** BEGIN GPL/BL DUAL 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. The Blender
|
||||
* Foundation also sells licenses for use in proprietary software under
|
||||
* the Blender License. See http://www.blender.org/BL/ for information
|
||||
* about this.
|
||||
*
|
||||
* 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) 2001-2002 by NaN Holding BV.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This is a new part of Blender.
|
||||
*
|
||||
* Contributor(s): Joseph Gilbert
|
||||
*
|
||||
* ***** END GPL/BL DUAL LICENSE BLOCK *****
|
||||
*/
|
||||
|
||||
#ifndef EXPP_LOGIC_H
|
||||
#define EXPP_LOGIC_H
|
||||
|
||||
#include "Python.h"
|
||||
#include <DNA_property_types.h>
|
||||
|
||||
//--------------------------Python BPy_Property structure definition.---------------------
|
||||
typedef struct{
|
||||
PyObject_HEAD
|
||||
//reference to property data if object linked
|
||||
bProperty *property;
|
||||
//list of vars that define the property
|
||||
char *name;
|
||||
PyObject *data;
|
||||
short type;
|
||||
}BPy_Property;
|
||||
|
||||
//------------------------------visible prototypes----------------------------------------------
|
||||
PyObject *Property_CreatePyObject (struct bProperty *prop);
|
||||
int Property_CheckPyObject (PyObject * py_obj);
|
||||
bProperty *Property_FromPyObject (PyObject * py_obj);
|
||||
PyObject * newPropertyObject (char *name, PyObject *data, int type);
|
||||
int updatePyProperty(BPy_Property *self);
|
||||
int updateProperyData(BPy_Property *self);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user