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