-
Notifications
You must be signed in to change notification settings - Fork 22
/
utils.py
144 lines (124 loc) · 4.35 KB
/
utils.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/python
"""
maya2katana
Copyright (C) 2016-2019 Andriy Babak, Animagrad
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Author: Andriy Babak
e-mail: [email protected]
------------------------------
Common utility functions to be imported by plugins
------------------------------
"""
import re
import logging
import maya.cmds as cmds
log = logging.getLogger("clip")
def node_attributes(node):
"""
Get Maya node attributes
"""
attributes = cmds.listAttr(node)
attr = {}
attr["node_name"] = node
attr["node_type"] = cmds.nodeType(node)
for attribute in attributes:
if "." in attribute:
continue
try:
val = cmds.getAttr(node + "." + attribute)
except RuntimeError:
continue
attr[attribute] = val
return attr
def unique_name(name=None, reset=False):
"""
Create a unique node name by appending A-Z letters
"""
if not hasattr(unique_name, "usedNames") or reset is True:
unique_name.usedNames = []
if isinstance(reset, list):
unique_name.usedNames = list(reset)
if name in unique_name.usedNames:
if name[-1] > "Z":
name = name + "A"
while name in unique_name.usedNames:
c = chr(ord(name[-1]) + 1)
if c > "Z":
c = "AA"
name = name[:-1] + c
unique_name.usedNames.append(name)
return name
def get_out_connection(connection):
"""
Generate full connection path
"""
if not connection:
return ""
out_port = [strip_namespace(connection["node"])]
if connection.get("original_port").startswith(("outDisplacement", "outEigenvalue")):
out_port.append(connection.get("original_port"))
elif connection.get("original_port").startswith("out"):
out_port.append("out")
else:
out_port.append(connection.get("original_port"))
original_port = re.findall(
r"^out(?:Color|Value)([RGBAXYZ])", connection.get("original_port") or ""
)
if original_port:
out_port.append(original_port[0].lower())
return ".".join(out_port)
def rename_connections(nodes):
"""
Perform node renamings
"""
renamings = {}
for node_name, node in nodes.items():
renamings.update(node["renamings"])
for node_name, node in nodes.items():
for source in node["connections"].values():
if source.get("node") not in renamings:
continue
renaming = renamings[source["node"]]
if node_name == renaming["name"]:
continue
# print 'Renaming "{original}" to "{new}" ({port})'.format(
# original=source['node'],
# new=renaming['name'],
# port=renaming['original_port'])
source["node"] = renaming["name"]
original_port = renaming.get("original_port")
if original_port:
source["original_port"] = original_port
def propagate_connection_weights(nodes):
"""
Connections may include weights so we need to propagate
them to respective upstream nodes
"""
for node in nodes.values():
for source in node["connections"].values():
weight = source.get("weight")
if not weight:
continue
target_node = nodes[source["node"]]
target_node["weight"] = target_node.get("weight", 0) + weight
def has_connection(node, param):
"""
Check if node dictionary includes the requested attribute connection
"""
return param in node["connections"]
def strip_namespace(name):
"""
Strip all namespaces.
Katana imports nodes with namespaces but at least RenderMan
refuses to render them
"""
return name.rsplit(":", 1)[-1]