<?xml version="1.0" encoding="UTF-8"?>
<shelfDocument>
  <!-- This file contains definitions of shelves, toolbars, and tools.
 It should not be hand-edited when it is being used by the application.
 Note, that two definitions of the same element are not allowed in
 a single file. -->

  <tool name="open_cookbook" label="Open Cookbook" icon="hicon:/SVGIcons.index?BUTTONS_new_find_window.svg">
    <helpText><![CDATA["""Open Cookbook website"""
]]></helpText>
    <script scriptType="python"><![CDATA[import webbrowser

webbrowser.open("https://codercat.xyz/cookbook/")]]></script>
  </tool>

  <toolshelf name="codercat_cookbook" label="Cookbook">
    <memberTool name="cookbook_copy"/>
    <memberTool name="cookbook_paste"/>
    <memberTool name="open_cookbook"/>
  </toolshelf>

  <tool name="cookbook_copy" label="Copy" icon="hicon:/SVGIcons.index?BUTTONS_copy.svg">
    <helpText><![CDATA["""Copy selected nodes as recipe serialized data."""

This then can be shared on https://codercat.xyz/cookbook/ or pasted using the Paste shelf button into the apropriate node context.]]></helpText>
    <toolMenuContext name="viewer">
      <contextNetType>OBJ</contextNetType>
      <contextNetType>SOP</contextNetType>
      <contextNetType>DOP</contextNetType>
      <contextNetType>LOP</contextNetType>
    </toolMenuContext>
    <toolMenuContext name="cop_viewer">
      <contextNetType>COP2</contextNetType>
    </toolMenuContext>
    <toolMenuContext name="network">
      <contextNetType>OBJ</contextNetType>
      <contextNetType>SOP</contextNetType>
      <contextNetType>CHOP</contextNetType>
      <contextNetType>ROP</contextNetType>
      <contextNetType>SHOP</contextNetType>
      <contextNetType>COP2</contextNetType>
      <contextNetType>VOP</contextNetType>
      <contextNetType>VOPNET</contextNetType>
      <contextNetType>DOP</contextNetType>
      <contextNetType>TOP</contextNetType>
      <contextNetType>LOP</contextNetType>
    </toolMenuContext>
    <script scriptType="python"><![CDATA[import json 

selected = hou.selectedNodes()

if len(selected) > 0:
    pos = selected[0].position()
    context = selected[0].type().category().name().lower()
    version = hou.applicationVersionString()

    meta = {}
    meta["context"] = context
    meta["version"] = version
    
    data = hou.data.selectedItemsAsData(anchor_position=pos)
    data["__cookbookMeta"] = meta
    
    hou.ui.copyTextToClipboard(json.dumps(data))]]></script>
  </tool>

  <tool name="cookbook_paste" label="Paste" icon="hicon:/SVGIcons.index?BUTTONS_paste.svg">
    <helpText><![CDATA["""Paste serialized recipe data to appropriate node context."""]]></helpText>
    <toolMenuContext name="viewer">
      <contextNetType>OBJ</contextNetType>
      <contextNetType>SOP</contextNetType>
      <contextNetType>DOP</contextNetType>
      <contextNetType>LOP</contextNetType>
    </toolMenuContext>
    <toolMenuContext name="cop_viewer">
      <contextNetType>COP2</contextNetType>
    </toolMenuContext>
    <toolMenuContext name="network">
      <contextNetType>OBJ</contextNetType>
      <contextNetType>SOP</contextNetType>
      <contextNetType>CHOP</contextNetType>
      <contextNetType>ROP</contextNetType>
      <contextNetType>SHOP</contextNetType>
      <contextNetType>COP2</contextNetType>
      <contextNetType>VOP</contextNetType>
      <contextNetType>VOPNET</contextNetType>
      <contextNetType>DOP</contextNetType>
      <contextNetType>TOP</contextNetType>
      <contextNetType>LOP</contextNetType>
    </toolMenuContext>
    <script scriptType="python"><![CDATA[import json

# Get all visible pane tabs
all_pane_tabs = hou.ui.paneTabs()
offset = hou.Vector2(0, 0)
text = hou.ui.getTextFromClipboard()


try:
    data = json.loads(text)
except json.JSONDecodeError as e:
    hou.ui.displayMessage("Clipboard does not contain a valid recipe.")
    exit()


#remove meta
meta = data.pop('__cookbookMeta', None)
context = meta["context"]

found = False
# Iterate over all pane tabs
for pane_tab in all_pane_tabs:
    # Check if the pane tab is a scene viewer (common for Geometry nodes)
    if pane_tab.type() != hou.paneTabType.NetworkEditor:
        continue
    # Get the current node in the pane tab
    # Get visible bounds of the network editor
    paneContext = pane_tab.pwd().childTypeCategory().name().lower()

    if context in paneContext:
        bounds = pane_tab.visibleBounds()
        offset = bounds.center()
        current_node = pane_tab.pwd()
        found = True
        break

if not found :
    hou.ui.displayMessage("Please open " + context + " context to paste recipe")
    exit()


items = hou.createItemsFromData(current_node, data)

if len(items) < 1:
    hou.ui.displayMessage("Please open the correct " + context + " context to paste recipe")
    exit()

for item in items.values():
    if not item.parentNetworkBox():
        nodePos = item.position()
        item.setPosition(nodePos + offset)
]]></script>
  </tool>
</shelfDocument>
