Blender V2.61 - r43446

BL_ArmatureActuator.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "DNA_action_types.h"
00034 #include "DNA_constraint_types.h"
00035 #include "DNA_actuator_types.h"
00036 #include "BKE_constraint.h"
00037 #include "BL_ArmatureActuator.h"
00038 #include "BL_ArmatureObject.h"
00039 #include "BLI_math.h"
00040 
00050 BL_ArmatureActuator::BL_ArmatureActuator(SCA_IObject* obj,
00051                         int type,
00052                         const char *posechannel,
00053                         const char *constraintname,
00054                         KX_GameObject* targetobj,
00055                         KX_GameObject* subtargetobj,
00056                         float weight) :
00057     SCA_IActuator(obj, KX_ACT_ARMATURE),
00058     m_constraint(NULL),
00059     m_gametarget(targetobj),
00060     m_gamesubtarget(subtargetobj),
00061     m_posechannel(posechannel),
00062     m_constraintname(constraintname),
00063     m_weight(weight),
00064     m_type(type)
00065 {
00066     if (m_gametarget)
00067         m_gametarget->RegisterActuator(this);
00068     if (m_gamesubtarget)
00069         m_gamesubtarget->RegisterActuator(this);
00070     FindConstraint();
00071 }
00072 
00073 BL_ArmatureActuator::~BL_ArmatureActuator()
00074 {
00075     if (m_gametarget)
00076         m_gametarget->UnregisterActuator(this);
00077     if (m_gamesubtarget)
00078         m_gamesubtarget->UnregisterActuator(this);
00079 }
00080 
00081 void BL_ArmatureActuator::ProcessReplica()
00082 {
00083     // the replica is tracking the same object => register it (this may be changed in Relnk())
00084     if (m_gametarget)
00085         m_gametarget->RegisterActuator(this);
00086     if (m_gamesubtarget)
00087         m_gamesubtarget->UnregisterActuator(this);
00088     SCA_IActuator::ProcessReplica();
00089 }
00090 
00091 void BL_ArmatureActuator::ReParent(SCA_IObject* parent)
00092 {
00093     SCA_IActuator::ReParent(parent);
00094     // must remap the constraint
00095     FindConstraint();
00096 }
00097 
00098 bool BL_ArmatureActuator::UnlinkObject(SCA_IObject* clientobj)
00099 {
00100     bool res=false;
00101     if (clientobj == m_gametarget)
00102     {
00103         // this object is being deleted, we cannot continue to track it.
00104         m_gametarget = NULL;
00105         res = true;
00106     }
00107     if (clientobj == m_gamesubtarget)
00108     {
00109         // this object is being deleted, we cannot continue to track it.
00110         m_gamesubtarget = NULL;
00111         res = true;
00112     }
00113     return res;
00114 }
00115 
00116 void BL_ArmatureActuator::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
00117 {
00118     void **h_obj = (*obj_map)[m_gametarget];
00119     if (h_obj) {
00120         if (m_gametarget)
00121             m_gametarget->UnregisterActuator(this);
00122         m_gametarget = (KX_GameObject*)(*h_obj);
00123         m_gametarget->RegisterActuator(this);
00124     }
00125     h_obj = (*obj_map)[m_gamesubtarget];
00126     if (h_obj) {
00127         if (m_gamesubtarget)
00128             m_gamesubtarget->UnregisterActuator(this);
00129         m_gamesubtarget = (KX_GameObject*)(*h_obj);
00130         m_gamesubtarget->RegisterActuator(this);
00131     }
00132 }
00133 
00134 void BL_ArmatureActuator::FindConstraint()
00135 {
00136     m_constraint = NULL;
00137 
00138     if (m_gameobj->GetGameObjectType() == SCA_IObject::OBJ_ARMATURE) {
00139         BL_ArmatureObject* armobj = (BL_ArmatureObject*)m_gameobj;
00140         m_constraint = armobj->GetConstraint(m_posechannel, m_constraintname);
00141     }
00142 }
00143 
00144 bool BL_ArmatureActuator::Update(double curtime, bool frame)
00145 {
00146     // the only role of this actuator is to ensure that the armature pose will be evaluated
00147     bool result = false;    
00148     bool bNegativeEvent = IsNegativeEvent();
00149     RemoveAllEvents();
00150 
00151     if (!bNegativeEvent) {
00152         BL_ArmatureObject *obj = (BL_ArmatureObject*)GetParent();
00153         switch (m_type) {
00154         case ACT_ARM_RUN:
00155             result = true;
00156             obj->SetActiveAction(NULL, 0, curtime);
00157             break;
00158         case ACT_ARM_ENABLE:
00159             if (m_constraint)
00160                 m_constraint->ClrConstraintFlag(CONSTRAINT_OFF);
00161             break;
00162         case ACT_ARM_DISABLE:
00163             if (m_constraint)
00164                 m_constraint->SetConstraintFlag(CONSTRAINT_OFF);
00165             break;
00166         case ACT_ARM_SETTARGET:
00167             if (m_constraint) {
00168                 m_constraint->SetTarget(m_gametarget);
00169                 m_constraint->SetSubtarget(m_gamesubtarget);
00170             }
00171             break;
00172         case ACT_ARM_SETWEIGHT:
00173             if (m_constraint)
00174                 m_constraint->SetWeight(m_weight);
00175             break;
00176         }
00177     }
00178     return result;
00179 }
00180 
00181 #ifdef WITH_PYTHON
00182 
00183 /* ------------------------------------------------------------------------- */
00184 /* Python Integration Hooks                                                  */
00185 /* ------------------------------------------------------------------------- */
00186 
00187 PyTypeObject BL_ArmatureActuator::Type = {
00188     PyVarObject_HEAD_INIT(NULL, 0)
00189         "BL_ArmatureActuator",
00190         sizeof(PyObjectPlus_Proxy),
00191         0,
00192         py_base_dealloc,
00193         0,
00194         0,
00195         0,
00196         0,
00197         py_base_repr,
00198         0,0,0,0,0,0,0,0,0,
00199         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00200         0,0,0,0,0,0,0,
00201         Methods,
00202         0,
00203         0,
00204         &SCA_IActuator::Type,
00205         0,0,0,0,0,0,
00206         py_base_new
00207 };
00208 
00209 
00210 PyMethodDef BL_ArmatureActuator::Methods[] = {
00211     {NULL,NULL} //Sentinel
00212 };
00213 
00214 PyAttributeDef BL_ArmatureActuator::Attributes[] = {
00215     KX_PYATTRIBUTE_RO_FUNCTION("constraint", BL_ArmatureActuator, pyattr_get_constraint),
00216     KX_PYATTRIBUTE_RW_FUNCTION("target", BL_ArmatureActuator, pyattr_get_object, pyattr_set_object),
00217     KX_PYATTRIBUTE_RW_FUNCTION("subtarget", BL_ArmatureActuator, pyattr_get_object, pyattr_set_object),
00218     KX_PYATTRIBUTE_FLOAT_RW("weight",0.0f,1.0f,BL_ArmatureActuator,m_weight),
00219     KX_PYATTRIBUTE_INT_RW("type",0,ACT_ARM_MAXTYPE,false,BL_ArmatureActuator,m_type),
00220     { NULL }    //Sentinel
00221 };
00222 
00223 PyObject* BL_ArmatureActuator::pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
00224 {
00225     BL_ArmatureActuator* actuator = static_cast<BL_ArmatureActuator*>(self);
00226     KX_GameObject *target = (!strcmp(attrdef->m_name, "target")) ? actuator->m_gametarget : actuator->m_gamesubtarget;
00227     if (!target)    
00228         Py_RETURN_NONE;
00229     else
00230         return target->GetProxy();
00231 }
00232 
00233 int BL_ArmatureActuator::pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
00234 {
00235     BL_ArmatureActuator* actuator = static_cast<BL_ArmatureActuator*>(self);
00236     KX_GameObject* &target = (!strcmp(attrdef->m_name, "target")) ? actuator->m_gametarget : actuator->m_gamesubtarget;
00237     KX_GameObject *gameobj;
00238         
00239     if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: BL_ArmatureActuator"))
00240         return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
00241         
00242     if (target != NULL)
00243         target->UnregisterActuator(actuator);   
00244 
00245     target = gameobj;
00246         
00247     if (target)
00248         target->RegisterActuator(actuator);
00249         
00250     return PY_SET_ATTR_SUCCESS;
00251 }
00252 
00253 PyObject* BL_ArmatureActuator::pyattr_get_constraint(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
00254 {
00255     BL_ArmatureActuator* actuator = static_cast<BL_ArmatureActuator*>(self);
00256     BL_ArmatureConstraint* constraint = actuator->m_constraint;
00257     if (!constraint)    
00258         Py_RETURN_NONE;
00259     else
00260         return constraint->GetProxy();
00261 }
00262 
00263 #endif // WITH_PYTHON
00264