-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
165 lines (119 loc) · 3.85 KB
/
utils.py
File metadata and controls
165 lines (119 loc) · 3.85 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
from __future__ import absolute_import, division, print_function, unicode_literals
from maya import cmds
from maya.api import OpenMaya as om
from .maya import apiundo as apiundo
class DagModifier(om.MDagModifier):
"""
Undoable MDagModifier command class.
"""
def __init__(self, *args, **kwargs):
super(DagModifier, self).__init__(*args, **kwargs)
apiundo.commit(self.undoIt, self.doIt)
def create_node(nodeName, nodeType, uuidStr=None, parent=None):
"""
Create node use api2.0.
Args:
nodeName (str): The name of the node to be created
nodeType (str): Node type name of the node to be created
uuidStr (str, optional): Used when specifying UUID. Defaults to None.
parent (str, optional): Used when specifying parent. Defaults to None.
Returns:
str: new created node name.
"""
dg_modifier = DagModifier()
if parent:
sel = om.MGlobal.getSelectionListByName(parent)
parent = sel.getDependNode(0)
node_obj = dg_modifier.createNode(nodeType, parent)
else:
node_obj = dg_modifier.createNode(nodeType)
node_fn = om.MFnDependencyNode(node_obj)
if uuidStr:
uuid = om.MUuid(uuidStr)
node_fn.setUuid(uuid)
node_fn.setName(nodeName)
dg_modifier.doIt()
return node_fn.name()
def get_dagPathObj(node):
selection_list = om.MGlobal.getSelectionListByName(node)
return selection_list.getDagPath(0)
def get_mObject(node):
selection_list = om.MGlobal.getSelectionListByName(node)
mObj = selection_list.getDependNode(0)
return mObj
def get_all_childs(node):
return cmds.listRelatives(node, c=True, ad=True)
def get_parentNode(node):
return cmds.listRelatives(node, p=True)
def get_instanceNumber(shape):
sl = om.MSelectionList()
sl.add(shape)
path = om.MDagPath()
sl.getDagPath(0, path)
return path.instanceNumber() if path.isValid() else -1
def get_instancePath(shape, iid):
sl = om.MSelectionList()
sl.add(shape)
obj = om.MObject()
sl.getDependNode(0, obj)
if obj.isNull():
return None
paths = om.MDagPathArray()
om.MDagPath.getAllPathsTo(obj, paths)
if iid >= paths.length():
return None
return paths[iid].partialPathName()
def get_allInstancePaths(shape):
sl = om.MSelectionList()
sl.add(shape)
obj = om.MObject()
sl.getDependNode(0, obj)
if obj.isNull():
return []
paths = om.MDagPathArray()
om.MDagPath.getAllPathsTo(obj, paths)
names = set()
for i in range(paths.length()):
names.add(paths[i].partialPathName())
lst = list(names)
lst.sort()
return lst
def get_uuid(node):
return cmds.ls(getNode(node, long=True), uuid=True)[0]
def get_nodeType(node):
return cmds.nodeType(getNode(node, long=True))
def has_shape(node):
return True if cmds.listRelatives(getNode(node, long=True), c=True, s=True) else False
def get_shapes(node):
return cmds.listRelatives(getNode(node, long=True), c=True, s=True) or []
def get_fullpath(node):
return cmds.ls(node, long=True)[0]
def get_visibility(node):
return cmds.getAttr(node + '.v')
def get_translate(node):
return cmds.getAttr(node + '.t')
def get_rotate(node):
return cmds.getAttr(node + '.r')
def get_scale(node):
return cmds.getAttr(node + '.s')
def get_pivot(node):
return cmds.xform(node, q=True, pivots=True, ws=True)
def getNode(name, long=False):
"""
Get node name from current scnene.
Args:
name (_type_): _description_
long (bool, optional): _description_. Defaults to False.
Returns:
_type_: _description_
"""
if cmds.objExists(name):
if long:
return get_fullpath(name)
else:
return cmds.ls(name)[0]
else:
return None