2
$\begingroup$

If I set the rotation X of a bone lets say

bone.rotation_euler = [rotX, 0, 0] 

and then I want to set the Z rotation, how can I get it so I don't overwrite it?

Something like:

rotX = bone.getRotationX()
bone.rotation_euler = [rotX, 0, rotZ]

PS: I must do it separately

$\endgroup$
2
  • $\begingroup$ can't you just set it individually with: bone.rotation_euler[2] = rotz $\endgroup$
    – photox
    Jan 25, 2017 at 19:21
  • $\begingroup$ rotX = bone.rotation_euler.x $\endgroup$ Jan 25, 2017 at 19:30

2 Answers 2

4
$\begingroup$

Concept

I first want to talk a little bit about the Armature/Animation concept as far as I understand it.

Bone

A bone is an element of the armature. You can see it as a structural element. You can view and modify the bones when you switch the armature to edit mode tab (in Blender).

enter image description here

You can't and you do not want to modify a bone within the game.

Channel

You are talking about a channel. A channel is a set of transformations of a single bone. Bone transformations can be translation, rotation and scale in any combination (material channels have different transformations). These transformations can change over time. I guess that is why it is called "channel" it is a flow. The transformation at a specific time is a "pose" of a bone. The pose of an armature contains the poses of all channels. You can see the channels when switching the armature to pose mode ctrl+tab (in Blender).

enter image description here

In Blender channels will be applied to bones of the same name. This is a loose coupling between these different entities. The great benefit is that you can apply an armature pose to a different armature. Only bones with the same name will receive the according bone pose from the channel.

Action

You can fix a bone pose by setting up a keyframe I. This keyframe will belong to an action. The action can contain several channels with multiple keyframes. The poses without keyframe will be calculated on the fly (within Blender and within the BGE).

The loose coupling between channels and bones allows to easily apply an action to another armature as long as the bones names match.

enter image description here

As you want to change the channel via custom code we do not need to look at actions any further.

Modifying Channels via custom code

Within the BGE you can access the current channels of an armature.

import bge

controller = bge.logic.getCurrentController()
armature = controller.owner

channel = armature.channels[0] # first channel

The channel has several attributes that can be changed. You are looking for a rotation.

Be aware there several different types of rotations. You set up the type in Blender or via BGE custom code.

enter image description here

I strongly suggest you always check the rotation mode rather than assuming it is a certain one. Mostlikely there will be no error when the mode is not what you expect but the resulting behavior will not be correct. So it is important to ensure it is what you need.

if channel.rotation_mode == bge.logic.ROT_MODE_XYZ:
    xyz_rotation = channel.rotation_euler

A rotation consists of several components dependent on the rotation mode. Directly modifying a component channel.rotation_euler.z = 10.0 will not result in a change of the rotation. You need to assign the rotation to make it effective:

    xyz_rotation.z = 10.0
    channel.rotation_euler = xyz_rotation

Btw. this belongs to nearly all operations that deal with Vectors. I'm not really sure but I guess the BGE does not notify when you change a single component. But it notifies when you exchange the container (by assigning a value to the attribute).

Remarks

I haven't used custom code to modify channels in the BGE only for investigation to your questions.

I noticed that changing the channel alone does not have immediate effect. Activating an armature actuator applied the changes from code. There might be other ways.

$\endgroup$
0
$\begingroup$
bone.rotation_euler.x = rotX
bone.rotation_euler.z = rotZ

Checkout the docs, there is a lot there :D

https://docs.blender.org/api/blender_python_api_current/mathutils.html?highlight=euler#mathutils.Euler.x

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .