Files
tekton/modify_tscn.py
T
2026-04-13 18:15:49 +08:00

64 lines
2.5 KiB
Python

import sys
import re
file_path = 'c:/Users/beng/Godot/Projects/tekton-enet/scenes/main.tscn'
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
interaction_idx = -1
for i, line in enumerate(lines):
if line.startswith('[node name="InteractionBtn" type="VBoxContainer"'):
interaction_idx = i
break
if interaction_idx != -1:
for i in range(interaction_idx, interaction_idx + 10):
if lines[i].startswith('offset_bottom = '):
lines[i] = 'offset_bottom = 282.9999\n'
break
power_btn_start = -1
power_btn_end = -1
for i, line in enumerate(lines):
if line.startswith('[node name="PowerUpBtn" type="Button" parent="TouchLayer/TouchControls/PowerUpInventoryUI"'):
power_btn_start = i
# Need to check original parent too in case the earlier modification didn't change this string perfectly, wait!
# Ah, PowerUpBtn parent was originally "PowerUpInventoryUI". I did change PowerUpInventoryUI's parent... but did I change PowerUpBtn's parent text in `.tscn`? NO! PowerUpBtn is still `parent="PowerUpInventoryUI"`.
if line.startswith('[node name="PowerUpBtn" type="Button" parent="PowerUpInventoryUI"'):
power_btn_start = i
if power_btn_start != -1:
for i in range(power_btn_start+1, len(lines)):
if lines[i].startswith('[node '):
power_btn_end = i
break
if power_btn_end == -1:
power_btn_end = len(lines)
insert_target_start = -1
if interaction_idx != -1:
for i in range(interaction_idx + 1, len(lines)):
if lines[i].startswith('[node '):
if 'parent="TouchLayer/TouchControls/InteractionBtn"' in lines[i]:
pass
else:
insert_target_start = i
break
if power_btn_start != -1 and insert_target_start != -1:
block = lines[power_btn_start:power_btn_end]
block[0] = block[0].replace('parent="PowerUpInventoryUI"', 'parent="TouchLayer/TouchControls/InteractionBtn"')
del lines[power_btn_start:power_btn_end]
if insert_target_start > power_btn_end:
insert_target_start -= (power_btn_end - power_btn_start)
lines = lines[:insert_target_start] + block + lines[insert_target_start:]
with open(file_path, 'w', encoding='utf-8') as f:
f.writelines(lines)
print('Successfully moved PowerUpBtn and resized InteractionBtn')
else:
print(f'Failed to find targets: btn_start={power_btn_start}, insert_target={insert_target_start}, interaction_idx={interaction_idx}')