pedohorse/hpaste

Make it working not only via Shelf but also via Menu

pojitonov opened this issue · 2 comments

As a user who is not using Shelf or strive to minimize it usage as much as possible since it uses so much space I hide it in 99% of time. So I am trying to add Submenu in the Edit menu to get access to main commands instead of Shelf.
Unfortunately currently the scripts are working only from Shelf.
It would be nice to make it possible to invoke scripts from menu as well.
Currently I am getting this error when trying to invoke Paste or Paste Web commands from the menu.
Snag_3ba48e
Snag_3c3ab3

I am using this code to generate Submenu file – MainMenuCommon.xml

<?xml version="1.0" encoding="UTF-8"?>

<mainMenu>
    <menuBar>
    <subMenu id="edit_menu">
    <subMenu id="hpaste_menu">
    <label>HPaste</label>
    <insertAfter>h.paste</insertAfter>
    <scriptItem id="hpaste_copy">
        <label>Copy</label>
        <scriptCode>
<![CDATA[
try:
    from PySide2.QtWidgets import QApplication
except:
    from PySide.QtGui import QApplication

import hpaste

def do():
    try:
        nodes=hou.selectedItems()
    except:
        nodes=hou.selectedNodes()
    if len(nodes) == 0:
        hou.ui.displayMessage("No nodes are selected!",severity=hou.severityType.Error)
        return
    try:
        s=hpaste.nodesToString(nodes)
    except RuntimeError as e:
        hou.ui.displayMessage("Error: %s"%str(e),severity=hou.severityType.Error)
        return
    except RuntimeWarning as e:
        hou.ui.displayMessage("Warning: %s"%str(e),severity=hou.severityType.Warning)
    except Exception as e:
        hou.ui.displayMessage("Internal Error: %s"%str(e),severity=hou.severityType.Error)
        return

    if hou.applicationVersion()[0] > 15:
        hou.ui.copyTextToClipboard(s)
    else:
        qapp = QApplication.instance()
        qapp.clipboard().setText(s)
    
do()
]]>
        </scriptCode>
        </scriptItem>
    <scriptItem id="hpaste_paste">
        <label>Paste</label>
        <scriptCode>
<![CDATA[
try:
    from PySide2.QtWidgets import QApplication
except:
    from PySide.QtGui import QApplication

import hpaste
import hpaste.hpasteoptions as opt

def do(pane):
    if hou.applicationVersion()[0] > 15:
        s = hou.ui.getTextFromClipboard()
    else:
        qapp = QApplication.instance()
        s = qapp.clipboard().text()

    geonode = None
    for _ in range(2):
        try:
            hpaste.stringToNodes(s, ne=pane, hou_parent=geonode)
        except hpaste.InvalidContextError as e:
            nec, snc = e.contexts()
            if snc == 'Sop' and nec == 'Object':
                if hou.ui.displayMessage("Error: %s" % str(e), severity=hou.severityType.Warning, buttons=('Create geo node', 'Cancel'), default_choice=0, close_choice=1) == 0:
                    if geonode is not None:
                        raise RuntimeError('are we in an infinite loop?')
                    geonode = e.node().createNode('geo')
                    if pane is not None:
                        geonode.setPosition(pane.cursorPosition())
                    pane = None
                    continue
            else:
                hou.ui.displayMessage("Error: %s"%str(e),severity=hou.severityType.Error)
                return
        except RuntimeError as e:
            hou.ui.displayMessage("Error: %s"%str(e),severity=hou.severityType.Error)
            return
        except RuntimeWarning as e:
            hou.ui.displayMessage("Warning: %s"%str(e),severity=hou.severityType.Warning)
        except Exception as e:
            hou.ui.displayMessage("Internal Error: %s"%str(e),severity=hou.severityType.Error)
            return
        break

do(kwargs['pane'])
]]>
        </scriptCode>
        </scriptItem>
    <scriptItem id="hpaste_webcopy">
        <label>Copy Web</label>
        <scriptCode>
<![CDATA[
from hpaste.hpasteshelffunctions import hcopyweb
hcopyweb()
]]>
        </scriptCode>
        </scriptItem>
    <scriptItem id="hpaste_webpaste">
        <label>Paste Web</label>
        <scriptCode>
<![CDATA[
from hpaste.hpasteshelffunctions import hpasteweb
hpasteweb(kwargs['pane'])
]]>
        </scriptCode>
        </scriptItem>
    </subMenu>
    </subMenu>
    </menuBar>
</mainMenu>

hey there!
the shelf tool code expects 'pane' key to be present in the kwargs, it doesn't in case it's launched from menu.

simple fix - just replaces all kwargs['pane'] with None - everything should work.

BUT i want to mention: I'm not using shelf either, hpaste is on shelf only because shelf tools can be assigned shortcuts.

default shortcut is set to Ctrl+Alt+Shift+C / Ctrl+Alt+Shift+V and Shift+Tab for collections - and never think of the shelfs or menus again!

Hi there, yes it does work with None. That is awesome, thank you for your reply.
Yeah shortcuts is nice to have, but I am not using HPaste very frequently. I am using this shortcuts to copy and paste relative references. For me personally the Menu commands are more preferable because it is always easy to find. Maybe you will consider to add the Menu commands as well as a default feature.
Thank you!