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