Skip to content

Instantly share code, notes, and snippets.

@matheusfillipe
Last active April 23, 2022 04:02
Show Gist options
  • Select an option

  • Save matheusfillipe/5ea2d5eda79a2cf4a8423a62526cae20 to your computer and use it in GitHub Desktop.

Select an option

Save matheusfillipe/5ea2d5eda79a2cf4a8423a62526cae20 to your computer and use it in GitHub Desktop.
Remove duplicated shortcuts on blender keymaps
# Reads all .py files in the current directory and checks if they have the global keyconfig_data
# caracteristic of blender keymaps and loops over the items of the keymaps removing duplicates.
# In the end saves them to the output directory
from pathlib import Path
from pprint import pformat
output = "output/"
boilerplate = """\
keyconfig_version = (3, 2, 4)
keyconfig_data = \\
{}
if __name__ == "__main__":
# Only add keywords that are supported.
from bpy.app import version as blender_version
keywords = {{}}
if blender_version >= (2, 92, 0):
keywords["keyconfig_version"] = keyconfig_version
import os
from bl_keymap_utils.io import keyconfig_import_from_data
keyconfig_import_from_data(
os.path.splitext(os.path.basename(__file__))[0],
keyconfig_data,
**keywords,
)
"""
keymaps = [str(p)[:-3] for p in Path(".").glob("*.py") if str(p) != Path(__file__).name]
modules = list(
filter(
lambda k: hasattr(k, "keyconfig_data")
and k.keyconfig_data
and type(k.keyconfig_data) is list,
map(__import__, keymaps),
)
)
new_keymaps = {}
for module in modules:
name = module.__name__
new_keymaps[name] = []
print(f"\n\nProcessing {name}")
print("-------------------------------------------------------")
for keyconfig in module.keyconfig_data:
editor = keyconfig[0]
items = keyconfig[2].get("items", [])
new_items = []
for item in items:
if item in new_items:
continue
new_items.append(item)
if len(items) != len(new_items):
print(f"REDUCED: {len(items)} -->{len(new_items)} keymaps in {editor}")
new_keymaps[name].append(
(
keyconfig[0],
keyconfig[1],
{"items": new_items},
)
)
Path(output).mkdir(parents=True, exist_ok=True)
for name in new_keymaps:
with open(output + name + ".py", "w") as f:
f.write(boilerplate.format(pformat(new_keymaps[name])))
print(f"\n\n\nWRITTEN TO: {Path(output).absolute()}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment