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