[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [22933] branches/itasc/source: Add GE Python API to armature object/sensor/actuator/constraint.
Benoit Bolsee
benoit.bolsee at online.be
Tue Sep 1 18:51:31 CEST 2009
Revision: 22933
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=22933
Author: ben2610
Date: 2009-09-01 18:51:31 +0200 (Tue, 01 Sep 2009)
Log Message:
-----------
Add GE Python API to armature object/sensor/actuator/constraint. All constraint operations are now possible through python without the use of sensor/acturator. PyDoc added for BL_ArmatureObject, BL_ArmatureActuator, KX_ArmatureSensor, BL_ArmatureConstraint.
Modified Paths:
--------------
branches/itasc/source/blender/makesdna/DNA_actuator_types.h
branches/itasc/source/blender/makesdna/DNA_sensor_types.h
branches/itasc/source/gameengine/Converter/BL_ArmatureActuator.cpp
branches/itasc/source/gameengine/Converter/BL_ArmatureActuator.h
branches/itasc/source/gameengine/Converter/BL_ArmatureConstraint.cpp
branches/itasc/source/gameengine/Converter/BL_ArmatureConstraint.h
branches/itasc/source/gameengine/Converter/BL_ArmatureObject.cpp
branches/itasc/source/gameengine/Converter/BL_ArmatureObject.h
branches/itasc/source/gameengine/Ketsji/KX_ArmatureSensor.cpp
branches/itasc/source/gameengine/Ketsji/KX_ArmatureSensor.h
branches/itasc/source/gameengine/Ketsji/KX_KetsjiEngine.cpp
branches/itasc/source/gameengine/Ketsji/KX_KetsjiEngine.h
branches/itasc/source/gameengine/Ketsji/KX_PythonInit.cpp
branches/itasc/source/gameengine/Ketsji/KX_PythonInitTypes.cpp
branches/itasc/source/gameengine/Ketsji/KX_PythonSeq.cpp
branches/itasc/source/gameengine/Ketsji/KX_PythonSeq.h
branches/itasc/source/gameengine/PyDoc/GameTypes.py
Modified: branches/itasc/source/blender/makesdna/DNA_actuator_types.h
===================================================================
--- branches/itasc/source/blender/makesdna/DNA_actuator_types.h 2009-09-01 16:37:23 UTC (rev 22932)
+++ branches/itasc/source/blender/makesdna/DNA_actuator_types.h 2009-09-01 16:51:31 UTC (rev 22933)
@@ -500,6 +500,8 @@
#define ACT_ARM_DISABLE 2
#define ACT_ARM_SETTARGET 3
#define ACT_ARM_SETWEIGHT 4
+/* update this define if more type are addedd */
+#define ACT_ARM_MAXTYPE 4
#endif
Modified: branches/itasc/source/blender/makesdna/DNA_sensor_types.h
===================================================================
--- branches/itasc/source/blender/makesdna/DNA_sensor_types.h 2009-09-01 16:37:23 UTC (rev 22932)
+++ branches/itasc/source/blender/makesdna/DNA_sensor_types.h 2009-09-01 16:51:31 UTC (rev 22933)
@@ -215,6 +215,8 @@
#define SENS_ARM_LIN_ERROR_ABOVE 2
#define SENS_ARM_ROT_ERROR_BELOW 3
#define SENS_ARM_ROT_ERROR_ABOVE 4
+/* update this when adding new type */
+#define SENS_ARM_MAXTYPE 4
/* sensor->type */
#define SENS_ALWAYS 0
Modified: branches/itasc/source/gameengine/Converter/BL_ArmatureActuator.cpp
===================================================================
--- branches/itasc/source/gameengine/Converter/BL_ArmatureActuator.cpp 2009-09-01 16:37:23 UTC (rev 22932)
+++ branches/itasc/source/gameengine/Converter/BL_ArmatureActuator.cpp 2009-09-01 16:51:31 UTC (rev 22933)
@@ -161,8 +161,10 @@
m_constraint->SetConstraintFlag(CONSTRAINT_OFF);
break;
case ACT_ARM_SETTARGET:
- if (m_constraint)
- m_constraint->SetTarget(m_gametarget, m_gamesubtarget);
+ if (m_constraint) {
+ m_constraint->SetTarget(m_gametarget);
+ m_constraint->SetSubtarget(m_gamesubtarget);
+ }
break;
case ACT_ARM_SETWEIGHT:
if (m_constraint)
@@ -207,13 +209,15 @@
PyMethodDef BL_ArmatureActuator::Methods[] = {
- // no method so far
{NULL,NULL} //Sentinel
};
PyAttributeDef BL_ArmatureActuator::Attributes[] = {
+ KX_PYATTRIBUTE_RO_FUNCTION("constraint", BL_ArmatureActuator, pyattr_get_constraint),
KX_PYATTRIBUTE_RW_FUNCTION("target", BL_ArmatureActuator, pyattr_get_object, pyattr_set_object),
KX_PYATTRIBUTE_RW_FUNCTION("subtarget", BL_ArmatureActuator, pyattr_get_object, pyattr_set_object),
+ KX_PYATTRIBUTE_FLOAT_RW("weight",0.0f,1.0f,BL_ArmatureActuator,m_weight),
+ KX_PYATTRIBUTE_INT_RW("type",0,ACT_ARM_MAXTYPE,false,BL_ArmatureActuator,m_type),
{ NULL } //Sentinel
};
@@ -247,5 +251,15 @@
return PY_SET_ATTR_SUCCESS;
}
+PyObject* BL_ArmatureActuator::pyattr_get_constraint(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
+{
+ BL_ArmatureActuator* actuator = static_cast<BL_ArmatureActuator*>(self);
+ BL_ArmatureConstraint* constraint = actuator->m_constraint;
+ if (!constraint)
+ Py_RETURN_NONE;
+ else
+ return constraint->GetProxy();
+}
+
Modified: branches/itasc/source/gameengine/Converter/BL_ArmatureActuator.h
===================================================================
--- branches/itasc/source/gameengine/Converter/BL_ArmatureActuator.h 2009-09-01 16:37:23 UTC (rev 22932)
+++ branches/itasc/source/gameengine/Converter/BL_ArmatureActuator.h 2009-09-01 16:51:31 UTC (rev 22933)
@@ -43,8 +43,8 @@
class BL_ArmatureActuator : public SCA_IActuator
{
+ Py_Header;
public:
- Py_Header;
BL_ArmatureActuator(SCA_IObject* gameobj,
int type,
const char *posechannel,
@@ -67,6 +67,7 @@
virtual void ReParent(SCA_IObject* parent);
/* These are used to get and set m_target */
+ static PyObject* pyattr_get_constraint(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
static PyObject* pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
static int pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
Modified: branches/itasc/source/gameengine/Converter/BL_ArmatureConstraint.cpp
===================================================================
--- branches/itasc/source/gameengine/Converter/BL_ArmatureConstraint.cpp 2009-09-01 16:37:23 UTC (rev 22932)
+++ branches/itasc/source/gameengine/Converter/BL_ArmatureConstraint.cpp 2009-09-01 16:51:31 UTC (rev 22933)
@@ -35,6 +35,7 @@
#include "BL_ArmatureConstraint.h"
#include "BL_ArmatureObject.h"
#include "BLI_arithb.h"
+#include "BLI_string.h"
PyTypeObject BL_ArmatureConstraint::Type = {
PyVarObject_HEAD_INIT(NULL, 0)
@@ -58,17 +59,9 @@
py_base_new
};
-PyMethodDef BL_ArmatureConstraint::Methods[] = {
- {NULL,NULL} //Sentinel
-};
-
-PyAttributeDef BL_ArmatureConstraint::Attributes[] = {
- { NULL } //Sentinel
-};
-
PyObject* BL_ArmatureConstraint::py_repr(void)
{
- return PyUnicode_FromFormat("%s:%s", m_posechannel->name, m_constraint->name);
+ return PyUnicode_FromString(m_name);
}
BL_ArmatureConstraint::BL_ArmatureConstraint(
@@ -98,6 +91,7 @@
m_target->RegisterObject(m_armature);
if (m_subtarget)
m_subtarget->RegisterObject(m_armature);
+ BLI_snprintf(m_name, sizeof(m_name), "%s:%s", m_posechannel->name, m_constraint->name);
}
BL_ArmatureConstraint::~BL_ArmatureConstraint()
@@ -217,7 +211,7 @@
return (!strcmp(m_posechannel->name, posechannel) && !strcmp(m_constraint->name, constraint));
}
-void BL_ArmatureConstraint::SetTarget(KX_GameObject* target, KX_GameObject* subtarget)
+void BL_ArmatureConstraint::SetTarget(KX_GameObject* target)
{
if (m_blendtarget) {
if (target != m_target) {
@@ -229,3 +223,225 @@
}
}
+
+void BL_ArmatureConstraint::SetSubtarget(KX_GameObject* subtarget)
+{
+ if (m_blendsubtarget) {
+ if (subtarget != m_subtarget) {
+ m_subtarget->UnregisterObject(m_armature);
+ m_subtarget = subtarget;
+ if (m_subtarget)
+ m_subtarget->RegisterObject(m_armature);
+ }
+ }
+
+}
+
+// PYTHON
+
+PyMethodDef BL_ArmatureConstraint::Methods[] = {
+ {NULL,NULL} //Sentinel
+};
+
+// order of definition of attributes, must match Attributes[] array
+#define BCA_TYPE 0
+#define BCA_NAME 1
+#define BCA_ENFORCE 2
+#define BCA_HEADTAIL 3
+#define BCA_LINERROR 4
+#define BCA_ROTERROR 5
+#define BCA_TARGET 6
+#define BCA_SUBTARGET 7
+#define BCA_ACTIVE 8
+#define BCA_IKWEIGHT 9
+#define BCA_IKTYPE 10
+#define BCA_IKFLAG 11
+#define BCA_IKDIST 12
+#define BCA_IKMODE 13
+
+PyAttributeDef BL_ArmatureConstraint::Attributes[] = {
+ // Keep these attributes in order of BCA_ defines!!! used by py_attr_getattr and py_attr_setattr
+ KX_PYATTRIBUTE_RO_FUNCTION("type",BL_ArmatureConstraint,py_attr_getattr),
+ KX_PYATTRIBUTE_RO_FUNCTION("name",BL_ArmatureConstraint,py_attr_getattr),
+ KX_PYATTRIBUTE_RW_FUNCTION("enforce",BL_ArmatureConstraint,py_attr_getattr,py_attr_setattr),
+ KX_PYATTRIBUTE_RW_FUNCTION("headtail",BL_ArmatureConstraint,py_attr_getattr,py_attr_setattr),
+ KX_PYATTRIBUTE_RO_FUNCTION("lin_error",BL_ArmatureConstraint,py_attr_getattr),
+ KX_PYATTRIBUTE_RO_FUNCTION("rot_error",BL_ArmatureConstraint,py_attr_getattr),
+ KX_PYATTRIBUTE_RW_FUNCTION("target",BL_ArmatureConstraint,py_attr_getattr,py_attr_setattr),
+ KX_PYATTRIBUTE_RW_FUNCTION("subtarget",BL_ArmatureConstraint,py_attr_getattr,py_attr_setattr),
+ KX_PYATTRIBUTE_RW_FUNCTION("active",BL_ArmatureConstraint,py_attr_getattr,py_attr_setattr),
+ KX_PYATTRIBUTE_RW_FUNCTION("ik_weight",BL_ArmatureConstraint,py_attr_getattr,py_attr_setattr),
+ KX_PYATTRIBUTE_RO_FUNCTION("ik_type",BL_ArmatureConstraint,py_attr_getattr),
+ KX_PYATTRIBUTE_RO_FUNCTION("ik_flag",BL_ArmatureConstraint,py_attr_getattr),
+ KX_PYATTRIBUTE_RW_FUNCTION("ik_dist",BL_ArmatureConstraint,py_attr_getattr,py_attr_setattr),
+ KX_PYATTRIBUTE_RW_FUNCTION("ik_mode",BL_ArmatureConstraint,py_attr_getattr,py_attr_setattr),
+
+ { NULL } //Sentinel
+};
+
+
+PyObject* BL_ArmatureConstraint::py_attr_getattr(void *self_v, const struct KX_PYATTRIBUTE_DEF *attrdef)
+{
+ BL_ArmatureConstraint* self= static_cast<BL_ArmatureConstraint*>(self_v);
+ bConstraint* constraint = self->m_constraint;
+ bKinematicConstraint* ikconstraint = (constraint && constraint->type == CONSTRAINT_TYPE_KINEMATIC) ? (bKinematicConstraint*)constraint->data : NULL;
+ int attr_order = attrdef-Attributes;
+
+ if (!constraint) {
+ PyErr_SetString(PyExc_AttributeError, "constraint is NULL");
+ return NULL;
+ }
+
+ switch (attr_order) {
+ case BCA_TYPE:
+ return PyLong_FromLong(constraint->type);
+ case BCA_NAME:
+ return PyUnicode_FromString(constraint->name);
+ case BCA_ENFORCE:
+ return PyFloat_FromDouble(constraint->enforce);
+ case BCA_HEADTAIL:
+ return PyFloat_FromDouble(constraint->headtail);
+ case BCA_LINERROR:
+ return PyFloat_FromDouble(constraint->lin_error);
+ case BCA_ROTERROR:
+ return PyFloat_FromDouble(constraint->rot_error);
+ case BCA_TARGET:
+ if (!self->m_target)
+ Py_RETURN_NONE;
+ else
+ return self->m_target->GetProxy();
+ case BCA_SUBTARGET:
+ if (!self->m_subtarget)
+ Py_RETURN_NONE;
+ else
+ return self->m_subtarget->GetProxy();
+ case BCA_ACTIVE:
+ return PyBool_FromLong(constraint->flag & CONSTRAINT_OFF);
+ case BCA_IKWEIGHT:
+ case BCA_IKTYPE:
+ case BCA_IKFLAG:
+ case BCA_IKDIST:
+ case BCA_IKMODE:
+ if (!ikconstraint) {
+ PyErr_SetString(PyExc_AttributeError, "constraint is not of IK type");
+ return NULL;
+ }
+ switch (attr_order) {
+ case BCA_IKWEIGHT:
+ return PyFloat_FromDouble((ikconstraint)?ikconstraint->weight:0.0);
+ case BCA_IKTYPE:
+ return PyLong_FromLong(ikconstraint->type);
+ case BCA_IKFLAG:
+ return PyLong_FromLong(ikconstraint->flag);
+ case BCA_IKDIST:
+ return PyFloat_FromDouble(ikconstraint->dist);
+ case BCA_IKMODE:
+ return PyLong_FromLong(ikconstraint->mode);
+ }
+ // should not come here
+ break;
+ }
+ PyErr_SetString(PyExc_AttributeError, "constraint unknown attribute");
+ return NULL;
+}
+
+int BL_ArmatureConstraint::py_attr_setattr(void *self_v, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+ BL_ArmatureConstraint* self= static_cast<BL_ArmatureConstraint*>(self_v);
+ bConstraint* constraint = self->m_constraint;
+ bKinematicConstraint* ikconstraint = (constraint && constraint->type == CONSTRAINT_TYPE_KINEMATIC) ? (bKinematicConstraint*)constraint->data : NULL;
+ int attr_order = attrdef-Attributes;
+ int ival;
+ double dval;
+ char* sval;
+ KX_GameObject *oval;
+
+ if (!constraint) {
+ PyErr_SetString(PyExc_AttributeError, "constraint is NULL");
+ return PY_SET_ATTR_FAIL;
+ }
+
+ switch (attr_order) {
+ case BCA_ENFORCE:
+ dval = PyFloat_AsDouble(value);
+ if (dval < 0.0f || dval > 1.0f) { /* also accounts for non float */
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list