Moving bones with script - get_bone_rest: Index p_bone = -1 is out of bounds (bones.size() = 21).

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Liam Steyn

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")

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)
:bust_in_silhouette: Reply From: jgodfrey

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…