-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
node2xml.exporter.xml
14 lines (14 loc) · 7.94 KB
/
node2xml.exporter.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<katana release="4.5v1" version="4.5.1.000008">
<node name="__SAVE_exportedNodes" type="Group">
<node baseType="Group" edited="true" name="node2xml" ns_basicDisplay="1" ns_fromContext="legacy" ns_iconName="" ns_viewState="2" selected="true" type="Group" x="-320.0" y="-32.0">
<group_parameter name="node2xml">
<group_parameter hints="{'widget': 'userParamsEditor', 'hideTitle': 'True'}" name="user">
<string_parameter expression="@node2xml" name="node_name"/>
<string_parameter hints="{'widget': 'fileInput'}" name="export_path" value="G:\personal\code\Foundry_Katana\workspace\v0001\Foundry_Katana\src\nodegraph\node2xml\node2xml.exporter.xml"/>
<string_parameter hints="{'widget': 'scriptButton', 'scriptText': '"""\nversion=3\npython>=2.7.1\nauthor=Liam Collod\nlast_modified=12/03/2022\n\n\nConvert the selected nodes to an XML representation, and write or print it.\n\nTo be used in a scrip button.\nThe button parameter must be named <export> or <print> to execute the\n corresponding function.\n\nThe following parameters should also exists on the same node :\n- user.node_name (str) : name of the node to export/print\n- user.export_path (str) : export path of the xml\n\n"""\nimport os\n\nfrom Katana import NodegraphAPI, UI4\n\n\ndef err(message):\n """\n Raise a small dialog with the given message and then raise a RuntimeError\n\n Args:\n message(str): error maise to raise\n """\n message = "[ScriptButton][node2xml]{}".format(message)\n raise RuntimeError(message)\n\n\ndef log(message):\n # the print function for this script\n message = "[ScriptButton][node2xml]{}".format(message)\n print(message)\n return\n\n\ndef get_selection_xml():\n nodes = NodegraphAPI.GetAllSelectedNodes()\n return NodegraphAPI.BuildNodesXmlIO(nodes)\n\n\ndef print_xml(xml=None):\n xml = xml or get_selection_xml()\n return log("\\n" + xml.writeString())\n\n\ndef write_xml(target_dir, target_name, display=False):\n """\n\n Args:\n target_dir(str): path to an existing directory\n target_name(str): name of the file to write without the extension\n display(bool): True to also print the xml file\n """\n\n target_path = os.path.join(target_dir, "{}.xml".format(target_name))\n\n xml = get_selection_xml()\n\n if display:\n print_xml(xml)\n\n xml.write(\n file=target_path,\n outputStyles=None\n )\n\n return log("[write_xml] Finished. XML written to <{}>".format(target_path))\n\n\ndef run():\n\n process = parameter.getName()\n\n export_node_name = node.getParameter("user.node_name").getValue(0)\n export_node = NodegraphAPI.GetNode(export_node_name)\n if not export_node:\n err("[run] Can\'t find node_name={}".format(export_node_name))\n\n # the xml function act on the selected node\n # we save the curent selection to override it and re-apply it at the end\n current_selection = NodegraphAPI.GetAllSelectedNodes()\n NodegraphAPI.SetAllSelectedNodes([export_node])\n\n if process == "export":\n\n export_path = node.getParameter("user.export_path").getValue(0)\n if not export_path.endswith(".xml"):\n err("[run] Export path doesn\'t ends with .xml: <{}>".format(\n export_path))\n\n export_dir, export_name = os.path.split(export_path)\n export_name = os.path.splitext(export_name)[0] # strip the .xml\n if not os.path.exists(export_dir):\n err("[run] Export directory must exists ! <{}>".format(export_path))\n\n write_xml(\n target_dir=export_dir,\n target_name=export_name\n )\n\n elif process == "print":\n print_xml()\n\n else:\n err(\n "This button <{}> should be named <export> or <print>"\n "".format(process)\n )\n\n NodegraphAPI.SetAllSelectedNodes(current_selection)\n return\n\n\nrun()'}" name="export" value=""/>
<string_parameter hints="{'widget': 'scriptButton', 'scriptText': '"""\nversion=3\npython>=2.7.1\nauthor=Liam Collod\nlast_modified=12/03/2022\n\n\nConvert the selected nodes to an XML representation, and write or print it.\n\nTo be used in a scrip button.\nThe button parameter must be named <export> or <print> to execute the\n corresponding function.\n\nThe following parameters should also exists on the same node :\n- user.node_name (str) : name of the node to export/print\n- user.export_path (str) : export path of the xml\n\n"""\nimport os\n\nfrom Katana import NodegraphAPI, UI4\n\n\ndef err(message):\n """\n Raise a small dialog with the given message and then raise a RuntimeError\n\n Args:\n message(str): error maise to raise\n """\n message = "[ScriptButton][node2xml]{}".format(message)\n raise RuntimeError(message)\n\n\ndef log(message):\n # the print function for this script\n message = "[ScriptButton][node2xml]{}".format(message)\n print(message)\n return\n\n\ndef get_selection_xml():\n nodes = NodegraphAPI.GetAllSelectedNodes()\n return NodegraphAPI.BuildNodesXmlIO(nodes)\n\n\ndef print_xml(xml=None):\n xml = xml or get_selection_xml()\n return log("\\n" + xml.writeString())\n\n\ndef write_xml(target_dir, target_name, display=False):\n """\n\n Args:\n target_dir(str): path to an existing directory\n target_name(str): name of the file to write without the extension\n display(bool): True to also print the xml file\n """\n\n target_path = os.path.join(target_dir, "{}.xml".format(target_name))\n\n xml = get_selection_xml()\n\n if display:\n print_xml(xml)\n\n xml.write(\n file=target_path,\n outputStyles=None\n )\n\n return log("[write_xml] Finished. XML written to <{}>".format(target_path))\n\n\ndef run():\n\n process = parameter.getName()\n\n export_node_name = node.getParameter("user.node_name").getValue(0)\n export_node = NodegraphAPI.GetNode(export_node_name)\n if not export_node:\n err("[run] Can\'t find node_name={}".format(export_node_name))\n\n # the xml function act on the selected node\n # we save the curent selection to override it and re-apply it at the end\n current_selection = NodegraphAPI.GetAllSelectedNodes()\n NodegraphAPI.SetAllSelectedNodes([export_node])\n\n if process == "export":\n\n export_path = node.getParameter("user.export_path").getValue(0)\n if not export_path.endswith(".xml"):\n err("[run] Export path doesn\'t ends with .xml: <{}>".format(\n export_path))\n\n export_dir, export_name = os.path.split(export_path)\n export_name = os.path.splitext(export_name)[0] # strip the .xml\n if not os.path.exists(export_dir):\n err("[run] Export directory must exists ! <{}>".format(export_path))\n\n write_xml(\n target_dir=export_dir,\n target_name=export_name\n )\n\n elif process == "print":\n print_xml()\n\n else:\n err(\n "This button <{}> should be named <export> or <print>"\n "".format(process)\n )\n\n NodegraphAPI.SetAllSelectedNodes(current_selection)\n return\n\n\nrun()'}" name="print" value=""/>
</group_parameter>
</group_parameter>
</node>
</node>
</katana>