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
# ID props will be segmented by replacing ][ with ].[
data_chunks = data_path_no_names.replace('][', '].[').split('.')
# probably regex should be used here and things put into dictionary
# so it's clear what chunk is what
# depends of use case, the main idea is to extract names, segment, then put back
# 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)