Unreal Enhanced Input Mapping to CSV
The streamer ThePrimeTime posted a review of an article [https://sourcegraph.com/blog/the-death-of-the-junior-developer] about using ChatGPT to code, vis a vis junior developers. https://www.youtube.com/watch?v=_cgONVgHzRw During the review, he pointed out that maybe the claim that you can now code with just "ChatJipperty" (or others) is worth testing out. I'm not sure this counts as an example, but the use case was pretty handy for me and fast to get working. I know it's a really simple utility case, not some deep coding problem. Note that I can barely read code, let alone write code, but I can be kind of persistent. I know Unreal okay ... enough to find this Python script example I came up with actually useful (for me only probably, but feel free to try it out). For the record, the script took 1.35 hours all up to get working, testing, trying out a few tweaks, during which I munched on pizza leftovers. What's the use case? I want to get the key mappings for my game all at once. There may be a way to do that already, but I don't know, so I though a script to automate this is a good quick thing to try out.
Unreal's Enhanced Input Mapping is essentially an array of Actions paired with an array of Input Keys (whether keyboard, mouse, or gamepad). These lists can grow quite long, so it's easy to lose track of what is what and looking in the actual Input Mapping asset is kind of slow as you have to expand the values one at a time.
So, I thought maybe ChatJipperty could write a python script to run in the editor and ouput a list of Action/Mapping pairs I could paste to a spreadsheet.
At first, the script would output only the memory address that unreal uses to store its key names (maybe that's what that is)... Input,ActionMapping <Struct 'Key' (0x00000640AD50F4C0) {}>, ... and so on. The actual value isn't human readable, and the bot couldn't figure out how to obtain the right value.
I tried asking ChatJipperty to use the methods you might use in Blueprints, but we got stuck in a conversational loop where no forward progress was made.
In the end, I figured it can just copy the entire array 'Mapping' from the asset and parse the key values from text (isntead of from the struct).
Final output!
exec(open("D:/Dropbox/PythonScripts/UnrealInputMappingCapture.py").read())
The command only works if you have an Input Mapping asset selected in unreal editor's Content Browser. It will warn in the Output Log if nothing was selected or the selection is not the right type.
The python is expandable below. Note that the script's output copies to clipboard, so you have to paste the output to Notepad++ (or similar) then save as CSV to import into google sheets (or similar). Note that for python scripts, there are library files that need to be installed sometimes. In this case, the script uses pyperclip to handshake with the system clipboard. Unreal doesn't know anything about this, but the bot was clever enough to figure a way to add it to the unreal's python scripts path as an appended source of libraries.
Assuming you already have Python installed on your PC, pyperclip (an extra library for Python) can be installed from python's repo, using the Windows Start > CMD Prompt: pip install pyperclip
Add the site-packages directory of your global Python installation to Unreal Engine's Python path: sys.path.append(r'C:\Users\UserName\AppData\Roaming\Python\Python311\site-packages') import pyperclip Check this path is correct using CMD: python -m site --user-site
import sys
import unreal
import csv
import io
import re
# Add the site-packages directory of your global Python installation to Unreal Engine's Python path
sys.path.append(r'C:\Users\UserName\AppData\Roaming\Python\Python311\site-packages')
import pyperclip # Ensure you have pyperclip installed (`pip install pyperclip`)
def get_selected_asset():
selected_assets = unreal.EditorUtilityLibrary.get_selected_assets()
if selected_assets:
return selected_assets[0]
return None
def extract_key_name(mapping_text):
key_match = re.search(r'Key=([A-Za-z0-9_]+)', mapping_text)
if key_match:
return key_match.group(1)
return "Unknown"
def get_input_mappings(input_mapping_asset):
mappings = []
if not input_mapping_asset:
unreal.log_error("No Input Mapping asset selected.")
return mappings
input_mappings = input_mapping_asset.get_editor_property('mappings')
for mapping in input_mappings:
mapping_text = mapping.export_text()
input_key_name = extract_key_name(mapping_text)
action_name = mapping.action.get_name()
mappings.append((input_key_name, action_name))
return mappings
def mappings_to_csv(mappings):
output = io.StringIO()
writer = csv.writer(output)
writer.writerow(['Input', 'ActionMapping'])
for mapping in mappings:
writer.writerow(mapping)
return output.getvalue()
def copy_to_clipboard(text):
pyperclip.copy(text)
unreal.log("CSV copied to clipboard.")
def main():
input_mapping_asset = get_selected_asset()
if not input_mapping_asset:
unreal.log_error("No Input Mapping asset selected in the content browser.")
return
mappings = get_input_mappings(input_mapping_asset)
if mappings:
csv_text = mappings_to_csv(mappings)
copy_to_clipboard(csv_text)
unreal.log("Input mappings have been exported to CSV and copied to clipboard.")
else:
unreal.log_error("No mappings found in the selected Input Mapping asset.")
if __name__ == "__main__":
main()
Kommentare