From 417aeb935a9a2ca94f54e177e92bc7b616e04458 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 28 Jun 2009 14:55:22 +0000 Subject: [PATCH] patch [#18950] Get/set bone and type of drivers by Alberto Torres Ruiz (dithi) --- quoting the patch submission This patchs adds the properties IpoCurve.driverBone and IpoCurve.driverBone2 and modifies IpoCurve.driverChannel to allow OB_ROT_DIFF. It sets the driver type to pose if IpoCurve.driverBone is not empty or None. Otherwise the driver type is set to object. Attached is the patch (with python doc) and an example .blend. It also fixes the confusing description of IpoCurve.driver --- source/blender/python/api2_2x/Ipocurve.c | 112 +++++++++++++++++- source/blender/python/api2_2x/doc/IpoCurve.py | 10 +- 2 files changed, 119 insertions(+), 3 deletions(-) diff --git a/source/blender/python/api2_2x/Ipocurve.c b/source/blender/python/api2_2x/Ipocurve.c index 5dfb52f2733..d286eda6904 100644 --- a/source/blender/python/api2_2x/Ipocurve.c +++ b/source/blender/python/api2_2x/Ipocurve.c @@ -89,10 +89,14 @@ static PyObject *IpoCurve_getDriverObject( C_IpoCurve * self); static int IpoCurve_setDriverObject( C_IpoCurve * self, PyObject * args ); static PyObject *IpoCurve_getDriverChannel( C_IpoCurve * self); static int IpoCurve_setDriverChannel( C_IpoCurve * self, PyObject * args ); +static PyObject *IpoCurve_getDriverBone( C_IpoCurve * self); +static PyObject *IpoCurve_getDriverBone2( C_IpoCurve * self); static PyObject *IpoCurve_getDriverExpression( C_IpoCurve * self); static PyObject *IpoCurve_getFlag( C_IpoCurve * self, void *type); static int IpoCurve_setFlag( C_IpoCurve * self, PyObject *value, void *type); +static int IpoCurve_setDriverBone( C_IpoCurve * self, PyObject * args ); +static int IpoCurve_setDriverBone2( C_IpoCurve * self, PyObject * args ); static int IpoCurve_setDriverExpression( C_IpoCurve * self, PyObject * args ); static PyObject *IpoCurve_getCurval( C_IpoCurve * self, PyObject * args ); static int IpoCurve_setCurval( C_IpoCurve * self, PyObject * key, @@ -159,6 +163,14 @@ static PyGetSetDef C_IpoCurve_getseters[] = { (getter)IpoCurve_getDriverChannel, (setter)IpoCurve_setDriverChannel, "The channel on the driver object used to drive the IpoCurve", NULL}, + {"driverBone", + (getter)IpoCurve_getDriverBone, (setter)IpoCurve_setDriverBone, + "The armature bone used to drive the IpoCurve", + NULL}, + {"driverBone2", + (getter)IpoCurve_getDriverBone2, (setter)IpoCurve_setDriverBone2, + "The second armature bone used to drive the IpoCurve (only with ROT_DIFF channel)", + NULL}, {"driverExpression", (getter)IpoCurve_getDriverExpression, (setter)IpoCurve_setDriverExpression, "The python expression on the driver used to drive the IpoCurve", @@ -912,7 +924,8 @@ static int IpoCurve_setDriverChannel( C_IpoCurve * self, PyObject * args ) param = (short)PyInt_AS_LONG ( args ); if( ( param >= OB_LOC_X && param <= OB_LOC_Z ) || ( param >= OB_ROT_X && param <= OB_ROT_Z ) - || ( param >= OB_SIZE_X && param <= OB_SIZE_Z ) ) { + || ( param >= OB_SIZE_X && param <= OB_SIZE_Z ) + || ( param == OB_ROT_DIFF && ipo->driver->blocktype==ID_AR ) ) { ipo->driver->adrcode = (short)PyInt_AS_LONG ( args ); return 0; } @@ -920,6 +933,102 @@ static int IpoCurve_setDriverChannel( C_IpoCurve * self, PyObject * args ) return EXPP_ReturnIntError( PyExc_ValueError, "invalid int argument" ); } +static PyObject *IpoCurve_getDriverBone( C_IpoCurve * self ) +{ + IpoCurve *ipo = self->ipocurve; + + if( ipo->driver && ipo->driver->type == IPO_DRIVER_TYPE_NORMAL && + ipo->driver->ob->type==OB_ARMATURE && + ipo->driver->blocktype==ID_AR ) + return PyString_FromString( ipo->driver->name ); + + Py_RETURN_NONE; +} + +static PyObject *IpoCurve_getDriverBone2( C_IpoCurve * self ) +{ + IpoCurve *ipo = self->ipocurve; + + if( ipo->driver && ipo->driver->type == IPO_DRIVER_TYPE_NORMAL && + ipo->driver->ob->type==OB_ARMATURE && + ipo->driver->blocktype==ID_AR ) + return PyString_FromString( ipo->driver->name+DRIVER_NAME_OFFS ); + + Py_RETURN_NONE; +} + +static int IpoCurve_setDriverBone( C_IpoCurve * self, PyObject * arg ) +{ + IpoCurve *ipo = self->ipocurve; + char *bone; /* bone name */ + + if( !ipo->driver ) + return EXPP_ReturnIntError( PyExc_RuntimeError, + "This IpoCurve does not have an active driver" ); + + if (ipo->driver->type != IPO_DRIVER_TYPE_NORMAL || + ipo->driver->ob->type!=OB_ARMATURE) + return EXPP_ReturnIntError( PyExc_RuntimeError, + "This driver is not of object type or object is not an armature" ); + + if(!PyString_Check(arg) && arg!=Py_None) + return EXPP_ReturnIntError( PyExc_RuntimeError, + "expected a string argument or None" ); + + if( arg!=Py_None ){ + bone = PyString_AsString(arg); + if (strlen(bone)>31) + return EXPP_ReturnIntError( PyExc_ValueError, + "string is too long, use 31 characters or less" ); + + strcpy(ipo->driver->name, bone); + + if (strlen(bone)>0) + ipo->driver->blocktype=ID_AR; + else + ipo->driver->blocktype=ID_OB; + } else { + ipo->driver->name[0]=0; + ipo->driver->blocktype=ID_OB; + } + + return 0; +} + +static int IpoCurve_setDriverBone2( C_IpoCurve * self, PyObject * arg ) +{ + IpoCurve *ipo = self->ipocurve; + char *bone; /* bone name */ + + if( !ipo->driver ) + return EXPP_ReturnIntError( PyExc_RuntimeError, + "This IpoCurve does not have an active driver" ); + + if (ipo->driver->type != IPO_DRIVER_TYPE_NORMAL || + ipo->driver->ob->type!=OB_ARMATURE) + return EXPP_ReturnIntError( PyExc_RuntimeError, + "This driver is not of object type or object is not an armature" ); + + if(!PyString_Check(arg) && arg!=Py_None) + return EXPP_ReturnIntError( PyExc_RuntimeError, + "expected a string argument or None" ); + + if( arg!=Py_None ){ + bone = PyString_AsString(arg); + if (strlen(bone)>31) + return EXPP_ReturnIntError( PyExc_ValueError, + "string is too long, use 31 characters or less" ); + + strcpy(ipo->driver->name+DRIVER_NAME_OFFS, bone); + + } else { + ipo->driver->name[DRIVER_NAME_OFFS]=0; + } + + return 0; +} + + static PyObject *IpoCurve_getDriverExpression( C_IpoCurve * self ) { IpoCurve *ipo = self->ipocurve; @@ -1010,6 +1119,7 @@ PyObject *IpoCurve_Init( void ) PyModule_AddIntConstant( submodule, "SIZE_X", OB_SIZE_X ); PyModule_AddIntConstant( submodule, "SIZE_Y", OB_SIZE_Y ); PyModule_AddIntConstant( submodule, "SIZE_Z", OB_SIZE_Z ); + PyModule_AddIntConstant( submodule, "ROT_DIFF", OB_ROT_DIFF ); if( ExtendTypes ) PyModule_AddObject( submodule, "ExtendTypes", ExtendTypes ); diff --git a/source/blender/python/api2_2x/doc/IpoCurve.py b/source/blender/python/api2_2x/doc/IpoCurve.py index 850a1825325..c96dafe0625 100644 --- a/source/blender/python/api2_2x/doc/IpoCurve.py +++ b/source/blender/python/api2_2x/doc/IpoCurve.py @@ -50,10 +50,16 @@ class IpoCurve: the IPO Curve Editor window. Positive rotations are in a counter-clockwise direction, following the standard convention. - @ivar driver: Status of the driver. 1= on, 0= object, 2= python expression. + @ivar driver: Status of the driver. 1= object or armature/bone driver, 2= python expression, 0= no driver @type driver: int @ivar driverObject: Object used to drive the Ipo curve. @type driverObject: Blender Object or None + @ivar driverBone: Name of the armature bone used to drive the Ipo curve. + If empty or None, sets driver type to object, otherwise sets driver type to pose. [0 - 31 chars] + @type driverBone: string or None + @ivar driverBone2: Name of the second bone used to drive the Ipo curve. + Only to be used with ROT_DIFF channel. [0 - 31 chars] + @type driverBone2: string or None @ivar driverExpression: Python expression used to drive the Ipo curve. [0 - 127 chars] @type driverExpression: string @ivar sel: The selection state of this curve. @@ -61,7 +67,7 @@ class IpoCurve: @ivar driverChannel: Object channel used to drive the Ipo curve. Use module constants: IpoCurve.LOC_X, IpoCurve.LOC_Y, IpoCurve.LOC_Z, IpoCurve.ROT_X, IpoCurve.ROT_Y, IpoCurve.ROT_Z, IpoCurve.SIZE_X, - IpoCurve.SIZE_Y, IpoCurve.SIZE_Z + IpoCurve.SIZE_Y, IpoCurve.SIZE_Z, IpoCurve.ROT_DIFF (this last one is only for pose type drivers) @type driverChannel: int @ivar name: The IpoCurve data name. @type name: string