How to locate object parts of rigged object visible in camera
Either use an index pass as you suggested, or just mimick one by using an unshaded material with a distinct colour for each body part.

You say you want to do this in Python, but you don't say why or what part of the process you want to automate. If you elaborate in a comment I might be able to give more Python-related info in this answer.