Fix #110732: blender_theme_as_c fails fails with error

Renaming members of a struct that it's self was renamed was not working,
support renamed structs as well as order member renaming - needed for
the lamp members that replace one member with another.
This commit is contained in:
Campbell Barton 2023-08-04 18:00:08 +10:00
parent 1e1f4db8b6
commit 1f612605ab
1 changed files with 43 additions and 5 deletions

View File

@ -88,6 +88,12 @@ def dna_rename_defs(blend):
"""
from blendfile import DNAName
import re
re_dna_struct_rename = re.compile(
r'DNA_STRUCT_RENAME+\('
r'([a-zA-Z0-9_]+)' r',\s*'
r'([a-zA-Z0-9_]+)' r'\)',
)
re_dna_struct_rename_elem = re.compile(
r'DNA_STRUCT_RENAME_ELEM+\('
r'([a-zA-Z0-9_]+)' r',\s*'
@ -96,14 +102,46 @@ def dna_rename_defs(blend):
)
with open(dna_rename_defs_h, 'r', encoding='utf-8') as fh:
data = fh.read()
for l in data.split('\n'):
m = re_dna_struct_rename_elem.match(l)
struct_runtime_to_storage_map = {}
member_runtime_to_storage_map = {}
for line in data.split('\n'):
m = re_dna_struct_rename.match(line)
if m is not None:
struct_name, member_storage, member_runtime = m.groups()
struct_name = struct_name.encode('utf-8')
struct_storage, struct_runtime = m.groups()
struct_runtime_to_storage_map[struct_runtime] = struct_storage
continue
m = re_dna_struct_rename_elem.match(line)
if m is not None:
struct_name_runtime, member_storage, member_runtime = m.groups()
if struct_name_runtime not in member_runtime_to_storage_map:
member_runtime_to_storage_map[struct_name_runtime] = []
member_runtime_to_storage_map[struct_name_runtime].append((member_storage, member_runtime))
continue
for struct_name_runtime, members in member_runtime_to_storage_map.items():
if len(members) > 1:
# Order renames that are themselves destinations to go first, so that the item is not removed.
# Needed for e.g.
# `DNA_STRUCT_RENAME_ELEM(Light, energy_new, energy);`
# `DNA_STRUCT_RENAME_ELEM(Light, energy, energy_deprecated)`
# ... in this case the order matters.
member_runtime_set = set(member_runtime for (_member_storage, member_runtime) in members)
members_ordered = ([], [])
for (member_storage, member_runtime) in members:
members_ordered[member_storage not in member_runtime_set].append((member_storage, member_runtime))
members = members_ordered[0] + members_ordered[1]
del member_runtime_set, members_ordered
for (member_storage, member_runtime) in members:
struct_name_storage = struct_runtime_to_storage_map.get(struct_name_runtime, struct_name_runtime)
struct_name_storage = struct_name_storage.encode('utf-8')
# The struct it's self may have been renamed.
member_storage = member_storage.encode('utf-8')
member_runtime = member_runtime.encode('utf-8')
dna_struct = blend.structs[blend.sdna_index_from_id[struct_name]]
dna_struct = blend.structs[blend.sdna_index_from_id[struct_name_storage]]
for field in dna_struct.fields:
dna_name = field.dna_name
if member_storage == dna_name.name_only: