[Bf-blender-cvs] [23f5407] master: BGE: Added getActionName() function to KX_GameObject()

Mateo de Mayo noreply at git.blender.org
Tue Aug 4 00:11:47 CEST 2015


Commit: 23f54076db6d241af2a8f9404ab5f5b8072a4db0
Author: Mateo de Mayo
Date:   Mon Aug 3 23:47:46 2015 +0200
Branches: master
https://developer.blender.org/rB23f54076db6d241af2a8f9404ab5f5b8072a4db0

BGE: Added getActionName() function to KX_GameObject()

It works similar to getActionFrame(), you have to give a layer or not (for layer 0) as the argument and it returns the name of the animation that the object is currently playing.

Example:

```
import bge
own = bge.logic.getCurrentController().owner
own.playAction("SomeAction",0,20)
print(own.getActionName())
```
>> SomeAction

Here is an example file, just open the blend file with the terminal opened
and press P, you can see how the current animation is being printed:
{F217484}

Reviewers: moguri, hg1, panzergame, campbellbarton

Reviewed By: panzergame

Subscribers: campbellbarton, hg1, #game_engine

Projects: #game_engine

Differential Revision: https://developer.blender.org/D1443

===================================================================

M	doc/python_api/rst/bge_types/bge.types.KX_GameObject.rst
M	source/gameengine/Ketsji/BL_Action.cpp
M	source/gameengine/Ketsji/BL_Action.h
M	source/gameengine/Ketsji/BL_ActionManager.cpp
M	source/gameengine/Ketsji/BL_ActionManager.h
M	source/gameengine/Ketsji/KX_GameObject.cpp
M	source/gameengine/Ketsji/KX_GameObject.h

===================================================================

diff --git a/doc/python_api/rst/bge_types/bge.types.KX_GameObject.rst b/doc/python_api/rst/bge_types/bge.types.KX_GameObject.rst
index a24aa54..88b1d7d 100644
--- a/doc/python_api/rst/bge_types/bge.types.KX_GameObject.rst
+++ b/doc/python_api/rst/bge_types/bge.types.KX_GameObject.rst
@@ -972,6 +972,16 @@ base class --- :class:`SCA_IObject`
       :return: The current frame of the action
       :rtype: float
 
+   .. method:: getActionName(layer=0)
+
+      Gets the name of the current action playing in the supplied layer.
+
+      :arg layer: The layer that you want to get the action name from.
+      :type layer: integer
+
+      :return: The name of the current action
+      :rtype: string
+
    .. method:: setActionFrame(frame, layer=0)
 
       Set the current frame of the action playing in the supplied layer.
diff --git a/source/gameengine/Ketsji/BL_Action.cpp b/source/gameengine/Ketsji/BL_Action.cpp
index 9bae119..12a1cae 100644
--- a/source/gameengine/Ketsji/BL_Action.cpp
+++ b/source/gameengine/Ketsji/BL_Action.cpp
@@ -301,6 +301,18 @@ float BL_Action::GetFrame()
 	return m_localtime;
 }
 
+const char *BL_Action::GetName()
+{
+	if (m_action != NULL) {
+		return m_action->id.name + 2;
+	}
+	else {
+		return "";
+	}
+
+	            
+}
+
 void BL_Action::SetFrame(float frame)
 {
 	// Clamp the frame to the start and end frame
diff --git a/source/gameengine/Ketsji/BL_Action.h b/source/gameengine/Ketsji/BL_Action.h
index dd1cd1f..379dd52 100644
--- a/source/gameengine/Ketsji/BL_Action.h
+++ b/source/gameengine/Ketsji/BL_Action.h
@@ -112,6 +112,8 @@ public:
 
 	// Accessors
 	float GetFrame();
+	const char *GetName();
+
 	struct bAction *GetAction();
 
 	// Mutators
diff --git a/source/gameengine/Ketsji/BL_ActionManager.cpp b/source/gameengine/Ketsji/BL_ActionManager.cpp
index 975f9ea..9e46905 100644
--- a/source/gameengine/Ketsji/BL_ActionManager.cpp
+++ b/source/gameengine/Ketsji/BL_ActionManager.cpp
@@ -68,6 +68,12 @@ float BL_ActionManager::GetActionFrame(short layer)
 	return action ? action->GetFrame() : 0.f;
 }
 
+const char *BL_ActionManager::GetActionName(short layer)
+{
+	BL_Action *action = GetAction(layer);
+	return action ? action->GetName() : "";
+}
+
 void BL_ActionManager::SetActionFrame(short layer, float frame)
 {
 	BL_Action *action = GetAction(layer);
diff --git a/source/gameengine/Ketsji/BL_ActionManager.h b/source/gameengine/Ketsji/BL_ActionManager.h
index 00e5366..97d6d88 100644
--- a/source/gameengine/Ketsji/BL_ActionManager.h
+++ b/source/gameengine/Ketsji/BL_ActionManager.h
@@ -85,6 +85,11 @@ public:
 	float GetActionFrame(short layer);
 
 	/**
+	 * Gets the name of the current action
+	 */        
+	const char *GetActionName(short layer);
+
+	/**
 	 * Sets the current frame of an action
 	 */
 	void SetActionFrame(short layer, float frame);
diff --git a/source/gameengine/Ketsji/KX_GameObject.cpp b/source/gameengine/Ketsji/KX_GameObject.cpp
index 536670d..651df9a 100644
--- a/source/gameengine/Ketsji/KX_GameObject.cpp
+++ b/source/gameengine/Ketsji/KX_GameObject.cpp
@@ -494,6 +494,11 @@ float KX_GameObject::GetActionFrame(short layer)
 	return GetActionManager()->GetActionFrame(layer);
 }
 
