Skip to content

Commit

Permalink
updated load_from_configuration function
Browse files Browse the repository at this point in the history
  • Loading branch information
aardschok committed Sep 4, 2017
1 parent 1c39c7b commit d68f267
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 134 deletions.
112 changes: 49 additions & 63 deletions python/scriptsmenu/scriptsmenu.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
import os
from collections import OrderedDict


from .vendor.Qt import QtWidgets, QtCore
Expand Down Expand Up @@ -86,7 +85,7 @@ def create_default_items(self):
separator = self.addSeparator()
separator.setObjectName("separator")

def add_menu(self, parent, title):
def add_menu(self, title, parent=None):
"""
Create a sub menu for a parent widget
Expand All @@ -99,6 +98,9 @@ def add_menu(self, parent, title):
:return: QtWidget.QMenu instance
"""

if not parent:
parent = self

menu = QtWidgets.QMenu(parent, title)
menu.setTitle(title)
menu.setObjectName(title)
Expand Down Expand Up @@ -188,6 +190,50 @@ def add_script(self, parent, title, command, sourcetype, icon=None,

return script_action

def load_from_configuration(self, parent, configuration):
"""Process the configurations and store the configuration
This creates all submenus from a configuration.json file.
When the configuration holds the key `main` all scripts under `main` will
be added to the main menu first before adding the rest
Args:
parent (ScriptsMenu): script menu instance
configuration (list): A ScriptsMenu configuration list
"""

print "DEBUG : {}".format(parent.title())

for item in configuration:
assert isinstance(item, dict), "Configuration is wrong!"

# skip items which have no `type` key
item_type = item.get('type', None)
if not item_type:
log.warning("Missing 'type' from configuration item")
continue

# add separator
# Special behavior for separators
if item_type == "separator":
parent.addSeparator()

# add submenu
# items should hold a collection of submenu items (dict)
elif item_type == "menu":
assert "items" in item, "Menu is missing 'items' key"
menu = self.add_menu(parent=parent, title=item["title"])
self.load_from_configuration(menu, item["items"])

# add script
elif item_type == "action":
# filter out `type` from the item dict
config = {key: value for key, value in
item.items() if key != "type"}

self.add_script(parent=parent, **config)

def set_update_visible(self, state):
self.update_action.setVisible(state)

Expand Down Expand Up @@ -230,19 +276,6 @@ def _update_search(self, search):
action.setVisible(visible)


def create_submenu(menu, script, parent, items):

title = script["title"]
submenu = menu.add_menu(parent=parent, title=title)
for item in items:
assert isinstance(script, dict), "Configuration is wrong!"
if item['title'] == "separator":
submenu.addSeparator()
continue

menu.add_script(parent=submenu, **item)


def load_configuration(path):

if not os.path.isfile(path):
Expand All @@ -256,58 +289,11 @@ def load_configuration(path):

# retrieve and store config
with open(path, "r") as f:
data = json.load(f)

# check if configuration has an specific order
order = data.get("order", None)
if order is not None:
configuration = OrderedDict()
for key in order:
configuration[key] = data[key]
else:
configuration = data
configuration = json.load(f)

return configuration


def load_from_configuration(menu, configuration):
"""Process the configurations and store the configuration
This creates all submenus from a configuration.json file.
When the configuration holds the key `main` all scripts under `main` will
be added to the main menu first before adding the rest
Args:
menu (QtGui.QMenu): menu instance
configuration (dict): A ScriptsMenu configuration dictionary
"""

# todo: improve for-loop, too complex

for section, scripts in configuration.items():

if section == "main":
parent_menu = menu
else:
parent_menu = menu.add_menu(parent=menu, title=section)

for script in scripts:
assert isinstance(script, dict), "Configuration is wrong!"
# Special behavior for separators
if script['title'] == "separator":
menu.addSeparator()
continue

# items should hold a collection of submenu items (dict)
items = script.get("items", None)
if items:
create_submenu(menu, script, parent_menu, items)
continue

menu.add_script(parent=parent_menu, **script)


def application(configuration, parent):
import sys
app = QtWidgets.QApplication(sys.argv)
Expand Down
144 changes: 94 additions & 50 deletions samples/sample_configuration_a.json
Original file line number Diff line number Diff line change
@@ -1,61 +1,105 @@
{"Animation": [{"title" : "Script A",
[
{
"title": "Animation",
"type": "menu",
"items": [
{
"title": "Script A",
"tooltip": "Script A",
"command": "$SCRIPTMENU/script_a.py",
"sourcetype": "file",
"tags": ["test", "script", "cluster"],
"tags": [
"test",
"script",
"cluster"
],
"icon": "$SCRIPTMENU/resources/script_a.png",
"label": "SCR A"},
{"title" : "Script B",
"label": "SCR A"
},
{
"title": "Script B",
"tooltip": "Run script B",
"command": "$SCRIPTMENU/script_b.py",
"sourcetype": "file",
"tags": ["test", "script", "curves"]},
{"title" : "Script C",
"tags": [
"test",
"script",
"curves"
]
},
{
"title": "Script C",
"tooltip": "Run script C",
"command": "$SCRIPTMENU/script_c.py",
"sourcetype": "file",
"tags": ["test", "script", "joints"],
"icon": ""}
],

"Modeling": [{"title" : "Script A",
"tooltip": "Run script A",
"command": "$SCRIPTMENU/script_a.py",
"sourcetype": "file",
"tags": ["test", "script", "model", "blendshapes"]},
{"title" : "Script B",
"tooltip": "Run script B",
"command": "$SCRIPTMENU/script_b.py",
"sourcetype": "file",
"tags": ["test", "script", "normals", "model"]},
{"title" : "Script C",
"tooltip": "Run script C",
"command": "$SCRIPTMENU/script_c.py",
"sourcetype": "file",
"tags": ["math", "power", "sum"]}
],

"Rigging": [{"title" : "Script A",
"tooltip": "Run script A",
"command": "$SCRIPTMENU/script_a.py",
"sourcetype": "file",
"tags": ["test", "tool", "rig", "skeleton"]},
{"title" : "Script B",
"tooltip": "Run script A",
"command": "$SCRIPTMENU/script_b.py",
"sourcetype": "file",
"tags": ["test", "script", "cloth", "rig", "setup"]},
{"title" : "Script C",
"tooltip": "Run script C",
"command": "$SCRIPTMENU/script_c.py",
"sourcetype": "file",
"tags": ["test", "script", "approval"]}
],

"MEL": [{"title" : "Create cube",
"tooltip": "Launch character rigging tool",
"command": "polyCube -w 1 -h 1 -d 1;",
"sourcetype": "mel",
"tags": ["test", "script", "mel"]}
"tags": [
"test",
"script",
"joints"
],
"icon": ""
}
]
}
},
{
"title": "Modeling",
"type": "menu",
"items": [
{
"type": "action",
"title": "Script A",
"tooltip": "Run script A",
"command": "$SCRIPTMENU/script_a.py",
"sourcetype": "file",
"tags": [
"test",
"script",
"model",
"blendshapes"
]
},
{
"type": "action",
"title": "Script B",
"tooltip": "Run script B",
"command": "$SCRIPTMENU/script_b.py",
"sourcetype": "file",
"tags": [
"test",
"script",
"normals",
"model"
]
},
{
"type": "action",
"title": "Script C",
"tooltip": "Run script C",
"command": "$SCRIPTMENU/script_c.py",
"sourcetype": "file",
"tags": [
"math",
"power",
"sum"
]
}
]
},
{
"type": "menu",
"title": "MEL",
"items": [
{
"title": "Create cube",
"tooltip": "Launch character rigging tool",
"command": "polyCube -w 1 -h 1 -d 1;",
"sourcetype": "mel",
"tags": [
"test",
"script",
"mel"
]
}
]
}
]
65 changes: 44 additions & 21 deletions samples/sample_configuration_b.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
{
"Launcher": [
[
{
"title": "Echo command",
"tooltip": "Test print",
"command": "print('I am a tool launcher!')",
"sourcetype": "python",
"tags": ["test", "script", "echo", "command"]
},
{"title": "Echo second command",
"tooltip": "Test print",
"command": "print('I am a tool killer!')",
"sourcetype": "python",
"tags": ["test", "script", "echo", "command"]
},
{"title": "Launch script A",
"tooltip": "Test launcher",
"command": "$SCRIPTMENU/script_a.py",
"sourcetype": "file",
"tags": ["test", "script", "launch"]
"title":"Launcher",
"type": "menu",
"items": [
{
"type": "action",
"title": "Echo command",
"tooltip": "Test print",
"command": "print('I am a tool launcher!')",
"sourcetype": "python",
"tags": [
"test",
"script",
"echo",
"command"
]
},
{
"type": "action",
"title": "Echo second command",
"tooltip": "Test print",
"command": "print('I am a tool killer!')",
"sourcetype": "python",
"tags": [
"test",
"script",
"echo",
"command"
]
},
{
"type": "action",
"title": "Launch script A",
"tooltip": "Test launcher",
"command": "$SCRIPTMENU/script_a.py",
"sourcetype": "file",
"tags": [
"test",
"script",
"launch"
]
}
]
}
]
}
]

0 comments on commit d68f267

Please sign in to comment.