Merged 15170:15635 from trunk (no conflicts or even merges)
[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                         // dont continue where we left off when restarting
188                         if (m_end_reset) {
189                                 m_flag &= ~ACT_FLAG_LOCKINPUT;
190                         }
191                         
192                         if (!(m_flag & ACT_FLAG_ACTIVE))
193                                 return false;
194                         m_flag &= ~ACT_FLAG_ACTIVE;
195                 }
196         }
197         
198         /*      We know that action actuators have been discarded from all non armature objects:
199         if we're being called, we're attached to a BL_ArmatureObject */
200         BL_ArmatureObject *obj = (BL_ArmatureObject*)GetParent();
201         float length = m_endframe - m_startframe;
202         
203         priority = m_priority;
204         
205         /* Determine pre-incrementation behaviour and set appropriate flags */
206         switch (m_playtype){
207         case ACT_ACTION_MOTION:
208                 if (bNegativeEvent){
209                         keepgoing=false;
210                         apply=false;
211                 };
212                 break;
213         case ACT_ACTION_FROM_PROP:
214                 if (bNegativeEvent){
215                         apply=false;
216                         keepgoing=false;
217                 }
218                 break;
219         case ACT_ACTION_LOOP_END:
220                 if (bPositiveEvent){
221                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
222                                 m_flag &= ~ACT_FLAG_KEYUP;
223                                 m_flag &= ~ACT_FLAG_REVERSE;
224                                 m_flag |= ACT_FLAG_LOCKINPUT;
225                                 m_localtime = m_startframe;
226                                 m_starttime = curtime;
227                         }
228                 }
229                 if (bNegativeEvent){
230                         m_flag |= ACT_FLAG_KEYUP;
231                 }
232                 break;
233         case ACT_ACTION_LOOP_STOP:
234                 if (bPositiveEvent){
235                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
236                                 m_flag &= ~ACT_FLAG_REVERSE;
237                                 m_flag &= ~ACT_FLAG_KEYUP;
238                                 m_flag |= ACT_FLAG_LOCKINPUT;
239                                 SetStartTime(curtime);
240                         }
241                 }
242                 if (bNegativeEvent){
243                         m_flag |= ACT_FLAG_KEYUP;
244                         m_flag &= ~ACT_FLAG_LOCKINPUT;
245                         keepgoing=false;
246                         apply=false;
247                 }
248                 break;
249         case ACT_ACTION_FLIPPER:
250                 if (bPositiveEvent){
251                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
252                                 m_flag &= ~ACT_FLAG_REVERSE;
253                                 m_flag |= ACT_FLAG_LOCKINPUT;
254                                 SetStartTime(curtime);
255                         }
256                 }
257                 else if (bNegativeEvent){
258                         m_flag |= ACT_FLAG_REVERSE;
259                         m_flag &= ~ACT_FLAG_LOCKINPUT;
260                         SetStartTime(curtime);
261                 }
262                 break;
263         case ACT_ACTION_PLAY:
264                 if (bPositiveEvent){
265                         if (!(m_flag & ACT_FLAG_LOCKINPUT)){
266                                 m_flag &= ~ACT_FLAG_REVERSE;
267                                 m_localtime = m_starttime;
268                                 m_starttime = curtime;
269                                 m_flag |= ACT_FLAG_LOCKINPUT;
270                         }
271                 }
272                 break;
273         default:
274                 break;
275         }
276         
277         /* Perform increment */
278         if (keepgoing){
279                 if (m_playtype == ACT_ACTION_MOTION){
280                         MT_Point3       newpos;
281                         MT_Point3       deltapos;
282                         
283                         newpos = obj->NodeGetWorldPosition();
284                         
285                         /* Find displacement */
286                         deltapos = newpos-m_lastpos;
287                         m_localtime += (length/m_stridelength) * deltapos.length();
288                         m_lastpos = newpos;
289                 }
290                 else{
291                         SetLocalTime(curtime);
292                 }
293         }
294         
295         /* Check if a wrapping response is needed */
296         if (length){
297                 if (m_localtime < m_startframe || m_localtime > m_endframe)
298                 {
299                         m_localtime = m_startframe + fmod(m_localtime, length);
300                         wrap = true;
301                 }
302         }
303         else
304                 m_localtime = m_startframe;
305         
306         /* Perform post-increment tasks */
307         switch (m_playtype){
308         case ACT_ACTION_FROM_PROP:
309                 {
310                         CValue* propval = GetParent()->GetProperty(m_propname);
311                         if (propval)
312                                 m_localtime = propval->GetNumber();
313                         
314                         if (bNegativeEvent){
315                                 keepgoing=false;
316                         }
317                 }
318                 break;
319         case ACT_ACTION_MOTION:
320                 break;
321         case ACT_ACTION_LOOP_STOP:
322                 break;
323         case ACT_ACTION_FLIPPER:
324                 if (wrap){
325                         if (!(m_flag & ACT_FLAG_REVERSE)){
326                                 m_localtime=m_endframe;
327                                 //keepgoing = false;
328                         }
329                         else {
330                                 m_localtime=m_startframe;
331                                 keepgoing = false;
332                         }
333                 }
334                 break;
335         case ACT_ACTION_LOOP_END:
336                 if (wrap){
337                         if (m_flag & ACT_FLAG_KEYUP){
338                                 keepgoing = false;
339                                 m_localtime = m_endframe;
340                                 m_flag &= ~ACT_FLAG_LOCKINPUT;
341                         }
342                         SetStartTime(curtime);
343                 }
344                 break;
345         case ACT_ACTION_PLAY:
346                 if (wrap){
347                         m_localtime = m_endframe;
348                         keepgoing = false;
349                         m_flag &= ~ACT_FLAG_LOCKINPUT;
350                 }
351                 break;
352         default:
353                 keepgoing = false;
354                 break;
355         }
356         
357         /* Set the property if its defined */
358         if (m_framepropname[0] != '\0') {
359                 CValue* propowner = GetParent();
360                 CValue* oldprop = propowner->GetProperty(m_framepropname);
361                 CValue* newval = new CFloatValue(m_localtime);
362                 if (oldprop) {
363                         oldprop->SetValue(newval);
364                 } else {
365                         propowner->SetProperty(m_framepropname, newval);
366                 }
367                 newval->Release();
368         }
369         
370         if (bNegativeEvent)
371                 m_blendframe=0.0;
372         
373         /* Apply the pose if necessary*/
374         if (apply){
375
376                 /* Priority test */
377                 if (obj->SetActiveAction(this, priority, curtime)){
378                         
379                         /* Get the underlying pose from the armature */
380                         obj->GetPose(&m_pose);
381                         
382                         /* Override the necessary channels with ones from the action */
383                         extract_pose_from_action(m_pose, m_action, m_localtime);
384
385                         /* Perform the user override (if any) */
386                         if (m_userpose){
387                                 extract_pose_from_pose(m_pose, m_userpose);
388 //                              clear_pose(m_userpose);
389                                 MEM_freeN(m_userpose);
390                                 m_userpose = NULL;
391                         }
392 #if 1
393                         /* Handle blending */
394                         if (m_blendin && (m_blendframe<m_blendin)){
395                                 /* If this is the start of a blending sequence... */
396                                 if ((m_blendframe==0.0) || (!m_blendpose)){
397                                         obj->GetMRDPose(&m_blendpose);
398                                         m_blendstart = curtime;
399                                 }
400                                 
401                                 /* Find percentages */
402                                 newweight = (m_blendframe/(float)m_blendin);
403                                 blend_poses(m_pose, m_blendpose, 1.0 - newweight, ACTSTRIPMODE_BLEND);
404
405                                 /* Increment current blending percentage */
406                                 m_blendframe = (curtime - m_blendstart)*KX_KetsjiEngine::GetAnimFrameRate();
407                                 if (m_blendframe>m_blendin)
408                                         m_blendframe = m_blendin;
409                                 
410                         }
411 #endif
412                         m_lastUpdate = m_localtime;
413                         obj->SetPose (m_pose);
414                 }
415                 else{
416                         m_blendframe = 0.0;
417                 }
418         }
419         
420         if (!keepgoing){
421                 m_blendframe = 0.0;
422         }
423         return keepgoing;
424 };
425
426 /* ------------------------------------------------------------------------- */
427 /* Python functions                                                          */
428 /* ------------------------------------------------------------------------- */
429
430 /* Integration hooks ------------------------------------------------------- */
431
432 PyTypeObject BL_ActionActuator::Type = {
433         PyObject_HEAD_INIT(&PyType_Type)
434                 0,
435                 "BL_ActionActuator",
436                 sizeof(BL_ActionActuator),
437                 0,
438                 PyDestructor,
439                 0,
440                 __getattr,
441                 __setattr,
442                 0, //&MyPyCompare,
443                 __repr,
444                 0, //&cvalue_as_number,
445                 0,
446                 0,
447                 0,
448                 0
449 };
450
451 PyParentObject BL_ActionActuator::Parents[] = {
452         &BL_ActionActuator::Type,
453                 &SCA_IActuator::Type,
454                 &SCA_ILogicBrick::Type,
455                 &CValue::Type,
456                 NULL
457 };
458
459 PyMethodDef BL_ActionActuator::Methods[] = {
460         {"setAction", (PyCFunction) BL_ActionActuator::sPySetAction, METH_VARARGS, SetAction_doc},
461         {"setStart", (PyCFunction) BL_ActionActuator::sPySetStart, METH_VARARGS, SetStart_doc},
462         {"setEnd", (PyCFunction) BL_ActionActuator::sPySetEnd, METH_VARARGS, SetEnd_doc},
463         {"setBlendin", (PyCFunction) BL_ActionActuator::sPySetBlendin, METH_VARARGS, SetBlendin_doc},
464         {"setPriority", (PyCFunction) BL_ActionActuator::sPySetPriority, METH_VARARGS, SetPriority_doc},
465         {"setFrame", (PyCFunction) BL_ActionActuator::sPySetFrame, METH_VARARGS, SetFrame_doc},
466         {"setProperty", (PyCFunction) BL_ActionActuator::sPySetProperty, METH_VARARGS, SetProperty_doc},
467         {"setFrameProperty", (PyCFunction) BL_ActionActuator::sPySetFrameProperty, METH_VARARGS, SetFrameProperty_doc},
468         {"setBlendtime", (PyCFunction) BL_ActionActuator::sPySetBlendtime, METH_VARARGS, SetBlendtime_doc},
469
470         {"getAction", (PyCFunction) BL_ActionActuator::sPyGetAction, METH_VARARGS, GetAction_doc},
471         {"getStart", (PyCFunction) BL_ActionActuator::sPyGetStart, METH_VARARGS, GetStart_doc},
472         {"getEnd", (PyCFunction) BL_ActionActuator::sPyGetEnd, METH_VARARGS, GetEnd_doc},
473         {"getBlendin", (PyCFunction) BL_ActionActuator::sPyGetBlendin, METH_VARARGS, GetBlendin_doc},
474         {"getPriority", (PyCFunction) BL_ActionActuator::sPyGetPriority, METH_VARARGS, GetPriority_doc},
475         {"getFrame", (PyCFunction) BL_ActionActuator::sPyGetFrame, METH_VARARGS, GetFrame_doc},
476         {"getProperty", (PyCFunction) BL_ActionActuator::sPyGetProperty, METH_VARARGS, GetProperty_doc},
477         {"getFrameProperty", (PyCFunction) BL_ActionActuator::sPyGetFrameProperty, METH_VARARGS, GetFrameProperty_doc},
478         {"setChannel", (PyCFunction) BL_ActionActuator::sPySetChannel, METH_VARARGS, SetChannel_doc},
479 //      {"getChannel", (PyCFunction) BL_ActionActuator::sPyGetChannel, METH_VARARGS},
480         {"getType", (PyCFunction) BL_ActionActuator::sPyGetType, METH_VARARGS, GetType_doc},
481         {"setType", (PyCFunction) BL_ActionActuator::sPySetType, METH_VARARGS, SetType_doc},
482         {"getContinue", (PyCFunction) BL_ActionActuator::sPyGetContinue, METH_NOARGS, 0},       
483         {"setContinue", (PyCFunction) BL_ActionActuator::sPySetContinue, METH_O, 0},
484         {NULL,NULL} //Sentinel
485 };
486
487 PyObject* BL_ActionActuator::_getattr(const STR_String& attr) {
488         _getattr_up(SCA_IActuator);
489 }
490
491 /*     setStart                                                              */
492 char BL_ActionActuator::GetAction_doc[] = 
493 "getAction()\n"
494 "\tReturns a string containing the name of the current action.\n";
495
496 PyObject* BL_ActionActuator::PyGetAction(PyObject* self, 
497                                                                                  PyObject* args, 
498                                                                                  PyObject* kwds) {
499         PyObject *result;
500         
501         if (m_action){
502                 result = Py_BuildValue("s", m_action->id.name+2);
503         }
504         else{
505                 Py_INCREF(Py_None);
506                 result = Py_None;
507         }
508         
509         return result;
510 }
511
512 /*     getProperty                                                             */
513 char BL_ActionActuator::GetProperty_doc[] = 
514 "getProperty()\n"
515 "\tReturns the name of the property to be used in FromProp mode.\n";
516
517 PyObject* BL_ActionActuator::PyGetProperty(PyObject* self, 
518                                                                                    PyObject* args, 
519                                                                                    PyObject* kwds) {
520         PyObject *result;
521         
522         result = Py_BuildValue("s", (const char *)m_propname);
523         
524         return result;
525 }
526
527 /*     getProperty                                                             */
528 char BL_ActionActuator::GetFrameProperty_doc[] = 
529 "getFrameProperty()\n"
530 "\tReturns the name of the property, that is set to the current frame number.\n";
531
532 PyObject* BL_ActionActuator::PyGetFrameProperty(PyObject* self, 
533                                                                                    PyObject* args, 
534                                                                                    PyObject* kwds) {
535         PyObject *result;
536         
537         result = Py_BuildValue("s", (const char *)m_framepropname);
538         
539         return result;
540 }
541
542 /*     getFrame                                                              */
543 char BL_ActionActuator::GetFrame_doc[] = 
544 "getFrame()\n"
545 "\tReturns the current frame number.\n";
546
547 PyObject* BL_ActionActuator::PyGetFrame(PyObject* self, 
548                                                                                 PyObject* args, 
549                                                                                 PyObject* kwds) {
550         PyObject *result;
551         
552         result = Py_BuildValue("f", m_localtime);
553         
554         return result;
555 }
556
557 /*     getEnd                                                                */
558 char BL_ActionActuator::GetEnd_doc[] = 
559 "getEnd()\n"
560 "\tReturns the last frame of the action.\n";
561
562 PyObject* BL_ActionActuator::PyGetEnd(PyObject* self, 
563                                                                           PyObject* args, 
564                                                                           PyObject* kwds) {
565         PyObject *result;
566         
567         result = Py_BuildValue("f", m_endframe);
568         
569         return result;
570 }
571
572 /*     getStart                                                              */
573 char BL_ActionActuator::GetStart_doc[] = 
574 "getStart()\n"
575 "\tReturns the starting frame of the action.\n";
576
577 PyObject* BL_ActionActuator::PyGetStart(PyObject* self, 
578                                                                                 PyObject* args, 
579                                                                                 PyObject* kwds) {
580         PyObject *result;
581         
582         result = Py_BuildValue("f", m_startframe);
583         
584         return result;
585 }
586
587 /*     getBlendin                                                            */
588 char BL_ActionActuator::GetBlendin_doc[] = 
589 "getBlendin()\n"
590 "\tReturns the number of interpolation animation frames to be\n"
591 "\tgenerated when this actuator is triggered.\n";
592
593 PyObject* BL_ActionActuator::PyGetBlendin(PyObject* self, 
594                                                                                   PyObject* args, 
595                                                                                   PyObject* kwds) {
596         PyObject *result;
597         
598         result = Py_BuildValue("f", m_blendin);
599         
600         return result;
601 }
602
603 /*     getPriority                                                           */
604 char BL_ActionActuator::GetPriority_doc[] = 
605 "getPriority()\n"
606 "\tReturns the priority for this actuator.  Actuators with lower\n"
607 "\tPriority numbers will override actuators with higher numbers.\n";
608
609 PyObject* BL_ActionActuator::PyGetPriority(PyObject* self, 
610                                                                                    PyObject* args, 
611                                                                                    PyObject* kwds) {
612         PyObject *result;
613         
614         result = Py_BuildValue("i", m_priority);
615         
616         return result;
617 }
618
619 /*     setAction                                                             */
620 char BL_ActionActuator::SetAction_doc[] = 
621 "setAction(action, (reset))\n"
622 "\t - action    : The name of the action to set as the current action.\n"
623 "\t - reset     : Optional parameter indicating whether to reset the\n"
624 "\t               blend timer or not.  A value of 1 indicates that the\n"
625 "\t               timer should be reset.  A value of 0 will leave it\n"
626 "\t               unchanged.  If reset is not specified, the timer will"
627 "\t                   be reset.\n";
628
629 PyObject* BL_ActionActuator::PySetAction(PyObject* self, 
630                                                                                  PyObject* args, 
631                                                                                  PyObject* kwds) {
632         char *string;
633         int     reset = 1;
634
635         if (PyArg_ParseTuple(args,"s|i",&string, &reset))
636         {
637                 bAction *action;
638                 
639                 action = (bAction*)SCA_ILogicBrick::m_sCurrentLogicManager->GetActionByName(STR_String(string));
640                 
641                 if (!action){
642                         /* NOTE!  Throw an exception or something */
643                         //                      printf ("setAction failed: Action not found\n", string);
644                 }
645                 else{
646                         m_action=action;
647                         if (reset)
648                                 m_blendframe = 0;
649                 }
650         }
651         else {
652                 return NULL;
653         }
654         
655         Py_INCREF(Py_None);
656         return Py_None;
657 }
658
659 /*     setStart                                                              */
660 char BL_ActionActuator::SetStart_doc[] = 
661 "setStart(start)\n"
662 "\t - start     : Specifies the starting frame of the animation.\n";
663
664 PyObject* BL_ActionActuator::PySetStart(PyObject* self, 
665                                                                                 PyObject* args, 
666                                                                                 PyObject* kwds) {
667         float start;
668         
669         if (PyArg_ParseTuple(args,"f",&start))
670         {
671                 m_startframe = start;
672         }
673         else {
674                 return NULL;
675         }
676         
677         Py_INCREF(Py_None);
678         return Py_None;
679 }
680
681 /*     setEnd                                                                */
682 char BL_ActionActuator::SetEnd_doc[] = 
683 "setEnd(end)\n"
684 "\t - end       : Specifies the ending frame of the animation.\n";
685
686 PyObject* BL_ActionActuator::PySetEnd(PyObject* self, 
687                                                                           PyObject* args, 
688                                                                           PyObject* kwds) {
689         float end;
690         
691         if (PyArg_ParseTuple(args,"f",&end))
692         {
693                 m_endframe = end;
694         }
695         else {
696                 return NULL;
697         }
698         
699         Py_INCREF(Py_None);
700         return Py_None;
701 }
702
703 /*     setBlendin                                                            */
704 char BL_ActionActuator::SetBlendin_doc[] = 
705 "setBlendin(blendin)\n"
706 "\t - blendin   : Specifies the number of frames of animation to generate\n"
707 "\t               when making transitions between actions.\n";
708
709 PyObject* BL_ActionActuator::PySetBlendin(PyObject* self, 
710                                                                                   PyObject* args, 
711                                                                                   PyObject* kwds) {
712         float blendin;
713         
714         if (PyArg_ParseTuple(args,"f",&blendin))
715         {
716                 m_blendin = blendin;
717         }
718         else {
719                 return NULL;
720         }
721         
722         Py_INCREF(Py_None);
723         return Py_None;
724 }
725
726 /*     setBlendtime                                                          */
727 char BL_ActionActuator::SetBlendtime_doc[] = 
728 "setBlendtime(blendtime)\n"
729 "\t - blendtime : Allows the script to directly modify the internal timer\n"
730 "\t               used when generating transitions between actions.  This\n"
731 "\t               parameter must be in the range from 0.0 to 1.0.\n";
732
733 PyObject* BL_ActionActuator::PySetBlendtime(PyObject* self, 
734                                                                                   PyObject* args, 
735                                                                                   PyObject* kwds) {
736         float blendframe;
737         
738         if (PyArg_ParseTuple(args,"f",&blendframe))
739         {
740                 m_blendframe = blendframe * m_blendin;
741                 if (m_blendframe<0)
742                         m_blendframe = 0;
743                 if (m_blendframe>m_blendin)
744                         m_blendframe = m_blendin;
745         }
746         else {
747                 return NULL;
748         }
749         
750         Py_INCREF(Py_None);
751         return Py_None;
752 }
753
754 /*     setPriority                                                           */
755 char BL_ActionActuator::SetPriority_doc[] = 
756 "setPriority(priority)\n"
757 "\t - priority  : Specifies the new priority.  Actuators will lower\n"
758 "\t               priority numbers will override actuators with higher\n"
759 "\t               numbers.\n";
760
761 PyObject* BL_ActionActuator::PySetPriority(PyObject* self, 
762                                                                                    PyObject* args, 
763                                                                                    PyObject* kwds) {
764         int priority;
765         
766         if (PyArg_ParseTuple(args,"i",&priority))
767         {
768                 m_priority = priority;
769         }
770         else {
771                 return NULL;
772         }
773         
774         Py_INCREF(Py_None);
775         return Py_None;
776 }
777
778 /*     setFrame                                                              */
779 char BL_ActionActuator::SetFrame_doc[] = 
780 "setFrame(frame)\n"
781 "\t - frame     : Specifies the new current frame for the animation\n";
782
783 PyObject* BL_ActionActuator::PySetFrame(PyObject* self, 
784                                                                                 PyObject* args, 
785                                                                                 PyObject* kwds) {
786         float frame;
787         
788         if (PyArg_ParseTuple(args,"f",&frame))
789         {
790                 m_localtime = frame;
791                 if (m_localtime<m_startframe)
792                         m_localtime=m_startframe;
793                 else if (m_localtime>m_endframe)
794                         m_localtime=m_endframe;
795         }
796         else {
797                 return NULL;
798         }
799         
800         Py_INCREF(Py_None);
801         return Py_None;
802 }
803
804 /*     setProperty                                                           */
805 char BL_ActionActuator::SetProperty_doc[] = 
806 "setProperty(prop)\n"
807 "\t - prop      : A string specifying the property name to be used in\n"
808 "\t               FromProp playback mode.\n";
809
810 PyObject* BL_ActionActuator::PySetProperty(PyObject* self, 
811                                                                                    PyObject* args, 
812                                                                                    PyObject* kwds) {
813         char *string;
814         
815         if (PyArg_ParseTuple(args,"s",&string))
816         {
817                 m_propname = string;
818         }
819         else {
820                 return NULL;
821         }
822         
823         Py_INCREF(Py_None);
824         return Py_None;
825 }
826
827 /*     setFrameProperty                                                          */
828 char BL_ActionActuator::SetFrameProperty_doc[] = 
829 "setFrameProperty(prop)\n"
830 "\t - prop      : A string specifying the property of the frame set up update.\n";
831
832 PyObject* BL_ActionActuator::PySetFrameProperty(PyObject* self, 
833                                                                                    PyObject* args, 
834                                                                                    PyObject* kwds) {
835         char *string;
836         
837         if (PyArg_ParseTuple(args,"s",&string))
838         {
839                 m_framepropname = string;
840         }
841         else {
842                 return NULL;
843         }
844         
845         Py_INCREF(Py_None);
846         return Py_None;
847 }
848
849 /*
850 PyObject* BL_ActionActuator::PyGetChannel(PyObject* self, 
851                                                                                    PyObject* args, 
852                                                                                    PyObject* kwds) {
853         char *string;
854         
855         if (PyArg_ParseTuple(args,"s",&string))
856         {
857                 m_propname = string;
858         }
859         else {
860                 return NULL;
861         }
862         
863         Py_INCREF(Py_None);
864         return Py_None;
865 }
866 */
867
868 /*     setChannel                                                            */
869 char BL_ActionActuator::SetChannel_doc[] = 
870 "setChannel(channel, matrix)\n"
871 "\t - channel   : A string specifying the name of the bone channel.\n"
872 "\t - matrix    : A 4x4 matrix specifying the overriding transformation\n"
873 "\t               as an offset from the bone's rest position.\n";
874
875 PyObject* BL_ActionActuator::PySetChannel(PyObject* self, 
876                                                                                    PyObject* args, 
877                                                                                    PyObject* kwds) 
878 {
879         float matrix[4][4];
880         char *string;
881         PyObject* pylist;
882         bool    error = false;
883         int row,col;
884         int     mode = 0;       /* 0 for bone space, 1 for armature/world space */
885         
886         if (!PyArg_ParseTuple(args,"sO|i", &string, &pylist, &mode))
887                 return NULL;
888         
889         if (pylist->ob_type == &CListValue::Type)
890         {
891                 CListValue* listval = (CListValue*) pylist;
892                 if (listval->GetCount() == 4)
893                 {
894                         for (row=0;row<4;row++) // each row has a 4-vector [x,y,z, w]
895                         {
896                                 CListValue* vecval = (CListValue*)listval->GetValue(row);
897                                 for (col=0;col<4;col++)
898                                 {
899                                         matrix[row][col] = vecval->GetValue(col)->GetNumber();
900                                         
901                                 }
902                         }
903                 }
904                 else
905                 {
906                         error = true;
907                 }
908         }
909         else
910         {
911                 // assert the list is long enough...
912                 int numitems = PyList_Size(pylist);
913                 if (numitems == 4)
914                 {
915                         for (row=0;row<4;row++) // each row has a 4-vector [x,y,z, w]
916                         {
917                                 
918                                 PyObject* veclist = PyList_GetItem(pylist,row); // here we have a vector4 list
919                                 for (col=0;col<4;col++)
920                                 {
921                                         matrix[row][col] =  PyFloat_AsDouble(PyList_GetItem(veclist,col));
922                                         
923                                 }
924                         }
925                 }
926                 else
927                 {
928                         error = true;
929                 }
930         }
931         
932         if (!error)
933         {
934
935 /*      DO IT HERE */
936                 bPoseChannel *pchan= verify_pose_channel(m_userpose, string);
937
938                 Mat4ToQuat(matrix, pchan->quat);
939                 Mat4ToSize(matrix, pchan->size);
940                 VECCOPY (pchan->loc, matrix[3]);
941                 
942                 pchan->flag |= POSE_ROT|POSE_LOC|POSE_SIZE;
943
944                 if (!m_userpose){
945                         m_userpose = (bPose*)MEM_callocN(sizeof(bPose), "userPose");
946                 }
947         }
948         
949         Py_INCREF(Py_None);
950         return Py_None;
951 }
952
953 /* getType */
954 char BL_ActionActuator::GetType_doc[] =
955 "getType()\n"
956 "\tReturns the operation mode of the actuator.\n";
957 PyObject* BL_ActionActuator::PyGetType(PyObject* self,
958                                        PyObject* args, 
959                                        PyObject* kwds) {
960     return Py_BuildValue("h", m_playtype);
961 }
962
963 /* setType */
964 char BL_ActionActuator::SetType_doc[] =
965 "setType(mode)\n"
966 "\t - mode: Play (0), Flipper (2), LoopStop (3), LoopEnd (4) or Property (6)\n"
967 "\tSet the operation mode of the actuator.\n";
968 PyObject* BL_ActionActuator::PySetType(PyObject* self,
969                                        PyObject* args,
970                                        PyObject* kwds) {
971         short typeArg;
972                                                                                                              
973     if (!PyArg_ParseTuple(args, "h", &typeArg)) {
974         return NULL;
975     }
976
977         switch (typeArg) {
978         case KX_ACT_ACTION_PLAY:
979         case KX_ACT_ACTION_FLIPPER:
980         case KX_ACT_ACTION_LOOPSTOP:
981         case KX_ACT_ACTION_LOOPEND:
982         case KX_ACT_ACTION_PROPERTY:
983                 m_playtype = typeArg;
984                 break;
985         default:
986                 printf("Invalid type for action actuator: %d\n", typeArg); /* error */
987     }
988         Py_RETURN_NONE;
989 }
990
991 PyObject* BL_ActionActuator::PyGetContinue(PyObject* self) {
992     return PyInt_FromLong((long)(m_end_reset==0));
993 }
994
995 PyObject* BL_ActionActuator::PySetContinue(PyObject* self, PyObject* value) {
996         int param = PyObject_IsTrue( value );
997         
998         if( param == -1 ) {
999                 PyErr_SetString( PyExc_TypeError, "expected True/False or 0/1" );
1000                 return NULL;
1001         }
1002
1003         if (param) {
1004                 m_end_reset = 0;
1005         } else {
1006                 m_end_reset = 1;
1007         }
1008     Py_RETURN_NONE;
1009 }
1010