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