What I’m finding now is that it works - it changes the “EditorPerProjectUserSettings.ini” file, and updates it the way I want (it adds / changes settings).
The problem is that Unreal itself needs to be told to “read” the INI (ie. refresh the editor preferences) and I haven’t been able to find how to do that yet.
Here’s my code anyway:
import os
import unreal
import sys
data = {}
proj_dir = unreal.Paths.project_dir()
ini_path = os.path.join(proj_dir, 'Saved', 'Config', 'WindowsEditor', 'EditorPerProjectUserSettings.ini').replace("\\","/")
# -----------------------------
# Read the INI file and store as
# an array variable called 'data'
# -----------------------------
print("*****************************")
print("-----------------------------")
print("AnimPal: Starting to optimize your settings...")
print("AnimPal: Reading INI file 'EditorPerProjectUserSettings.ini'...")
with open(ini_path, "r") as f:
raw_data = f.read()
section = ""
lines = raw_data.split("\n")
for line in lines:
if len(line) == 0:
continue
if line.startswith("[") and line.endswith("]"):
section = line[1:-1]
data[section] = {}
continue
parts = line.split("=", 1)
key = parts[0]
value = ""
if len(parts) > 1:
value = parts[1]
if not data[section].get(key):
data[section][key] = value
continue
if isinstance(data[section][key], str):
data[section][key] = [data[section][key]]
data[section][key].append(value)
# ORBIT CAMERA AROUND SELECTION
print("-------")
try:
# Tries to pull the value, to see if it exists
orbitcamstatusstr = str(data['/Script/UnrealEd.LevelEditorViewportSettings']['bOrbitCameraAroundSelection'])
if orbitcamstatusstr == "True":
print("AnimPal: A setting for 'Orbit camera around selection' was found, and it's set to True. No change will be made.")
else:
print("AnimPal: A setting for 'Orbit camera around selection' was found, and it's set to False.")
data['/Script/UnrealEd.LevelEditorViewportSettings']['bOrbitCameraAroundSelection'] = "True"
print("AnimPal: Setting change to True.")
except:
print("AnimPal: There is no current setting for 'Orbit camera around selection'. Adding a setting and making it True.")
data['/Script/UnrealEd.LevelEditorViewportSettings']['bOrbitCameraAroundSelection'] = "True"
print("AnimPal: Setting added.")
# -----------------------------
# Write back the INI file:
# -----------------------------
print("-------")
print("AnimPal: Starting to write INI file 'EditorPerProjectUserSettings.ini'.")
deleting_key=False
if deleting_key:
if section in data:
data[section].pop(key, None)
else:
if not section in data:
data[section] = {}
data[section][key] = value
output = ""
for section_key, section_value in data.items():
output += "[" + section_key + "]" + "\n"
for item_key, item_value in section_value.items():
if isinstance(item_value, str):
output += item_key + "=" + item_value + "\n"
elif isinstance(item_value, list):
for list_item in item_value:
output += item_key + "=" + list_item + "\n"
output += "\n"
with open(ini_path, "w") as f:
f.write(output)
print("AnimPal: Completed update of INI file 'EditorPerProjectUserSettings.ini'.")
print("AnimPal: Finished optimizing your settings.")
print("-----------------------------")
print("*****************************")