0 votes

Following a Godot doc and just copied everything but I'm getting an error.

extends Spatial

var lowerarm_angle = Vector3()
var upperarm_angle = Vector3()
var skel
var bone = "upperarm"
var coordinate = 0


func _ready():
    skel = get_node("Armature/Skeleton")
    set_process(true)

func set_bone_rot(bone, ang):
    var b = skel.find_bone(bone)
    var rest = skel.get_bone_rest(b)
    var newposex = rest.rotated(Vector3(1.0, 0.0, 0.0), ang.x)
    var newposey = newposex.rotated(Vector3(0.0, 1.0, 0.0), ang.y)
    var newposez = newposey.rotated(Vector3(0.0, 0.0, 1.0), ang.z)
    var newpose = newposex and newposey and newposez
    skel.set_bone_pose(b, newpose)


func _process(delta):
    if Input.is_action_pressed("select_x"):
        coordinate = 0
    elif Input.is_action_pressed("select_y"):
        coordinate = 1
    elif Input.is_action_pressed("select_z"):
        coordinate = 2
    elif Input.is_action_pressed("select_upperarm"):
        bone = "upperarm"
    elif Input.is_action_pressed("select_lowearm"):
        bone = "lowerarm"
    elif Input.is_action_pressed("increment"):
        if bone == "lowerarm":
            lowerarm_angle[coordinate] += 1
        elif bone == "upperarm":
            upperarm_angle[coordinate] += 1
    elif Input.is_action_pressed("decrement"):
        if bone == "lowerarm":
            lowerarm_angle[coordinate] -= 1
        elif bone == "upperarm":
            upperarm_angle[coordinate] -= 1

    set_bone_rot("lowerarm", lowerarm_angle)
    set_bone_rot("upperarm", upperarm_angle)
Godot version 3.5
in Engine by (35 points)

1 Answer

0 votes

find_bone() will return the requested bone's index or -1 if it cannot be found. So, this code:

var b = skel.find_bone(bone)

...must be returning -1, and you're later using that here:

var rest = skel.get_bone_rest(b)

And, since -1 isn't a valid bone index, you get the reported error. To fix it, you'll need to determine why the bone can't be found. The most obvious reason is that you've provided the wrong name...

by (19,272 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.