Drivers, and in particular driver variable targets have data_paths that will break in the UI using layout.prop(target.id, target.data_path)
an example is a mesh target with shape_keys.key_blocks["Key 1"].value
as a datapath.
def split_path(data_path):
'''
Split a data_path into parts
'''
if not len(data_path):
return []
# remove all collection names
match = re.findall(r'\[\"(.*?)\"\]\.', data_path)
namedic = {}
for i, m in enumerate(match):
key = "Collection___NAME________%d" % i # surely not lol.
data_path = data_path.replace(m, key, 1)
namedic[key] = m
parts = data_path.rpartition(".")
props = []
while parts[0] != parts[1] != '':
dp = parts[0]
props.append(parts[2])
parts = dp.rpartition(".")
props.append(parts[2])
# replace the names
propstring = ",".join(props)
for key, name in namedic.items():
propstring = propstring.replace(key, name)
props = propstring.split(",")
# reverse list
props.reverse()
return props
will return ['shape_keys', 'key_blocks["Key 1"]', 'value']
for aforementioned data_path
However it's possible for users to name their objects with quotes " and square brackets et al which will break the re.
Any suggestions on how to parse more effectively for names?
pose.bones['Bone']
will not work. It only works with parenthesis (" "). You don't need to worry about quotes (' '). Am I understanding the question right? $\endgroup$'["Bone["Bone'
rather than'Bone'
If the dp ispose.bones["Bone"].location
I want to have['pose', 'bones["Bone"]', 'location']
returned, even if the name is "dumb name" , from which I can".".join
the first two and uselayout.prop(id_obj.path_resolve(joinedbit), 'location')
for instance $\endgroup$