+const char *KX_GameObject::GetActionName(short layer)
+{
+	return GetActionManager()->GetActionName(layer);
+}
+
 void KX_GameObject::SetActionFrame(short layer, float frame)
 {
 	GetActionManager()->SetActionFrame(layer, frame);
@@ -1957,6 +1962,7 @@ PyMethodDef KX_GameObject::Methods[] = {
 	KX_PYMETHODTABLE_KEYWORDS(KX_GameObject, playAction),
 	KX_PYMETHODTABLE(KX_GameObject, stopAction),
 	KX_PYMETHODTABLE(KX_GameObject, getActionFrame),
+	KX_PYMETHODTABLE(KX_GameObject, getActionName),
 	KX_PYMETHODTABLE(KX_GameObject, setActionFrame),
 	KX_PYMETHODTABLE(KX_GameObject, isPlayingAction),
 	
@@ -3921,7 +3927,7 @@ KX_PYMETHODDEF_DOC(KX_GameObject, stopAction,
 	"stopAction(layer=0)\n"
 	"Stop playing the action on the given layer\n")
 {
-	short layer=0;
+	short layer = 0;
 
 	if (!PyArg_ParseTuple(args, "|h:stopAction", &layer))
 		return NULL;
@@ -3937,7 +3943,7 @@ KX_PYMETHODDEF_DOC(KX_GameObject, getActionFrame,
 	"getActionFrame(layer=0)\n"
 	"Gets the current frame of the action playing in the supplied layer\n")
 {
-	short layer=0;
+	short layer = 0;
 
 	if (!PyArg_ParseTuple(args, "|h:getActionFrame", &layer))
 		return NULL;
@@ -3947,11 +3953,25 @@ KX_PYMETHODDEF_DOC(KX_GameObject, getActionFrame,
 	return PyFloat_FromDouble(GetActionFrame(layer));
 }
 
+KX_PYMETHODDEF_DOC(KX_GameObject, getActionName,
+	"getActionName(layer=0)\n"
+	"Gets the name of the current action playing in the supplied layer\n")
+{
+	short layer = 0;
+
+	if (!PyArg_ParseTuple(args, "|h:getActionName", &layer))
+		return NULL;
+
+	layer_check(layer, "getActionName");
+
+	return PyUnicode_FromString(GetActionName(layer));
+}
+
 KX_PYMETHODDEF_DOC(KX_GameObject, setActionFrame,
 	"setActionFrame(frame, layer=0)\n"
 	"Set the current frame of the action playing in the supplied layer\n")
 {
-	short layer=0;
+	short layer = 0;
 	float frame;
 
 	if (!PyArg_ParseTuple(args, "f|h:setActionFrame", &frame, &layer))
@@ -3968,7 +3988,7 @@ KX_PYMETHODDEF_DOC(KX_GameObject, isPlayingAction,
 	"isPlayingAction(layer=0)\n"
 	"Checks to see if there is an action playing in the given layer\n")
 {
-	short layer=0;
+	short layer = 0;
 
 	if (!PyArg_ParseTuple(args, "|h:isPlayingAction", &layer))
 		return NULL;
diff --git a/source/gameengine/Ketsji/KX_GameObject.h b/source/gameengine/Ketsji/KX_GameObject.h
index 99c8a6d..c10802a 100644
--- a/source/gameengine/Ketsji/KX_GameObject.h
+++ b/source/gameengine/Ketsji/KX_GameObject.h
@@ -277,6 +277,11 @@ public:
 	float GetActionFrame(short layer);
 
 	/**
+	 * Gets the name of the current action
+	 */
+	const char *GetActionName(short layer);
+
+	/**
 	 * Sets the current frame of an action
 	 */
 	void SetActionFrame(short layer, float frame);
@@ -1037,6 +1042,7 @@ public:
 	KX_PYMETHOD_DOC(KX_GameObject, playAction);
 	KX_PYMETHOD_DOC(KX_GameObject, stopAction);
 	KX_PYMETHOD_DOC(KX_GameObject, getActionFrame);
+	KX_PYMETHOD_DOC(KX_GameObject, getActionName);
 	KX_PYMETHOD_DOC(KX_GameObject, setActionFrame);
 	KX_PYMETHOD_DOC(KX_GameObject, isPlayingAction);




More information about the Bf-blender-cvs mailing list