Luckily I had to do something similar recently - haven't tested this in the terminal, but it doesn't rely on anything that isn't available in that context, AFAIK.
def locateNodes():
'''get all backdrop nodes in a script, and return a dictionary of {backdropName:[list of nodes]}'''
bdNodes=nuke.allNodes(filter="BackdropNode")
containedNodes={}
#create a dictionary of backdropName : (xpos, ypos, xpos+width, ypos+height)
regions={}
for backdropNode in bdNodes:
regions[backdropNode.name()]=(backdropNode.knob('xpos').value(),
backdropNode.knob('ypos').value(),
backdropNode.knob('xpos').value()+backdropNode.knob('bdwidth').value(),
backdropNode.knob('ypos').value()+backdropNode.knob('bdheight').value())
for node in nuke.allNodes():
if node.Class()!="BackdropNode":
pos=(node.knob('xpos').value(), node.knob('ypos').value())
for backdropNodeName in regions:
backdropRegion=regions[backdropNodeName]
if pos[0]>=backdropRegion[0] and pos[0]<=backdropRegion[2] and pos[1]>=backdropRegion[1] and pos[1]<=backdropRegion[3]:
try:
containedNodes[backdropNodeName].append(node)
except KeyError:
containedNodes[backdropNodeName]=[node]
return containedNodes