Joints behaving differently when instanced in editor and in code

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

I have the following block of code to create a rope, one segment at a time:

func add_segment(direction):
   var new_segment = Segment.instance()
   var new_segment_path = new_segment.get_path()

   new_segment.rotation = direction

   # Node a is this node, node b is next node
   if length > 0:
	  var prev_segment = segments.get_child(length-1)
	  prev_segment.connection.node_b = new_segment_path	
	  new_segment.position = (prev_segment.north_pole.position.rotated(direction) +
	  host_joint.node_b = new_segment_path
   length += 1

Each segment is a RigidBody2D with origin at the bottom of the segment and the north_pole at the top. Using this code causes strange behavior of the segments, and it seems like forces pull on them incorrectly. The joints are PinJoint2D, and node_a is defaulted to the segment it’s a child of. Host is a KinematicBody2D. I’m fairly certain the position I set new_segment.position to is correct.
However, when I add each segment to segments (a Node2D child of the rope structure) and connect its joint in the editor, everything behaves normally. Why is this the case?