Repository navigation
Expand file tree
/
Copy pathmaya_get_nodes_list.py
More file actions
498 lines (338 loc) · 12 KB
/
Copy pathmaya_get_nodes_list.py
File metadata and controls
498 lines (338 loc) · 12 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# coding=utf-8
# """
# Export .obj files for target shapes for a blendshape node in Maya.
# Author: Zhao Yafei (zhaoyafei0210@gmail.com)
# """
import os
import os.path as osp
import json
import maya.cmds as cmds
import maya.mel as mel
from pprint import pprint
def get_current_scene_name():
"""
Get current scene name.
Args:
None.
Returns:
str
Scene name.
"""
scene_name = cmds.file(query=True, sceneName=True, shortName=True)
scene_name = osp.splitext(scene_name)[0]
return scene_name
def get_node_types():
"""
Get list of all registered node types.
Args:
None.
Returns:
list of str
Name list of all registered node types.
"""
node_types_list = cmds.ls(nodeTypes=True)
node_types_list.sort()
return node_types_list
def get_all_nodes_list(show_type=True):
"""
Get name list of all available nodes in current scene/projects.
Args:
show_type: boolean
Whether to show the node type of each shape node.
Returns:
list of str
Name list of all available nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
nodes_list = cmds.ls(showType=show_type)
if not show_type:
nodes_list.sort()
return nodes_list
def get_all_mesh_nodes(show_type=True):
"""
Get name list of all mesh nodes.
Args:
show_type: boolean
Whether to show the node type of each shape node.
Returns:
list of str
Name list of all meshes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
# mesh_nodes_list = cmds.lsType("mesh")
mesh_nodes_list = cmds.ls(type="mesh", showType=show_type)
if not show_type:
mesh_nodes_list.sort()
return mesh_nodes_list
def get_all_blendshape_nodes(show_type=True):
"""
Get name list of all blendshape nodes.
Args:
show_type: boolean
Whether to show the node type of each shape node.
Returns:
list of str
Name list of all blendshape nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
# blendshape_nodes_list = cmds.lsType("blendShape")
blendshape_nodes_list = cmds.ls(type="blendShape", showType=show_type)
if not show_type:
blendshape_nodes_list.sort()
return blendshape_nodes_list
def get_all_joint_nodes(show_type=True):
"""
Get name list of all joint nodes.
Args:
show_type: boolean
Whether to show the node type of each shape node.
Returns:
list of str
Name list of all joint nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
# joint_nodes_list = cmds.lsType("joint")
joint_nodes_list = cmds.ls(type="joint", showType=show_type)
if not show_type:
joint_nodes_list.sort()
return joint_nodes_list
def get_all_shape_nodes(show_type=True):
"""
Get name list of all shape nodes.
(Shapes in Maya are selectable DAG objects that display in 3D views.
Meshes, NURBS surfaces and curves, and locators are just some examples of shapes.)
Args:
show_type: boolean
Whether to show the node type of each shape node.
Returns:
list of str
Name list of all shape nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
shape_nodes_list = cmds.ls(shapes=True, showType=show_type)
if not show_type:
shape_nodes_list.sort()
return shape_nodes_list
def get_all_transform_nodes(show_type=True):
"""
Get name list of all transform nodes.
Args:
show_type: boolean
Whether to show the node type of each transform node.
Returns:
list of str
Name list of all transform nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
transform_nodes_list = cmds.ls(transforms=True, showType=show_type)
if not show_type:
transform_nodes_list.sort()
return transform_nodes_list
def get_all_geometry_nodes(show_type=True):
"""
Get name list of all geometry nodes.
Args:
show_type: boolean
Whether to show the node type of each geometry node.
Returns:
list of str
Name list of all geometry nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
geometry_nodes_list = cmds.ls(geometry=True, showType=show_type)
if not show_type:
geometry_nodes_list.sort()
return geometry_nodes_list
def get_all_camera_nodes(show_type=True):
"""
Get name list of all camera nodes.
Args:
show_type: boolean
Whether to show the node type of each camera node.
Returns:
list of str
Name list of all camera nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
camera_nodes_list = cmds.ls(cameras=True, showType=show_type)
if not show_type:
camera_nodes_list.sort()
return camera_nodes_list
def get_all_light_nodes(show_type=True):
"""
Get name list of all light nodes.
Args:
show_type: boolean
Whether to show the node type of each light node.
Returns:
list of str
Name list of all light nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
light_nodes_list = cmds.ls(lights=True, showType=show_type)
if not show_type:
light_nodes_list.sort()
return light_nodes_list
def get_all_material_nodes(show_type=True):
"""
Get name list of all material nodes.
Args:
show_type: boolean
Whether to show the node type of each material node.
Returns:
list of str
Name list of all material nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
material_nodes_list = cmds.ls(materials=True, showType=show_type)
if not show_type:
material_nodes_list.sort()
return material_nodes_list
def get_all_texture_nodes(show_type=True):
"""
Get name list of all texture nodes.
Args:
show_type: boolean
Whether to show the node type of each texture node.
Returns:
list of str
Name list of all texture nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
texture_nodes_list = cmds.ls(textures=True, showType=show_type)
if not show_type:
texture_nodes_list.sort()
return texture_nodes_list
def get_all_container_nodes(show_type=True):
"""
Get name list of all container nodes.
Args:
show_type: boolean
Whether to show the node type of each container node.
Returns:
list of str
Name list of all container nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
container_nodes_list = cmds.ls(containers=True, showType=show_type)
if not show_type:
container_nodes_list.sort()
return container_nodes_list
def get_all_plane_nodes(show_type=True):
"""
Get name list of all plane nodes.
Args:
show_type: boolean
Whether to show the node type of each plane node.
Returns:
list of str
Name list of all plane nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
plane_nodes_list = cmds.ls(planes=True, showType=show_type)
if not show_type:
plane_nodes_list.sort()
return plane_nodes_list
def get_all_partition_nodes(show_type=True):
"""
Get name list of all partition nodes.
Args:
show_type: boolean
Whether to show the node type of each partition node.
Returns:
list of str
Name list of all partition nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
partition_nodes_list = cmds.ls(partitions=True, showType=show_type)
if not show_type:
partition_nodes_list.sort()
return partition_nodes_list
def get_all_set_nodes(show_type=True):
"""
Get name list of all set nodes.
Args:
show_type: boolean
Whether to show the node type of each set node.
Returns:
list of str
Name list of all set nodes.
If show_type==True, every second item in the list is the node type of its previous item.
"""
set_nodes_list = cmds.ls(sets=True, showType=show_type)
if not show_type:
set_nodes_list.sort()
return set_nodes_list
def export_nodes_list(save_dir='./', show_type=True):
"""
Export differnet type of nodes.
Args:
save_dir: str
Directory to save .obj files for all target shapes of a blendshape node.
show_type: boolean
Whether to show the node type of each shape node.
If show_type==True, every second line in the saved file shows the node type.
Returns:
None.
"""
if not osp.exists(save_dir):
os.makedirs(save_dir)
scene_name = get_current_scene_name()
node_types_filename = osp.join(
save_dir, '{}.node_types.txt'.format(scene_name))
pprint('\n===> get all node types')
node_types_list = get_node_types()
pprint('---> all node types:')
# pprint(node_types_list)
pprint('\n===> {} node types in total'.format(len(node_types_list)))
pprint('\n===> save node types into file: ')
pprint(node_types_filename)
fp = open(node_types_filename, 'w')
if len(node_types_list) > 0:
fp.write('\n'.join(node_types_list) + '\n')
fp.close()
pprint('\n===> get all nodes')
nodes_list_filename = osp.join(
save_dir, '{}.{}_nodes.txt'.format(scene_name, 'all'))
nodes_list = get_all_nodes_list(show_type)
# for idx, node in enumerate(nodes_list):
# pprint('---> {}: {}'.format(idx+1, node))
# pprint(nodes_list)
pprint('\n===> {} node keys in total'.format(len(nodes_list)))
pprint('\n===> save all nodes list into file: ')
pprint(nodes_list_filename)
fp = open(nodes_list_filename, 'w')
if len(nodes_list) > 0:
fp.write('\n'.join(nodes_list) + '\n')
fp.close()
node_types_list = ['transform', 'shape',
'geometry', 'mesh',
'blendshape', 'joint',
'camera', 'light',
'material', 'texture',
'set', 'partition',
'plane',
'container'
]
for node_type in node_types_list:
pprint('\n===> save nodes of type {}'.format(node_type))
nodes_list_filename = osp.join(
save_dir, '{}.nodes_list.{}_nodes.txt'.format(scene_name, node_type))
get_func_name = "get_all_{}_nodes".format(node_type)
nodes_list = eval(get_func_name)(show_type)
# for idx, node in enumerate(nodes_list):
# pprint('---> {}: {}'.format(idx+1, node))
# pprint(nodes_list)
pprint('\n===> {} nodes in total'.format(len(nodes_list)))
pprint('\n===> save list of nodes (type: {}) into file: '.format(node_type))
pprint(nodes_list_filename)
fp = open(nodes_list_filename, 'w')
if len(nodes_list) > 0:
fp.write('\n'.join(nodes_list) + '\n')
fp.close()
if __name__ == '__main__':
# blendshape_node_name = r'Head01_blendShape'
# mesh_node_name = r'Head01Shape'
save_dir = r'/Users/zhaoyafei/Downloads/bs_definition_3D_face/maya_exports'
show_type = True
export_nodes_list(save_dir, show_type)