-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
node2xml.button.py
119 lines (82 loc) · 2.98 KB
/
node2xml.button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
version=3
python>=2.7.1
author=Liam Collod
last_modified=12/03/2022
Convert the selected nodes to an XML representation, and write or print it.
To be used in a scrip button.
The button parameter must be named <export> or <print> to execute the
corresponding function.
The following parameters should also exists on the same node :
- user.node_name (str) : name of the node to export/print
- user.export_path (str) : export path of the xml
"""
import os
from Katana import NodegraphAPI, UI4
def err(message):
"""
Raise a small dialog with the given message and then raise a RuntimeError
Args:
message(str): error maise to raise
"""
message = "[ScriptButton][node2xml]{}".format(message)
raise RuntimeError(message)
def log(message):
# the print function for this script
message = "[ScriptButton][node2xml]{}".format(message)
print(message)
return
def get_selection_xml():
nodes = NodegraphAPI.GetAllSelectedNodes()
return NodegraphAPI.BuildNodesXmlIO(nodes)
def print_xml(xml=None):
xml = xml or get_selection_xml()
return log("\n" + xml.writeString())
def write_xml(target_dir, target_name, display=False):
"""
Args:
target_dir(str): path to an existing directory
target_name(str): name of the file to write without the extension
display(bool): True to also print the xml file
"""
target_path = os.path.join(target_dir, "{}.xml".format(target_name))
xml = get_selection_xml()
if display:
print_xml(xml)
xml.write(
file=target_path,
)
return log("[write_xml] Finished. XML written to <{}>".format(target_path))
def run():
process = parameter.getName()
export_node_name = node.getParameter("user.node_name").getValue(0)
export_node = NodegraphAPI.GetNode(export_node_name)
if not export_node:
err("[run] Can't find node_name={}".format(export_node_name))
# the xml function act on the selected node
# we save the curent selection to override it and re-apply it at the end
current_selection = NodegraphAPI.GetAllSelectedNodes()
NodegraphAPI.SetAllSelectedNodes([export_node])
if process == "export":
export_path = node.getParameter("user.export_path").getValue(0)
if not export_path.endswith(".xml"):
err("[run] Export path doesn't ends with .xml: <{}>".format(
export_path))
export_dir, export_name = os.path.split(export_path)
export_name = os.path.splitext(export_name)[0] # strip the .xml
if not os.path.exists(export_dir):
err("[run] Export directory must exists ! <{}>".format(export_path))
write_xml(
target_dir=export_dir,
target_name=export_name
)
elif process == "print":
print_xml()
else:
err(
"This button <{}> should be named <export> or <print>"
"".format(process)
)
NodegraphAPI.SetAllSelectedNodes(current_selection)
return
run()