BGE Python API
[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_nla_types.h"
43 #include "BKE_action.h"
44 #include "DNA_action_types.h"
45 #include "DNA_armature_types.h"
46 #include "MEM_guardedalloc.h"
47 #include "BLI_blenlib.h"
48 #include "BLI_arithb.h"
49 #include "MT_Matrix4x4.h"
50 #include "BKE_utildefines.h"
51 #include "FloatValue.h"
52 #include "PyObjectPlus.h"
53 #include "blendef.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 /*     setStart                                                              */
422 const char BL_ActionActuator::GetAction_doc[] = 
423 "getAction()\n"
424 "\tReturns a string containing the name of the current action.\n";
425
426 PyObject* BL_ActionActuator::PyGetAction(PyObject* self, 
427                                                                                  PyObject* args, 
428                                                                                  PyObject* kwds) {
429         ShowDeprecationWarning("getAction()", "the action property");
430
431         if (m_action){
432                 return PyString_FromString(m_action->id.name+2);
433         }
434         Py_RETURN_NONE;
435 }
436
437 /*     getProperty                                                             */
438 const char BL_ActionActuator::GetProperty_doc[] = 
439 "getProperty()\n"
440 "\tReturns the name of the property to be used in FromProp mode.\n";
441
442 PyObject* BL_ActionActuator::PyGetProperty(PyObject* self, 
443                                                                                    PyObject* args, 
444                                                                                    PyObject* kwds) {
445         ShowDeprecationWarning("getProperty()", "the property property");
446
447         PyObject *result;
448         
449         result = Py_BuildValue("s", (const char *)m_propname);
450         
451         return result;
452 }
453
454 /*     getProperty                                                             */
455 const char BL_ActionActuator::GetFrameProperty_doc[] = 
456 "getFrameProperty()\n"
457 "\tReturns the name of the property, that is set to the current frame number.\n";
458
459 PyObject* BL_ActionActuator::PyGetFrameProperty(PyObject* self, 
460                                                                                    PyObject* args, 
461                                                                                    PyObject* kwds) {
462         ShowDeprecationWarning("getFrameProperty()", "the frameProperty property");
463
464         PyObject *result;
465         
466         result = Py_BuildValue("s", (const char *)m_framepropname);
467         
468         return result;
469 }
470
471 /*     getFrame                                                              */
472 const char BL_ActionActuator::GetFrame_doc[] = 
473 "getFrame()\n"
474 "\tReturns the current frame number.\n";
475
476 PyObject* BL_ActionActuator::PyGetFrame(PyObject* self, 
477                                                                                 PyObject* args, 
478                                                                                 PyObject* kwds) {
479         ShowDeprecationWarning("getFrame()", "the frame property");
480
481         PyObject *result;
482         
483         result = Py_BuildValue("f", m_localtime);
484         
485         return result;
486 }
487
488 /*     getEnd                                                                */
489 const char BL_ActionActuator::GetEnd_doc[] = 
490 "getEnd()\n"
491 "\tReturns the last frame of the action.\n";
492
493 PyObject* BL_ActionActuator::PyGetEnd(PyObject* self, 
494                                                                           PyObject* args, 
495                                                                           PyObject* kwds) {
496         ShowDeprecationWarning("getEnd()", "the end property");
497
498         PyObject *result;
499         
500         result = Py_BuildValue("f", m_endframe);
501         
502         return result;
503 }
504
505 /*     getStart                                                              */
506 const char BL_ActionActuator::GetStart_doc[] = 
507 "getStart()\n"
508 "\tReturns the starting frame of the action.\n";
509
510 PyObject* BL_ActionActuator::PyGetStart(PyObject* self, 
511                                                                                 PyObject* args, 
512                                                                                 PyObject* kwds) {
513         ShowDeprecationWarning("getStart()", "the start property");
514
515         PyObject *result;
516         
517         result = Py_BuildValue("f", m_startframe);
518         
519         return result;
520 }
521
522 /*     getBlendin                                                            */
523 const char BL_ActionActuator::GetBlendin_doc[] = 
524 "getBlendin()\n"
525 "\tReturns the number of interpolation animation frames to be\n"
526 "\tgenerated when this actuator is triggered.\n";
527
528 PyObject* BL_ActionActuator::PyGetBlendin(PyObject* self, 
529                                                                                   PyObject* args, 
530                                                                                   PyObject* kwds) {
531         ShowDeprecationWarning("getBlendin()", "the blendin property");
532
533         PyObject *result;
534         
535         result = Py_BuildValue("f", m_blendin);
536         
537         return result;
538 }
539
540 /*     getPriority                                                           */
541 const char BL_ActionActuator::GetPriority_doc[] = 
542 "getPriority()\n"
543 "\tReturns the priority for this actuator.  Actuators with lower\n"
544 "\tPriority numbers will override actuators with higher numbers.\n";
545
546 PyObject* BL_ActionActuator::PyGetPriority(PyObject* self, 
547                                                                                    PyObject* args, 
548                                                                                    PyObject* kwds) {
549         ShowDeprecationWarning("getPriority()", "the priority property");
550
551         PyObject *result;
552         
553         result = Py_BuildValue("i", m_priority);
554         
555         return result;
556 }
557
558 /*     setAction                                                             */
559 const char BL_ActionActuator::SetAction_doc[] = 
560 "setAction(action, (reset))\n"
561 "\t - action    : The name of the action to set as the current action.\n"
562 "\t - reset     : Optional parameter indicating whether to reset the\n"
563 "\t               blend timer or not.  A value of 1 indicates that the\n"
564 "\t               timer should be reset.  A value of 0 will leave it\n"
565 "\t               unchanged.  If reset is not specified, the timer will"
566 "\t                   be reset.\n";
567
568 PyObject* BL_ActionActuator::PySetAction(PyObject* self, 
569                                                                                  PyObject* args, 
570                                                                                  PyObject* kwds) {
571         ShowDeprecationWarning("setAction()", "the action property");
572
573         char *string;
574         int     reset = 1;
575
576         if (PyArg_ParseTuple(args,"s|i",&string, &reset))
577         {
578                 bAction *action;
579                 
580                 action = (bAction*)SCA_ILogicBrick::m_sCurrentLogicManager->GetActionByName(STR_String(string));
581                 
582                 if (!action){
583                         /* NOTE!  Throw an exception or something */
584                         //                      printf ("setAction failed: Action not found\n", string);
585                 }
586                 else{
587                         m_action=action;
588                         if (reset)
589                                 m_blendframe = 0;
590                 }
591         }
592         else {
593                 return NULL;
594         }
595         
596         Py_RETURN_NONE;
597 }
598
599 /*     setStart                                                              */
600 const char BL_ActionActuator::SetStart_doc[] = 
601 "setStart(start)\n"
602 "\t - start     : Specifies the starting frame of the animation.\n";
603
604 PyObject* BL_ActionActuator::PySetStart(PyObject* self, 
605                                                                                 PyObject* args, 
606                                                                                 PyObject* kwds) {
607         ShowDeprecationWarning("setStart()", "the start property");
608
609         float start;
610         
611         if (PyArg_ParseTuple(args,"f",&start))
612         {
613                 m_startframe = start;
614         }
615         else {
616                 return NULL;
617         }
618         
619         Py_RETURN_NONE;
620 }
621
622 /*     setEnd                                                                */
623 const char BL_ActionActuator::SetEnd_doc[] = 
624 "setEnd(end)\n"
625 "\t - end       : Specifies the ending frame of the animation.\n";
626
627 PyObject* BL_ActionActuator::PySetEnd(PyObject* self, 
628                                                                           PyObject* args, 
629                                                                           PyObject* kwds) {
630         ShowDeprecationWarning("setEnd()", "the end property");
631
632         float end;
633         
634         if (PyArg_ParseTuple(args,"f",&end))
635         {
636                 m_endframe = end;
637         }
638         else {
639                 return NULL;
640         }
641         
642         Py_RETURN_NONE;
643 }
644
645 /*     setBlendin                                                            */
646 const char BL_ActionActuator::SetBlendin_doc[] = 
647 "setBlendin(blendin)\n"
648 "\t - blendin   : Specifies the number of frames of animation to generate\n"
649 "\t               when making transitions between actions.\n";
650
651 PyObject* BL_ActionActuator::PySetBlendin(PyObject* self, 
652                                                                                   PyObject* args, 
653                                                                                   PyObject* kwds) {
654         ShowDeprecationWarning("setBlendin()", "the blendin property");
655
656         float blendin;
657         
658         if (PyArg_ParseTuple(args,"f",&blendin))
659         {
660                 m_blendin = blendin;
661         }
662         else {
663                 return NULL;
664         }
665         
666         Py_RETURN_NONE;
667 }
668
669 /*     setBlendtime                                                          */
670 const char BL_ActionActuator::SetBlendtime_doc[] = 
671 "setBlendtime(blendtime)\n"
672 "\t - blendtime : Allows the script to directly modify the internal timer\n"
673 "\t               used when generating transitions between actions.  This\n"
674 "\t               parameter must be in the range from 0.0 to 1.0.\n";
675
676 PyObject* BL_ActionActuator::PySetBlendtime(PyObject* self, 
677                                                                                   PyObject* args, 
678                                                                                   PyObject* kwds) {
679         ShowDeprecationWarning("setBlendtime()", "the blendtime property");
680
681         float blendframe;
682         
683         if (PyArg_ParseTuple(args,"f",&blendframe))
684         {
685                 m_blendframe = blendframe * m_blendin;
686                 if (m_blendframe<0)
687                         m_blendframe = 0;
688                 if (m_blendframe>m_blendin)
689                         m_blendframe = m_blendin;
690         }
691         else {
692                 return NULL;
693         }
694         
695         Py_RETURN_NONE;
696 }
697
698 /*     setPriority                                                           */
699 const char BL_ActionActuator::SetPriority_doc[] = 
700 "setPriority(priority)\n"
701 "\t - priority  : Specifies the new priority.  Actuators will lower\n"
702 "\t               priority numbers will override actuators with higher\n"
703 "\t               numbers.\n";
704
705 PyObject* BL_ActionActuator::PySetPriority(PyObject* self, 
706                                                                                    PyObject* args, 
707                                                                                    PyObject* kwds) {
708         ShowDeprecationWarning("setPriority()", "the priority property");
709
710         int priority;
711         
712         if (PyArg_ParseTuple(args,"i",&priority))
713         {
714                 m_priority = priority;
715         }
716         else {
717                 return NULL;
718         }
719         
720         Py_RETURN_NONE;
721 }
722
723 /*     setFrame                                                              */
724 const 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         ShowDeprecationWarning("setFrame()", "the frame property");
732
733         float frame;
734         
735         if (PyArg_ParseTuple(args,"f",&frame))
736         {
737                 m_localtime = frame;
738                 if (m_localtime<m_startframe)
739                         m_localtime=m_startframe;
740                 else if (m_localtime>m_endframe)
741                         m_localtime=m_endframe;
742         }
743         else {
744                 return NULL;
745         }
746         
747         Py_RETURN_NONE;
748 }
749
750 /*     setProperty                                                           */
751 const char BL_ActionActuator::SetProperty_doc[] = 
752 "setProperty(prop)\n"
753 "\t - prop      : A string specifying the property name to be used in\n"
754 "\t               FromProp playback mode.\n";
755
756 PyObject* BL_ActionActuator::PySetProperty(PyObject* self, 
757                                                                                    PyObject* args, 
758                                                                                    PyObject* kwds) {
759         ShowDeprecationWarning("setProperty()", "the property property");
760
761         char *string;
762         
763         if (PyArg_ParseTuple(args,"s",&string))
764         {
765                 m_propname = string;
766         }
767         else {
768                 return NULL;
769         }
770         
771         Py_RETURN_NONE;
772 }
773
774 /*     setFrameProperty                                                          */
775 const char BL_ActionActuator::SetFrameProperty_doc[] = 
776 "setFrameProperty(prop)\n"
777 "\t - prop      : A string specifying the property of the frame set up update.\n";
778
779 PyObject* BL_ActionActuator::PySetFrameProperty(PyObject* self, 
780                                                                                    PyObject* args, 
781                                                                                    PyObject* kwds) {
782         ShowDeprecationWarning("setFrameProperty()", "the frameProperty property");
783
784         char *string;
785         
786         if (PyArg_ParseTuple(args,"s",&string))
787         {
788                 m_framepropname = string;
789         }
790         else {
791                 return NULL;
792         }
793         
794         Py_RETURN_NONE;
795 }
796
797 /*
798 PyObject* BL_ActionActuator::PyGetChannel(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_RETURN_NONE;
812 }
813 */
814
815 /* getType */
816 const char BL_ActionActuator::GetType_doc[] =
817 "getType()\n"
818 "\tReturns the operation mode of the actuator.\n";
819 PyObject* BL_ActionActuator::PyGetType(PyObject* self,
820                                        PyObject* args, 
821                                        PyObject* kwds) {
822         ShowDeprecationWarning("getType()", "the type property");
823
824     return Py_BuildValue("h", m_playtype);
825 }
826
827 /* setType */
828 const char BL_ActionActuator::SetType_doc[] =
829 "setType(mode)\n"
830 "\t - mode: Play (0), Flipper (2), LoopStop (3), LoopEnd (4) or Property (6)\n"
831 "\tSet the operation mode of the actuator.\n";
832 PyObject* BL_ActionActuator::PySetType(PyObject* self,
833                                        PyObject* args,
834                                        PyObject* kwds) {
835         ShowDeprecationWarning("setType()", "the type property");
836
837         short typeArg;
838                                                                                                              
839     if (!PyArg_ParseTuple(args, "h", &typeArg)) {
840         return NULL;
841     }
842
843         switch (typeArg) {
844         case ACT_ACTION_PLAY:
845         case ACT_ACTION_FLIPPER:
846         case ACT_ACTION_LOOP_STOP:
847         case ACT_ACTION_LOOP_END:
848         case ACT_ACTION_FROM_PROP:
849                 m_playtype = typeArg;
850                 break;
851         default:
852                 printf("Invalid type for action actuator: %d\n", typeArg); /* error */
853     }
854         Py_RETURN_NONE;
855 }
856
857 PyObject* BL_ActionActuator::PyGetContinue(PyObject* self) {
858         ShowDeprecationWarning("getContinue()", "the continue property");
859
860     return PyInt_FromLong((long)(m_end_reset==0));
861 }
862
863 PyObject* BL_ActionActuator::PySetContinue(PyObject* self, PyObject* value) {
864         ShowDeprecationWarning("setContinue()", "the continue property");
865
866         int param = PyObject_IsTrue( value );
867         
868         if( param == -1 ) {
869                 PyErr_SetString( PyExc_TypeError, "expected True/False or 0/1" );
870                 return NULL;
871         }
872
873         if (param) {
874                 m_end_reset = 0;
875         } else {
876                 m_end_reset = 1;
877         }
878     Py_RETURN_NONE;
879 }
880
881 //<-----Deprecated
882
883 /*     setChannel                                                         */
884 KX_PYMETHODDEF_DOC(BL_ActionActuator, setChannel,
885 "setChannel(channel, matrix)\n"
886 "\t - channel   : A string specifying the name of the bone channel.\n"
887 "\t - matrix    : A 4x4 matrix specifying the overriding transformation\n"
888 "\t               as an offset from the bone's rest position.\n")
889 {
890         float matrix[4][4];
891         char *string;
892         PyObject* pylist;
893         bool    error = false;
894         int row,col;
895         int     mode = 0;       /* 0 for bone space, 1 for armature/world space */
896         
897         if (!PyArg_ParseTuple(args,"sO|i", &string, &pylist, &mode))
898                 return NULL;
899         
900         if (pylist->ob_type == &CListValue::Type)
901         {
902                 CListValue* listval = (CListValue*) pylist;
903                 if (listval->GetCount() == 4)
904                 {
905                         for (row=0;row<4;row++) // each row has a 4-vector [x,y,z, w]
906                         {
907                                 CListValue* vecval = (CListValue*)listval->GetValue(row);
908                                 for (col=0;col<4;col++)
909                                 {
910                                         matrix[row][col] = vecval->GetValue(col)->GetNumber();
911                                         
912                                 }
913                         }
914                 }
915                 else
916                 {
917                         error = true;
918                 }
919         }
920         else
921         {
922                 // assert the list is long enough...
923                 int numitems = PyList_Size(pylist);
924                 if (numitems == 4)
925                 {
926                         for (row=0;row<4;row++) // each row has a 4-vector [x,y,z, w]
927                         {
928                                 
929                                 PyObject* veclist = PyList_GetItem(pylist,row); // here we have a vector4 list
930                                 for (col=0;col<4;col++)
931                                 {
932                                         matrix[row][col] =  PyFloat_AsDouble(PyList_GetItem(veclist,col));
933                                         
934                                 }
935                         }
936                 }
937                 else
938                 {
939                         error = true;
940                 }
941         }
942         
943         if (!error)
944         {
945
946 /*      DO IT HERE */
947                 bPoseChannel *pchan= verify_pose_channel(m_userpose, string);
948
949                 Mat4ToQuat(matrix, pchan->quat);
950                 Mat4ToSize(matrix, pchan->size);
951                 VECCOPY (pchan->loc, matrix[3]);
952                 
953                 pchan->flag |= POSE_ROT|POSE_LOC|POSE_SIZE;
954
955                 if (!m_userpose){
956                         m_userpose = (bPose*)MEM_callocN(sizeof(bPose), "userPose");
957                 }
958         }
959         
960         Py_RETURN_NONE;
961 }
962
963 /* ------------------------------------------------------------------------- */
964 /* Python Integration Hooks                                                                      */
965 /* ------------------------------------------------------------------------- */
966
967 PyTypeObject BL_ActionActuator::Type = {
968         PyObject_HEAD_INIT(&PyType_Type)
969                 0,
970                 "BL_ActionActuator",
971                 sizeof(BL_ActionActuator),
972                 0,
973                 PyDestructor,
974                 0,
975                 __getattr,
976                 __setattr,
977                 0, //&MyPyCompare,
978                 __repr,
979                 0, //&cvalue_as_number,
980                 0,
981                 0,
982                 0,
983                 0
984 };
985
986 PyParentObject BL_ActionActuator::Parents[] = {
987         &BL_ActionActuator::Type,
988                 &SCA_IActuator::Type,
989                 &SCA_ILogicBrick::Type,
990                 &CValue::Type,
991                 NULL
992 };
993
994 PyMethodDef BL_ActionActuator::Methods[] = {
995         //Deprecated ----->
996         {"setAction", (PyCFunction) BL_ActionActuator::sPySetAction, METH_VARARGS, (PY_METHODCHAR)SetAction_doc},
997         {"setStart", (PyCFunction) BL_ActionActuator::sPySetStart, METH_VARARGS, (PY_METHODCHAR)SetStart_doc},
998         {"setEnd", (PyCFunction) BL_ActionActuator::sPySetEnd, METH_VARARGS, (PY_METHODCHAR)SetEnd_doc},
999         {"setBlendin", (PyCFunction) BL_ActionActuator::sPySetBlendin, METH_VARARGS, (PY_METHODCHAR)SetBlendin_doc},
1000         {"setPriority", (PyCFunction) BL_ActionActuator::sPySetPriority, METH_VARARGS, (PY_METHODCHAR)SetPriority_doc},
1001         {"setFrame", (PyCFunction) BL_ActionActuator::sPySetFrame, METH_VARARGS, (PY_METHODCHAR)SetFrame_doc},
1002         {"setProperty", (PyCFunction) BL_ActionActuator::sPySetProperty, METH_VARARGS, (PY_METHODCHAR)SetProperty_doc},
1003         {"setFrameProperty", (PyCFunction) BL_ActionActuator::sPySetFrameProperty, METH_VARARGS, (PY_METHODCHAR)SetFrameProperty_doc},
1004         {"setBlendtime", (PyCFunction) BL_ActionActuator::sPySetBlendtime, METH_VARARGS, (PY_METHODCHAR)SetBlendtime_doc},
1005
1006         {"getAction", (PyCFunction) BL_ActionActuator::sPyGetAction, METH_VARARGS, (PY_METHODCHAR)GetAction_doc},
1007         {"getStart", (PyCFunction) BL_ActionActuator::sPyGetStart, METH_VARARGS, (PY_METHODCHAR)GetStart_doc},
1008         {"getEnd", (PyCFunction) BL_ActionActuator::sPyGetEnd, METH_VARARGS, (PY_METHODCHAR)GetEnd_doc},
1009         {"getBlendin", (PyCFunction) BL_ActionActuator::sPyGetBlendin, METH_VARARGS, (PY_METHODCHAR)GetBlendin_doc},
1010         {"getPriority", (PyCFunction) BL_ActionActuator::sPyGetPriority, METH_VARARGS, (PY_METHODCHAR)GetPriority_doc},
1011         {"getFrame", (PyCFunction) BL_ActionActuator::sPyGetFrame, METH_VARARGS, (PY_METHODCHAR)GetFrame_doc},
1012         {"getProperty", (PyCFunction) BL_ActionActuator::sPyGetProperty, METH_VARARGS, (PY_METHODCHAR)GetProperty_doc},
1013         {"getFrameProperty", (PyCFunction) BL_ActionActuator::sPyGetFrameProperty, METH_VARARGS, (PY_METHODCHAR)GetFrameProperty_doc},
1014 //      {"getChannel", (PyCFunction) BL_ActionActuator::sPyGetChannel, METH_VARARGS},
1015         {"getType", (PyCFunction) BL_ActionActuator::sPyGetType, METH_VARARGS, (PY_METHODCHAR)GetType_doc},
1016         {"setType", (PyCFunction) BL_ActionActuator::sPySetType, METH_VARARGS, (PY_METHODCHAR)SetType_doc},
1017         {"getContinue", (PyCFunction) BL_ActionActuator::sPyGetContinue, METH_NOARGS, 0},       
1018         {"setContinue", (PyCFunction) BL_ActionActuator::sPySetContinue, METH_O, 0},
1019         //<------
1020         KX_PYMETHODTABLE(BL_ActionActuator, setChannel),
1021         {NULL,NULL} //Sentinel
1022 };
1023
1024 PyAttributeDef BL_ActionActuator::Attributes[] = {
1025         KX_PYATTRIBUTE_FLOAT_RW("start", 0, MAXFRAMEF, BL_ActionActuator, m_startframe),
1026         KX_PYATTRIBUTE_FLOAT_RW("end", 0, MAXFRAMEF, BL_ActionActuator, m_endframe),
1027         KX_PYATTRIBUTE_FLOAT_RW("blendin", 0, MAXFRAMEF, BL_ActionActuator, m_blendin),
1028         KX_PYATTRIBUTE_SHORT_RW("priority", 0, 100, false, BL_ActionActuator, m_priority),
1029         KX_PYATTRIBUTE_FLOAT_RW_CHECK("frame", 0, MAXFRAMEF, BL_ActionActuator, m_localtime, CheckFrame),
1030         KX_PYATTRIBUTE_STRING_RW("property", 0, 31, false, BL_ActionActuator, m_propname),
1031         KX_PYATTRIBUTE_STRING_RW("frameProperty", 0, 31, false, BL_ActionActuator, m_framepropname),
1032         KX_PYATTRIBUTE_BOOL_RW("continue", BL_ActionActuator, m_end_reset),
1033         KX_PYATTRIBUTE_FLOAT_RW_CHECK("blendTime", 0, MAXFRAMEF, BL_ActionActuator, m_blendframe, CheckBlendTime),
1034         KX_PYATTRIBUTE_SHORT_RW_CHECK("type",0,100,false,BL_ActionActuator,m_playtype,CheckType),
1035         { NULL }        //Sentinel
1036 };
1037
1038 PyObject* BL_ActionActuator::_getattr(const char *attr) {
1039         if (!strcmp(attr, "action"))
1040                 return PyString_FromString(m_action->id.name+2);
1041         PyObject* object = _getattr_self(Attributes, this, attr);
1042         if (object != NULL)
1043                 return object;
1044         _getattr_up(SCA_IActuator);
1045 }
1046
1047 int BL_ActionActuator::_setattr(const char *attr, PyObject* value) {
1048         if (!strcmp(attr, "action"))
1049         {
1050                 if (!PyString_Check(value))
1051                 {
1052                         PyErr_SetString(PyExc_ValueError, "expected a string");
1053                         return 1;
1054                 }
1055
1056                 STR_String val = PyString_AsString(value);
1057                 
1058                 if (val == "")
1059                 {
1060                         m_action = NULL;
1061                         return 0;
1062                 }
1063
1064                 bAction *action;
1065                 
1066                 action = (bAction*)SCA_ILogicBrick::m_sCurrentLogicManager->GetActionByName(val);
1067                 
1068
1069                 if (!action)
1070                 {
1071                         PyErr_SetString(PyExc_ValueError, "action not found!");
1072                         return 1;
1073                 }
1074
1075                 m_action = action;
1076                 return 0;
1077         }
1078         int ret = _setattr_self(Attributes, this, attr, value);
1079         if (ret >= 0)
1080                 return ret;
1081         return SCA_IActuator::_setattr(attr, value);
1082 }