BGE patch #14386: Action Actuator Current Frame Prop. This patch is very usefull...
[blender.git] / source / gameengine / Converter / BL_ActionActuator.cpp
1 /**
2 * $Id$
3 *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28 */
29
30 #if defined (__sgi)
31 #include <math.h>
32 #else
33 #include <cmath>
34 #endif
35
36 #include "SCA_LogicManager.h"
37 #include "BL_ActionActuator.h"
38 #include "BL_ArmatureObject.h"
39 #include "BL_SkinDeformer.h"
40 #include "KX_GameObject.h"
41 #include "STR_HashedString.h"
42 #include "DNA_action_types.h"
43 #include "DNA_nla_types.h"
44 #include "DNA_actuator_types.h"
45 #include "BKE_action.h"
46 #include "DNA_armature_types.h"
47 #include "MEM_guardedalloc.h"
48 #include "BLI_blenlib.h"
49 #include "BLI_arithb.h"
50 #include "MT_Matrix4x4.h"
51 #include "BKE_utildefines.h"
52 #include "FloatValue.h"
53
54 #ifdef HAVE_CONFIG_H
55 #include <config.h>
56 #endif
57
58 BL_ActionActuator::~BL_ActionActuator()
59 {
60
61         if (m_pose) {
62                 free_pose_channels(m_pose);
63                 MEM_freeN(m_pose);
64                 m_pose = NULL;
65         };
66         
67         if (m_userpose){
68                 free_pose_channels(m_userpose);
69                 MEM_freeN(m_userpose);
70                 m_userpose=NULL;
71         }
72         if (m_blendpose) {
73                 free_pose_channels(m_blendpose);
74                 MEM_freeN(m_blendpose);
75                 m_blendpose = NULL;
76         };
77         
78 }
79
80 void BL_ActionActuator::ProcessReplica(){
81 //      bPose *oldpose = m_pose;
82 //      bPose *oldbpose = m_blendpose;
83         
84         m_pose = NULL;
85         m_blendpose = NULL;
86         m_localtime=m_startframe;
87         m_lastUpdate=-1;
88         
89 }
90
91 void BL_ActionActuator::SetBlendTime (float newtime){
92         m_blendframe = newtime;
93 }
94
95 CValue* BL_ActionActuator::GetReplica() {
96         BL_ActionActuator* replica = new BL_ActionActuator(*this);//m_float,GetName());
97         replica->ProcessReplica();
98         
99         // this will copy properties and so on...
100         CValue::AddDataToReplica(replica);
101         return replica;
102 }
103
104 bool BL_ActionActuator::ClampLocalTime()
105 {
106         if (m_startframe < m_endframe)
107         {
108                 if (m_localtime < m_startframe)
109                 {
110                         m_localtime = m_startframe;
111                         return true;
112                 } 
113                 else if (m_localtime > m_endframe)
114                 {
115                         m_localtime = m_endframe;
116                         return true;
117                 }
118         } else {
119                 if (m_localtime > m_startframe)
120                 {
121                         m_localtime = m_startframe;
122                         return true;
123                 }
124                 else if (m_localtime < m_endframe)
125                 {
126                         m_localtime = m_endframe;
127                         return true;
128                 }
129         }
130         return false;
131 }
132
133 void BL_ActionActuator::SetStartTime(float curtime)
134 {
135         float direction = m_startframe < m_endframe ? 1.0 : -1.0;
136         
137         if (!(m_flag & ACT_FLAG_REVERSE))
138                 m_starttime = curtime - direction*(m_localtime - m_startframe)/KX_KetsjiEngine::GetAnimFrameRate();
139         else
140                 m_starttime = curtime - direction*(m_endframe - m_localtime)/KX_KetsjiEngine::GetAnimFrameRate();
141 }
142
143 void BL_ActionActuator::SetLocalTime(float curtime)
144 {
145         float delta_time = (curtime - m_starttime)*KX_KetsjiEngine::GetAnimFrameRate();
146         
147         if (m_endframe < m_startframe)
148                 delta_time = -delta_time;
149
150         if (!(m_flag & ACT_FLAG_REVERSE))
151                 m_localtime = m_startframe + delta_time;
152         else
153                 m_localtime = m_endframe - delta_time;
154 }
155
156
157 bool BL_ActionActuator::Update(double curtime, bool frame)
158 {
159         bool bNegativeEvent = false;
160         bool bPositiveEvent = false;
161         bool keepgoing = true;
162         bool wrap = false;
163         bool apply=true;
164         int     priority;
165         float newweight;
166         
167         // result = true if animation has to be continued, false if animation stops
168         // maybe there are events for us in the queue !
169         if (frame)
170         {
171                 for (vector<CValue*>::iterator i=m_events.begin(); !(i==m_events.end());i++)
172                 {
173                         if ((*i)->GetNumber() == 0.0f)
174                                 bNegativeEvent = true;
175                         else
176                                 bPositiveEvent= true;
177                         (*i)->Release();
178                 
179                 }
180                 m_events.clear();
181                 
182                 if (bPositiveEvent)
183                         m_flag |= ACT_FLAG_ACTIVE;
184                 
185                 if (bNegativeEvent)
186                 {
187                         if (!(m_flag & ACT_FLAG_ACTIVE))
188                                 return false;
189                         m_flag &= ~ACT_FLAG_ACTIVE;
190                 }
191         }
192         
193         /*      We know that action actuators have been discarded from all non armature objects:
194         if we're being called, we're attached to a BL_ArmatureObject */
195         BL_ArmatureObject *obj = (BL_ArmatureObject*)GetParent();
196         float length = m_endframe - m_startframe;
197         
198         priority = m_priority;
199         
200         /* Determine pre-incrementation behaviour and set appropriate flags */
201         switch (m_playtype){
202         case ACT_ACTION_MOTION:
203                 if (bNegativeEvent){
204                         keepgoing=false;
205                         apply=false;
206                 };
207                 break;
208         case ACT_ACTION_FROM_PROP:
209                 if (bNegativeEvent){
210                         apply=false;
211                         keepgoing=false;
212                 }
213                 break;
214         case ACT_ACTION_LOOP_END:
215                 if (bPositiveEvent){
216                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
217                                 m_flag &= ~ACT_FLAG_KEYUP;
218                                 m_flag &= ~ACT_FLAG_REVERSE;
219                                 m_flag |= ACT_FLAG_LOCKINPUT;
220                                 m_localtime = m_startframe;
221                                 m_starttime = curtime;
222                         }
223                 }
224                 if (bNegativeEvent){
225                         m_flag |= ACT_FLAG_KEYUP;
226                 }
227                 break;
228         case ACT_ACTION_LOOP_STOP:
229                 if (bPositiveEvent){
230                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
231                                 m_flag &= ~ACT_FLAG_REVERSE;
232                                 m_flag &= ~ACT_FLAG_KEYUP;
233                                 m_flag |= ACT_FLAG_LOCKINPUT;
234                                 SetStartTime(curtime);
235                         }
236                 }
237                 if (bNegativeEvent){
238                         m_flag |= ACT_FLAG_KEYUP;
239                         m_flag &= ~ACT_FLAG_LOCKINPUT;
240                         keepgoing=false;
241                         apply=false;
242                 }
243                 break;
244         case ACT_ACTION_FLIPPER:
245                 if (bPositiveEvent){
246                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
247                                 m_flag &= ~ACT_FLAG_REVERSE;
248                                 m_flag |= ACT_FLAG_LOCKINPUT;
249                                 SetStartTime(curtime);
250                         }
251                 }
252                 else if (bNegativeEvent){
253                         m_flag |= ACT_FLAG_REVERSE;
254                         m_flag &= ~ACT_FLAG_LOCKINPUT;
255                         SetStartTime(curtime);
256                 }
257                 break;
258         case ACT_ACTION_PLAY:
259                 if (bPositiveEvent){
260                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
261                                 m_flag &= ~ACT_FLAG_REVERSE;
262                                 m_localtime = m_starttime;
263                                 m_starttime = curtime;
264                                 m_flag |= ACT_FLAG_LOCKINPUT;
265                         }
266                 }
267                 break;
268         default:
269                 break;
270         }
271         
272         /* Perform increment */
273         if (keepgoing){
274                 if (m_playtype == ACT_ACTION_MOTION){
275                         MT_Point3       newpos;
276                         MT_Point3       deltapos;
277                         
278                         newpos = obj->NodeGetWorldPosition();
279                         
280                         /* Find displacement */
281                         deltapos = newpos-m_lastpos;
282                         m_localtime += (length/m_stridelength) * deltapos.length();
283                         m_lastpos = newpos;
284                 }
285                 else{
286                         SetLocalTime(curtime);
287                 }
288         }
289         
290         /* Check if a wrapping response is needed */
291         if (length){
292                 if (m_localtime < m_startframe || m_localtime > m_endframe)
293                 {
294                         m_localtime = m_startframe + fmod(m_localtime, length);
295                         wrap = true;
296                 }
297         }
298         else
299                 m_localtime = m_startframe;
300         
301         /* Perform post-increment tasks */
302         switch (m_playtype){
303         case ACT_ACTION_FROM_PROP:
304                 {
305                         CValue* propval = GetParent()->GetProperty(m_propname);
306                         if (propval)
307                                 m_localtime = propval->GetNumber();
308                         
309                         if (bNegativeEvent){
310                                 keepgoing=false;
311                         }
312                 }
313                 break;
314         case ACT_ACTION_MOTION:
315                 break;
316         case ACT_ACTION_LOOP_STOP:
317                 break;
318         case ACT_ACTION_FLIPPER:
319                 if (wrap){
320                         if (!(m_flag & ACT_FLAG_REVERSE)){
321                                 m_localtime=m_endframe;
322                                 //keepgoing = false;
323                         }
324                         else {
325                                 m_localtime=m_startframe;
326                                 keepgoing = false;
327                         }
328                 }
329                 break;
330         case ACT_ACTION_LOOP_END:
331                 if (wrap){
332                         if (m_flag & ACT_FLAG_KEYUP){
333                                 keepgoing = false;
334                                 m_localtime = m_endframe;
335                                 m_flag &= ~ACT_FLAG_LOCKINPUT;
336                         }
337                         SetStartTime(curtime);
338                 }
339                 break;
340         case ACT_ACTION_PLAY:
341                 if (wrap){
342                         m_localtime = m_endframe;
343                         keepgoing = false;
344                         m_flag &= ~ACT_FLAG_LOCKINPUT;
345                 }
346                 break;
347         default:
348                 keepgoing = false;
349                 break;
350         }
351         
352         /* Set the property if its defined */
353         if (m_framepropname) {
354                 CValue* propowner = GetParent();
355                 CValue* oldprop = propowner->GetProperty(m_framepropname);
356                 CValue* newval = new CFloatValue(m_localtime);
357                 if (oldprop) {
358                         oldprop->SetValue(newval);
359                 } else {
360                         propowner->SetProperty(m_framepropname, newval);
361                 }
362                 newval->Release();
363         }
364         
365         if (bNegativeEvent)
366                 m_blendframe=0.0;
367         
368         /* Apply the pose if necessary*/
369         if (apply){
370
371                 /* Priority test */
372                 if (obj->SetActiveAction(this, priority, curtime)){
373                         
374                         /* Get the underlying pose from the armature */
375                         obj->GetPose(&m_pose);
376                         
377                         /* Override the necessary channels with ones from the action */
378                         extract_pose_from_action(m_pose, m_action, m_localtime);
379
380                         /* Perform the user override (if any) */
381                         if (m_userpose){
382                                 extract_pose_from_pose(m_pose, m_userpose);
383 //                              clear_pose(m_userpose);
384                                 MEM_freeN(m_userpose);
385                                 m_userpose = NULL;
386                         }
387 #if 1
388                         /* Handle blending */
389                         if (m_blendin && (m_blendframe<m_blendin)){
390                                 /* If this is the start of a blending sequence... */
391                                 if ((m_blendframe==0.0) || (!m_blendpose)){
392                                         obj->GetMRDPose(&m_blendpose);
393                                         m_blendstart = curtime;
394                                 }
395                                 
396                                 /* Find percentages */
397                                 newweight = (m_blendframe/(float)m_blendin);
398                                 blend_poses(m_pose, m_blendpose, 1.0 - newweight, ACTSTRIPMODE_BLEND);
399
400                                 /* Increment current blending percentage */
401                                 m_blendframe = (curtime - m_blendstart)*KX_KetsjiEngine::GetAnimFrameRate();
402                                 if (m_blendframe>m_blendin)
403                                         m_blendframe = m_blendin;
404                                 
405                         }
406 #endif
407                         m_lastUpdate = m_localtime;
408                         obj->SetPose (m_pose);
409                 }
410                 else{
411                         m_blendframe = 0.0;
412                 }
413         }
414         
415         if (!keepgoing){
416                 m_blendframe = 0.0;
417         }
418         return keepgoing;
419 };
420
421 /* ------------------------------------------------------------------------- */
422 /* Python functions                                                          */
423 /* ------------------------------------------------------------------------- */
424
425 /* Integration hooks ------------------------------------------------------- */
426
427 PyTypeObject BL_ActionActuator::Type = {
428         PyObject_HEAD_INIT(&PyType_Type)
429                 0,
430                 "BL_ActionActuator",
431                 sizeof(BL_ActionActuator),
432                 0,
433                 PyDestructor,
434                 0,
435                 __getattr,
436                 __setattr,
437                 0, //&MyPyCompare,
438                 __repr,
439                 0, //&cvalue_as_number,
440                 0,
441                 0,
442                 0,
443                 0
444 };
445
446 PyParentObject BL_ActionActuator::Parents[] = {
447         &BL_ActionActuator::Type,
448                 &SCA_IActuator::Type,
449                 &SCA_ILogicBrick::Type,
450                 &CValue::Type,
451                 NULL
452 };
453
454 PyMethodDef BL_ActionActuator::Methods[] = {
455         {"setAction", (PyCFunction) BL_ActionActuator::sPySetAction, METH_VARARGS, SetAction_doc},
456         {"setStart", (PyCFunction) BL_ActionActuator::sPySetStart, METH_VARARGS, SetStart_doc},
457         {"setEnd", (PyCFunction) BL_ActionActuator::sPySetEnd, METH_VARARGS, SetEnd_doc},
458         {"setBlendin", (PyCFunction) BL_ActionActuator::sPySetBlendin, METH_VARARGS, SetBlendin_doc},
459         {"setPriority", (PyCFunction) BL_ActionActuator::sPySetPriority, METH_VARARGS, SetPriority_doc},
460         {"setFrame", (PyCFunction) BL_ActionActuator::sPySetFrame, METH_VARARGS, SetFrame_doc},
461         {"setProperty", (PyCFunction) BL_ActionActuator::sPySetProperty, METH_VARARGS, SetProperty_doc},
462         {"setFrameProperty", (PyCFunction) BL_ActionActuator::sPySetFrameProperty, METH_VARARGS, SetFrameProperty_doc},
463         {"setBlendtime", (PyCFunction) BL_ActionActuator::sPySetBlendtime, METH_VARARGS, SetBlendtime_doc},
464
465         {"getAction", (PyCFunction) BL_ActionActuator::sPyGetAction, METH_VARARGS, GetAction_doc},
466         {"getStart", (PyCFunction) BL_ActionActuator::sPyGetStart, METH_VARARGS, GetStart_doc},
467         {"getEnd", (PyCFunction) BL_ActionActuator::sPyGetEnd, METH_VARARGS, GetEnd_doc},
468         {"getBlendin", (PyCFunction) BL_ActionActuator::sPyGetBlendin, METH_VARARGS, GetBlendin_doc},
469         {"getPriority", (PyCFunction) BL_ActionActuator::sPyGetPriority, METH_VARARGS, GetPriority_doc},
470         {"getFrame", (PyCFunction) BL_ActionActuator::sPyGetFrame, METH_VARARGS, GetFrame_doc},
471         {"getProperty", (PyCFunction) BL_ActionActuator::sPyGetProperty, METH_VARARGS, GetProperty_doc},
472         {"getFrameProperty", (PyCFunction) BL_ActionActuator::sPyGetFrameProperty, METH_VARARGS, GetFrameProperty_doc},
473         {"setChannel", (PyCFunction) BL_ActionActuator::sPySetChannel, METH_VARARGS, SetChannel_doc},
474 //      {"getChannel", (PyCFunction) BL_ActionActuator::sPyGetChannel, METH_VARARGS},
475         {"getType", (PyCFunction) BL_ActionActuator::sPyGetType, METH_VARARGS, GetType_doc},    
476         {"setType", (PyCFunction) BL_ActionActuator::sPySetType, METH_VARARGS, SetType_doc},
477         {NULL,NULL} //Sentinel
478 };
479
480 PyObject* BL_ActionActuator::_getattr(const STR_String& attr) {
481         _getattr_up(SCA_IActuator);
482 }
483
484 /*     setStart                                                              */
485 char BL_ActionActuator::GetAction_doc[] = 
486 "getAction()\n"
487 "\tReturns a string containing the name of the current action.\n";
488
489 PyObject* BL_ActionActuator::PyGetAction(PyObject* self, 
490                                                                                  PyObject* args, 
491                                                                                  PyObject* kwds) {
492         PyObject *result;
493         
494         if (m_action){
495                 result = Py_BuildValue("s", m_action->id.name+2);
496         }
497         else{
498                 Py_INCREF(Py_None);
499                 result = Py_None;
500         }
501         
502         return result;
503 }
504
505 /*     getProperty                                                             */
506 char BL_ActionActuator::GetProperty_doc[] = 
507 "getProperty()\n"
508 "\tReturns the name of the property to be used in FromProp mode.\n";
509
510 PyObject* BL_ActionActuator::PyGetProperty(PyObject* self, 
511                                                                                    PyObject* args, 
512                                                                                    PyObject* kwds) {
513         PyObject *result;
514         
515         result = Py_BuildValue("s", (const char *)m_propname);
516         
517         return result;
518 }
519
520 /*     getProperty                                                             */
521 char BL_ActionActuator::GetFrameProperty_doc[] = 
522 "getFrameProperty()\n"
523 "\tReturns the name of the property, that is set to the current frame number.\n";
524
525 PyObject* BL_ActionActuator::PyGetFrameProperty(PyObject* self, 
526                                                                                    PyObject* args, 
527                                                                                    PyObject* kwds) {
528         PyObject *result;
529         
530         result = Py_BuildValue("s", (const char *)m_framepropname);
531         
532         return result;
533 }
534
535 /*     getFrame                                                              */
536 char BL_ActionActuator::GetFrame_doc[] = 
537 "getFrame()\n"
538 "\tReturns the current frame number.\n";
539
540 PyObject* BL_ActionActuator::PyGetFrame(PyObject* self, 
541                                                                                 PyObject* args, 
542                                                                                 PyObject* kwds) {
543         PyObject *result;
544         
545         result = Py_BuildValue("f", m_localtime);
546         
547         return result;
548 }
549
550 /*     getEnd                                                                */
551 char BL_ActionActuator::GetEnd_doc[] = 
552 "getEnd()\n"
553 "\tReturns the last frame of the action.\n";
554
555 PyObject* BL_ActionActuator::PyGetEnd(PyObject* self, 
556                                                                           PyObject* args, 
557                                                                           PyObject* kwds) {
558         PyObject *result;
559         
560         result = Py_BuildValue("f", m_endframe);
561         
562         return result;
563 }
564
565 /*     getStart                                                              */
566 char BL_ActionActuator::GetStart_doc[] = 
567 "getStart()\n"
568 "\tReturns the starting frame of the action.\n";
569
570 PyObject* BL_ActionActuator::PyGetStart(PyObject* self, 
571                                                                                 PyObject* args, 
572                                                                                 PyObject* kwds) {
573         PyObject *result;
574         
575         result = Py_BuildValue("f", m_startframe);
576         
577         return result;
578 }
579
580 /*     getBlendin                                                            */
581 char BL_ActionActuator::GetBlendin_doc[] = 
582 "getBlendin()\n"
583 "\tReturns the number of interpolation animation frames to be\n"
584 "\tgenerated when this actuator is triggered.\n";
585
586 PyObject* BL_ActionActuator::PyGetBlendin(PyObject* self, 
587                                                                                   PyObject* args, 
588                                                                                   PyObject* kwds) {
589         PyObject *result;
590         
591         result = Py_BuildValue("f", m_blendin);
592         
593         return result;
594 }
595
596 /*     getPriority                                                           */
597 char BL_ActionActuator::GetPriority_doc[] = 
598 "getPriority()\n"
599 "\tReturns the priority for this actuator.  Actuators with lower\n"
600 "\tPriority numbers will override actuators with higher numbers.\n";
601
602 PyObject* BL_ActionActuator::PyGetPriority(PyObject* self, 
603                                                                                    PyObject* args, 
604                                                                                    PyObject* kwds) {
605         PyObject *result;
606         
607         result = Py_BuildValue("i", m_priority);
608         
609         return result;
610 }
611
612 /*     setAction                                                             */
613 char BL_ActionActuator::SetAction_doc[] = 
614 "setAction(action, (reset))\n"
615 "\t - action    : The name of the action to set as the current action.\n"
616 "\t - reset     : Optional parameter indicating whether to reset the\n"
617 "\t               blend timer or not.  A value of 1 indicates that the\n"
618 "\t               timer should be reset.  A value of 0 will leave it\n"
619 "\t               unchanged.  If reset is not specified, the timer will"
620 "\t                   be reset.\n";
621
622 PyObject* BL_ActionActuator::PySetAction(PyObject* self, 
623                                                                                  PyObject* args, 
624                                                                                  PyObject* kwds) {
625         char *string;
626         int     reset = 1;
627
628         if (PyArg_ParseTuple(args,"s|i",&string, &reset))
629         {
630                 bAction *action;
631                 
632                 action = (bAction*)SCA_ILogicBrick::m_sCurrentLogicManager->GetActionByName(STR_String(string));
633                 
634                 if (!action){
635                         /* NOTE!  Throw an exception or something */
636                         //                      printf ("setAction failed: Action not found\n", string);
637                 }
638                 else{
639                         m_action=action;
640                         if (reset)
641                                 m_blendframe = 0;
642                 }
643         }
644         
645         Py_INCREF(Py_None);
646         return Py_None;
647 }
648
649 /*     setStart                                                              */
650 char BL_ActionActuator::SetStart_doc[] = 
651 "setStart(start)\n"
652 "\t - start     : Specifies the starting frame of the animation.\n";
653
654 PyObject* BL_ActionActuator::PySetStart(PyObject* self, 
655                                                                                 PyObject* args, 
656                                                                                 PyObject* kwds) {
657         float start;
658         
659         if (PyArg_ParseTuple(args,"f",&start))
660         {
661                 m_startframe = start;
662         }
663         
664         Py_INCREF(Py_None);
665         return Py_None;
666 }
667
668 /*     setEnd                                                                */
669 char BL_ActionActuator::SetEnd_doc[] = 
670 "setEnd(end)\n"
671 "\t - end       : Specifies the ending frame of the animation.\n";
672
673 PyObject* BL_ActionActuator::PySetEnd(PyObject* self, 
674                                                                           PyObject* args, 
675                                                                           PyObject* kwds) {
676         float end;
677         
678         if (PyArg_ParseTuple(args,"f",&end))
679         {
680                 m_endframe = end;
681         }
682         
683         Py_INCREF(Py_None);
684         return Py_None;
685 }
686
687 /*     setBlendin                                                            */
688 char BL_ActionActuator::SetBlendin_doc[] = 
689 "setBlendin(blendin)\n"
690 "\t - blendin   : Specifies the number of frames of animation to generate\n"
691 "\t               when making transitions between actions.\n";
692
693 PyObject* BL_ActionActuator::PySetBlendin(PyObject* self, 
694                                                                                   PyObject* args, 
695                                                                                   PyObject* kwds) {
696         float blendin;
697         
698         if (PyArg_ParseTuple(args,"f",&blendin))
699         {
700                 m_blendin = blendin;
701         }
702         
703         Py_INCREF(Py_None);
704         return Py_None;
705 }
706
707 /*     setBlendtime                                                          */
708 char BL_ActionActuator::SetBlendtime_doc[] = 
709 "setBlendtime(blendtime)\n"
710 "\t - blendtime : Allows the script to directly modify the internal timer\n"
711 "\t               used when generating transitions between actions.  This\n"
712 "\t               parameter must be in the range from 0.0 to 1.0.\n";
713
714 PyObject* BL_ActionActuator::PySetBlendtime(PyObject* self, 
715                                                                                   PyObject* args, 
716                                                                                   PyObject* kwds) {
717         float blendframe;
718         
719         if (PyArg_ParseTuple(args,"f",&blendframe))
720         {
721                 m_blendframe = blendframe * m_blendin;
722                 if (m_blendframe<0)
723                         m_blendframe = 0;
724                 if (m_blendframe>m_blendin)
725                         m_blendframe = m_blendin;
726         }
727         
728         Py_INCREF(Py_None);
729         return Py_None;
730 }
731
732 /*     setPriority                                                           */
733 char BL_ActionActuator::SetPriority_doc[] = 
734 "setPriority(priority)\n"
735 "\t - priority  : Specifies the new priority.  Actuators will lower\n"
736 "\t               priority numbers will override actuators with higher\n"
737 "\t               numbers.\n";
738
739 PyObject* BL_ActionActuator::PySetPriority(PyObject* self, 
740                                                                                    PyObject* args, 
741                                                                                    PyObject* kwds) {
742         int priority;
743         
744         if (PyArg_ParseTuple(args,"i",&priority))
745         {
746                 m_priority = priority;
747         }
748         
749         Py_INCREF(Py_None);
750         return Py_None;
751 }
752
753 /*     setFrame                                                              */
754 char BL_ActionActuator::SetFrame_doc[] = 
755 "setFrame(frame)\n"
756 "\t - frame     : Specifies the new current frame for the animation\n";
757
758 PyObject* BL_ActionActuator::PySetFrame(PyObject* self, 
759                                                                                 PyObject* args, 
760                                                                                 PyObject* kwds) {
761         float frame;
762         
763         if (PyArg_ParseTuple(args,"f",&frame))
764         {
765                 m_localtime = frame;
766                 if (m_localtime<m_startframe)
767                         m_localtime=m_startframe;
768                 else if (m_localtime>m_endframe)
769                         m_localtime=m_endframe;
770         }
771         
772         Py_INCREF(Py_None);
773         return Py_None;
774 }
775
776 /*     setProperty                                                           */
777 char BL_ActionActuator::SetProperty_doc[] = 
778 "setProperty(prop)\n"
779 "\t - prop      : A string specifying the property name to be used in\n"
780 "\t               FromProp playback mode.\n";
781
782 PyObject* BL_ActionActuator::PySetProperty(PyObject* self, 
783                                                                                    PyObject* args, 
784                                                                                    PyObject* kwds) {
785         char *string;
786         
787         if (PyArg_ParseTuple(args,"s",&string))
788         {
789                 m_propname = string;
790         }
791         
792         Py_INCREF(Py_None);
793         return Py_None;
794 }
795
796 /*     setFrameProperty                                                          */
797 char BL_ActionActuator::SetFrameProperty_doc[] = 
798 "setFrameProperty(prop)\n"
799 "\t - prop      : A string specifying the property of the frame set up update.\n";
800
801 PyObject* BL_ActionActuator::PySetFrameProperty(PyObject* self, 
802                                                                                    PyObject* args, 
803                                                                                    PyObject* kwds) {
804         char *string;
805         
806         if (PyArg_ParseTuple(args,"s",&string))
807         {
808                 m_framepropname = string;
809         }
810         
811         Py_INCREF(Py_None);
812         return Py_None;
813 }
814
815 /*
816 PyObject* BL_ActionActuator::PyGetChannel(PyObject* self, 
817                                                                                    PyObject* args, 
818                                                                                    PyObject* kwds) {
819         char *string;
820         
821         if (PyArg_ParseTuple(args,"s",&string))
822         {
823                 m_propname = string;
824         }
825         
826         Py_INCREF(Py_None);
827         return Py_None;
828 }
829 */
830
831 /*     setChannel                                                            */
832 char BL_ActionActuator::SetChannel_doc[] = 
833 "setChannel(channel, matrix)\n"
834 "\t - channel   : A string specifying the name of the bone channel.\n"
835 "\t - matrix    : A 4x4 matrix specifying the overriding transformation\n"
836 "\t               as an offset from the bone's rest position.\n";
837
838 PyObject* BL_ActionActuator::PySetChannel(PyObject* self, 
839                                                                                    PyObject* args, 
840                                                                                    PyObject* kwds) 
841 {
842         float matrix[4][4];
843         char *string;
844         PyObject* pylist;
845         bool    error = false;
846         int row,col;
847         int     mode = 0;       /* 0 for bone space, 1 for armature/world space */
848         
849         PyArg_ParseTuple(args,"sO|i", &string, &pylist, &mode);
850         
851         if (pylist->ob_type == &CListValue::Type)
852         {
853                 CListValue* listval = (CListValue*) pylist;
854                 if (listval->GetCount() == 4)
855                 {
856                         for (row=0;row<4;row++) // each row has a 4-vector [x,y,z, w]
857                         {
858                                 CListValue* vecval = (CListValue*)listval->GetValue(row);
859                                 for (col=0;col<4;col++)
860                                 {
861                                         matrix[row][col] = vecval->GetValue(col)->GetNumber();
862                                         
863                                 }
864                         }
865                 }
866                 else
867                 {
868                         error = true;
869                 }
870         }
871         else
872         {
873                 // assert the list is long enough...
874                 int numitems = PyList_Size(pylist);
875                 if (numitems == 4)
876                 {
877                         for (row=0;row<4;row++) // each row has a 4-vector [x,y,z, w]
878                         {
879                                 
880                                 PyObject* veclist = PyList_GetItem(pylist,row); // here we have a vector4 list
881                                 for (col=0;col<4;col++)
882                                 {
883                                         matrix[row][col] =  PyFloat_AsDouble(PyList_GetItem(veclist,col));
884                                         
885                                 }
886                         }
887                 }
888                 else
889                 {
890                         error = true;
891                 }
892         }
893         
894         if (!error)
895         {
896
897 /*      DO IT HERE */
898                 bPoseChannel *pchan= verify_pose_channel(m_userpose, string);
899
900                 Mat4ToQuat(matrix, pchan->quat);
901                 Mat4ToSize(matrix, pchan->size);
902                 VECCOPY (pchan->loc, matrix[3]);
903                 
904                 pchan->flag |= POSE_ROT|POSE_LOC|POSE_SIZE;
905
906                 if (!m_userpose){
907                         m_userpose = (bPose*)MEM_callocN(sizeof(bPose), "userPose");
908                 }
909         }
910         
911         Py_INCREF(Py_None);
912         return Py_None;
913 }
914
915 /* getType */
916 char BL_ActionActuator::GetType_doc[] =
917 "getType()\n"
918 "\tReturns the operation mode of the actuator.\n";
919 PyObject* BL_ActionActuator::PyGetType(PyObject* self,
920                                        PyObject* args, 
921                                        PyObject* kwds) {
922     return Py_BuildValue("h", m_playtype);
923 }
924
925 /* setType */
926 char BL_ActionActuator::SetType_doc[] =
927 "setType(mode)\n"
928 "\t - mode: Play (0), Flipper (2), LoopStop (3), LoopEnd (4) or Property (6)\n"
929 "\tSet the operation mode of the actuator.\n";
930 PyObject* BL_ActionActuator::PySetType(PyObject* self,
931                                        PyObject* args,
932                                        PyObject* kwds) {
933         short typeArg;
934                                                                                                              
935     if (!PyArg_ParseTuple(args, "h", &typeArg)) {
936         return NULL;
937     }
938
939         switch (typeArg) {
940         case KX_ACT_ACTION_PLAY:
941         case KX_ACT_ACTION_FLIPPER:
942         case KX_ACT_ACTION_LOOPSTOP:
943         case KX_ACT_ACTION_LOOPEND:
944         case KX_ACT_ACTION_PROPERTY:
945                 m_playtype = typeArg;
946                 break;
947         default:
948                 printf("Invalid type for action actuator: %d\n", typeArg); /* error */
949     }
950         
951     Py_Return;
952 }
953