Skip to main content
2 of 3
added 2 characters in body

Problems with parsing data_path can occur when the user gives entities names containing apostrophes(names containing quotes will not work in driver data_paths). Such will still work in drivers, i.e:

"""pose.bones["Bone'bla'"].location.x"""

So we can assume every name has to be enclosed with [""] in such datastring that drivers will accept:

def split_path(data_path):
    '''
    Split a data_path into parts
    '''
    if not data_path:
        return []
    
    # extract names from data_path
    names = data_path.split('"')[1::2]
    data_path_no_names = ''.join(data_path.split('"')[0::2])
    
    # segment into chunks
    data_chunks = data_path_no_names.split('.')
    
    # put names back into chunks where [] are
    for id, chunk in enumerate(data_chunks):
        if chunk.find('[]') > 0:
            data_chunks[id] = chunk.replace('[]', '["' + names.pop(0) + '"]')
    
    return data_chunks

The part # segment into chunks is just basic, some more advanced segmenting can be implemented (for example one that will understand location.x is one thing or that will extract correct target.id and target.data_path)