-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontainer.py
277 lines (235 loc) · 9.36 KB
/
container.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import maya.api.OpenMaya as om2
from util import node
def getObjectsContainers(mQueryObject = []):
"""
Return a list of containers that the passed in objects reside in.
@param [] mQueryObject: list of objects you are wanting to know, in which container they exists.
@return: key = container name, value = container MObject.
@rtype: {}
"""
containerDict = {}
nodeFn = om2.MFnContainerNode()
selNodeFn = om2.MFnDependencyNode()
containerObjs = getAllDagContainers()
for selObj in mQueryObject:
for obj in containerObjs:
nodeFn.setObject(obj)
if selObj in nodeFn.getMembers():
selNodeFn.setObject(selObj)
containerName = str(nodeFn.name())
# Adds the object to a dictionary, using the container as the key
if containerDict.has_key(nodeFn.name()):
containerDict[containerName].append(selNodeFn.object())
else:
containerDict[containerName] = [selNodeFn.object()]
return containerDict
def getAllDagContainers():
"""
Will return all containers in the scene
@return: list of objects comprised of containers and DAGContainers
@rtype: []
"""
# Holds all the containers
containerObjs = []
# Search the root hierarchy for container types
# Some containers live in the DAG while others ar DG Nodes
dagIterator = om2.MItDag(om2.MItDag.kDepthFirst, om2.MFn.kDagContainer)
dgIterator = om2.MItDependencyNodes(om2.MFn.kContainer)
# TODO: Look at optomising by returning the allPaths, instead of looping
#dagArray = om.MDagPathArray(dagIterator.getAllPaths())
#return dagArray
# Node helpers
dagNodeFn = om2.MFnDagNode()
dgNodeFn = om2.MFnDependencyNode()
# Loop throught the DAG Graph (Scene)
while (not dagIterator.isDone()):
# get ths current dag node
dag_mObject = dagIterator.currentItem()
# updates dag function
dagNodeFn.setObject(dag_mObject)
containerObjs.append(dagNodeFn.object())
# gets the next item
dagIterator.next()
# Loop through the Dependency Nodes
while (not dgIterator.isDone()):
dgNodeFn.setObject(dgIterator.thisNode())
containerObjs.append(dgNodeFn.object())
dgIterator.next()
return containerObjs
def exposeSelection():
"""
NOT IMPLEMENTED YET
"""
#TODO: expose objects and parameters and look at creating input and output nodes
pass
def unexposeSelection():
"""
NOT IMPLEMENTED YET
"""
pass
def concealExposedObject(mobj):
"""
Hides the selected object, so it will no longer appear in the outliner when the container is black boxed
@param mobj: Object you wish to remove from being exposed
"""
mfnDepNode = om2.MFnDependencyNode(mobj)
msgPlug = mfnDepNode.findPlug("message", True)
for _plug in msgPlug.destinations():
# selected object is not connected to anything
if _plug.isNull:
continue
connectedNode = _plug.node()
if connectedNode.apiType() == om2.MFn.kContainer:
dgMod = om2.MDGModifier()
dgMod.disconnect(msgPlug, _plug)
dgMod.doIt()
def isContainerClosed(mobj):
"""
returns true is the container is closed
@param mobj: Container object
@type mobj: MObject
@return: True is container is cloased, False if open
@rtype: Boolean
"""
_mfnCont = om2.MFnContainerNode(mobj)
bbPlug = _mfnCont.findPlug("blackBox", True)
return bbPlug.asInt() == 1
def closeContainer(mobj):
"""
Close the container passed in
@param mobj: The container object, you wich to close
@type mobj: om.MObject
"""
_mfnCont = om2.MFnContainerNode(mobj)
bbPlug = _mfnCont.findPlug("blackBox", True)
bbPlug.setInt(1)
def openContainer(mobj):
"""
open the container passed in
@param mobj: The container object, you wich to open
@type mobj: om.MObject
"""
_mfnCont = om2.MFnContainerNode(mobj)
bbPlug = _mfnCont.findPlug("blackBox", True)
bbPlug.setInt(0)
def createContainer(name="unnamedContainer"):
"""
Creates and names the container
@param string name: The name of the container, that will be created
@return: The newly created container
@rtype: MObject
"""
dgMod = om2.MDGModifier()
containerObj = dgMod.createNode("container")
dgMod.doIt()
containerMfn = om2.MFnContainerNode(containerObj)
containerMfn.setName(name)
return containerObj
def deleteContainer(mobj):
"""
Deletes the container, and removes the containers hypergraph
@param mobj: Container that will be deleted
"""
if not mobj:
return False
_hyperLayoutDG = getContainerHyperLayout(mobj, create=False)
if _hyperLayoutDG:
node.disconnectAllPlugsOnNode(_hyperLayoutDG)
node.deleteNode(_hyperLayoutDG)
node.deleteNode(mobj)
def getContainerHyperLayout(container, create=True):
"""
Gets the hyperlayout that is connected to the container. If the container has not got one, and create is set to True,
a hyperlayout will be created and joined to the container.
@param MObject container: The container object that you want to return its hyperlayout.
@return: The hyperlayout connected to the container
@rtype: MObject
"""
_dgContainer = om2.MFnDependencyNode(container)
_hyperLPlug = _dgContainer.findPlug('hyperLayout', True)
_hyperLayoutMo = None
# check if container has a hyperlayout, is not creates one
if not _hyperLPlug.isConnected and not _hyperLPlug.isDestination:
if create:
_dgMod = om2.MDGModifier()
_hyperLayoutMo = _dgMod.createNode('hyperLayout')
_dgMod.doIt()
_hyperLayoutMfn = om2.MFnDependencyNode(_hyperLayoutMo)
_hyperLayoutMfn.setName(_dgContainer.name() + "_hyperLayout")
_dgMod.connect(_hyperLayoutMfn.findPlug("message", True), _hyperLPlug)
_dgMod.doIt()
else:
_hyperLayoutMo = _hyperLPlug.source().node()
return _hyperLayoutMo
def isDagExposed(mobj):
"""
Verify if object is exposed in a container
@param mobj: Object you wish to check
@return: return True if exposed, else False if not exposed
"""
mfnDepNode = om2.MFnDependencyNode(mobj)
msgPlug = mfnDepNode.findPlug("message",True)
for _plug in msgPlug.destinations():
if "pnod" in _plug.partialName() and _plug.node().apiType() == om2.MFn.kContainer:
return True
def exposeDagObject(mobj):
"""
Exposes the object on the container it is connected to.
@param om2.MObject mobj: DAG object you wish to expose in the container
"""
mfnDepNode = om2.MFnDependencyNode(mobj)
msgPlug = mfnDepNode.findPlug("message",True)
if isDagExposed(mobj):
return False
for _plug in msgPlug.destinations():
# selected object is not connected to anything
if _plug.isNull:
continue
connectedNode = _plug.node()
if connectedNode.apiType() == om2.MFn.kHyperLayout:
hyperLayoutDN = om2.MFnDependencyNode(connectedNode)
hlMsgPlug = hyperLayoutDN.findPlug("message", True)
# selected object is not connected to anything
if hlMsgPlug.isNull:
continue
# loop all the destination plugs
for _plugDest in hlMsgPlug.destinations():
contNode = _plugDest.node()
if contNode.apiType() == om2.MFn.kContainer:
contMfn = om2.MFnDependencyNode(contNode)
publishNodePlug = contMfn.findPlug("publishedNodeInfo", True)
availablePlug = node.getNextAvailablePlugInPlug(publishNodePlug, "publishedNode")
if availablePlug is None:
print("Error: Could not expose selection")
return False
else:
dgMod = om2.MDGModifier()
dgMod.connect(msgPlug, availablePlug)
dgMod.doIt()
def addToContainer(mobj, containerObj):
"""
Add an object to a container.
@param mobj: Object you wish to add to the container
@param om2.MObject containerObj: container that have the object added to it
"""
_hyperLayoutDG = getContainerHyperLayout(containerObj)
_hyperLayoutMfn = om2.MFnDependencyNode(_hyperLayoutDG)
_hyperLayoutMessagePlug = _hyperLayoutMfn.findPlug('hyperPosition', True)
_nextPlugIndex = _hyperLayoutMessagePlug.numElements()
_dagMod = om2.MDGModifier()
_mfnNode = om2.MFnDependencyNode()
_hyperposPlug = _hyperLayoutMessagePlug.elementByLogicalIndex(_nextPlugIndex)
_availableDependNodePlug = node.getNextAvailablePlugInPlug(_hyperposPlug, 'dependNode')
print("NEXT AVAILABLE dependNode : {0}".format(_availableDependNodePlug))
_mfnNode.setObject(mobj)
_msgPlug = _mfnNode.findPlug('message', True)
_badConnection = node.isSourceConnectedTo(_msgPlug, nodeType='hyperLayout')
# TODO: check if you are trying to add the object to the container it is already added to
if _availableDependNodePlug and not _badConnection:
_dagMod.connect(_msgPlug, _availableDependNodePlug)
_dagMod.doIt()
_nextPlugIndex += 1
else:
print("ERROR: Object not added to container!")
def removeFromContainer(mobj, containerObj):
pass