13
13
logger .setLevel (logging .INFO )
14
14
15
15
16
- def delete_unused_nodes ():
16
+ def delete_unused_nodes (verbose = True ):
17
17
"""
18
18
Deleted unused nodes (such as materials not connected to anything or nodes without any connections)
19
19
This is done through a Maya MEL function "MLdeleteUnused()" but it's called here again for better feedback.
20
+ Args:
21
+ verbose (bool, optional): If True, it will print feedback with the number of unused deleted nodes.
22
+ Returns:
23
+ int: Number of unused deleted nodes.
20
24
"""
21
25
num_deleted_nodes = mel .eval ('MLdeleteUnused();' )
22
- feedback = FeedbackMessage (quantity = num_deleted_nodes ,
23
- singular = 'unused node was' ,
24
- plural = 'unused nodes were' ,
25
- conclusion = 'deleted.' ,
26
- zero_overwrite_message = 'No unused nodes found in this scene.' )
27
- feedback .print_inview_message ()
26
+ if verbose :
27
+ feedback = FeedbackMessage (quantity = num_deleted_nodes ,
28
+ singular = 'unused node was' ,
29
+ plural = 'unused nodes were' ,
30
+ conclusion = 'deleted.' ,
31
+ zero_overwrite_message = 'No unused nodes found in this scene.' )
32
+ feedback .print_inview_message ()
33
+ return num_deleted_nodes
28
34
29
35
30
- def delete_nucleus_nodes ():
31
- """ Deletes all elements related to particles """
36
+ def delete_nucleus_nodes (verbose = True , include_fields = True ):
37
+ """
38
+ Deletes all elements related to particles.
39
+ Args:
40
+ verbose (bool, optional): If True, it will print feedback with the number of deleted nodes.
41
+ include_fields (bool, optional): If True, it will also count field as "nucleus nodes" to be deleted.
42
+ Returns:
43
+ int: Number of nucleus deleted nodes.
44
+ """
32
45
errors = ''
33
46
function_name = 'Delete Nucleus Nodes'
47
+ deleted_counter = 0
34
48
try :
35
49
cmds .undoInfo (openChunk = True , chunkName = function_name )
36
50
37
- # Without Transform
38
- emitters = cmds .ls (typ = 'pointEmitter' )
39
- solvers = cmds .ls (typ = 'nucleus' )
40
- instancers = cmds .ls (typ = 'instancer' )
41
-
42
- no_transforms = emitters + instancers + solvers + instancers
51
+ # Without Transform Types
52
+ no_transform_types = ['nucleus' ,
53
+ 'pointEmitter' ,
54
+ 'instancer' ]
55
+ # Fields/Solvers Types
56
+ if include_fields :
57
+ field_types = ['airField' ,
58
+ 'dragField' ,
59
+ 'newtonField' ,
60
+ 'radialField' ,
61
+ 'turbulenceField' ,
62
+ 'uniformField' ,
63
+ 'vortexField' ,
64
+ 'volumeAxisField' ]
65
+ no_transform_types += field_types
66
+ no_transforms = []
67
+ for node_type in no_transform_types :
68
+ no_transforms += cmds .ls (typ = node_type ) or []
43
69
44
70
# With Transform
45
- nparticle_nodes = cmds .ls (typ = 'nParticle' )
46
- spring_nodes = cmds .ls (typ = 'spring' )
47
- particle_nodes = cmds .ls (typ = 'particle' )
48
- nrigid_nodes = cmds .ls (typ = 'nRigid' )
49
- ncloth_nodes = cmds .ls (typ = 'nCloth' )
50
- pfxhair_nodes = cmds .ls (typ = 'pfxHair' )
51
- hair_nodes = cmds .ls (typ = 'hairSystem' )
52
- nconstraint_nodes = cmds .ls (typ = 'dynamicConstraint' )
53
-
54
- transforms = nparticle_nodes + spring_nodes + particle_nodes + nrigid_nodes
55
- transforms += ncloth_nodes + pfxhair_nodes + hair_nodes + nconstraint_nodes
56
-
57
- # Fields/Solvers Types
58
- # airField
59
- # dragField
60
- # newtonField
61
- # radialField
62
- # turbulenceField
63
- # uniformField
64
- # vortexField
65
- # volumeAxisField
66
-
67
- deleted_counter = 0
68
- for obj in transforms :
71
+ with_transform_types = ['nParticle' ,
72
+ 'spring' ,
73
+ 'particle' ,
74
+ 'nRigid' ,
75
+ 'nCloth' ,
76
+ 'pfxHair' ,
77
+ 'hairSystem' ,
78
+ 'dynamicConstraint' ]
79
+ with_transforms = []
80
+ for transform_node_type in with_transform_types :
81
+ with_transforms += cmds .ls (typ = transform_node_type ) or []
82
+
83
+ for obj in with_transforms :
69
84
try :
70
85
parent = cmds .listRelatives (obj , parent = True ) or []
71
86
cmds .delete (parent [0 ])
@@ -78,13 +93,13 @@ def delete_nucleus_nodes():
78
93
deleted_counter += 1
79
94
except Exception as e :
80
95
logger .debug (str (e ))
81
-
82
- feedback = FeedbackMessage (quantity = deleted_counter ,
83
- singular = 'object was' ,
84
- plural = 'objects were' ,
85
- conclusion = 'deleted.' ,
86
- zero_overwrite_message = 'No nucleus nodes found in this scene.' )
87
- feedback .print_inview_message ()
96
+ if verbose :
97
+ feedback = FeedbackMessage (quantity = deleted_counter ,
98
+ singular = 'object was' ,
99
+ plural = 'objects were' ,
100
+ conclusion = 'deleted.' ,
101
+ zero_overwrite_message = 'No nucleus nodes found in this scene.' )
102
+ feedback .print_inview_message ()
88
103
89
104
except Exception as e :
90
105
errors += str (e ) + '\n '
@@ -94,32 +109,50 @@ def delete_nucleus_nodes():
94
109
if errors != '' :
95
110
print ('######## Errors: ########' )
96
111
print (errors )
112
+ return deleted_counter
97
113
98
114
99
- def delete_all_locators ():
100
- """ Deletes all locators """
115
+ def delete_locators (verbose = True , filter_str = None ):
116
+ """
117
+ Deletes all locators
118
+ Args:
119
+ verbose (bool, optional): If True, it will print feedback when executing operation.
120
+ filter_str (str, optional): If provided, it will be used to filter locators.
121
+ Only locators containing this string will be deleted.
122
+ Returns:
123
+ int: Number of deleted locators
124
+ """
101
125
errors = ''
102
- function_name = 'Delete All Locators'
126
+ function_name = 'Delete Locators'
127
+ deleted_counter = 0
103
128
try :
104
129
cmds .undoInfo (openChunk = True , chunkName = function_name )
105
130
106
131
# With Transform
107
132
locators = cmds .ls (typ = 'locator' )
108
133
109
- deleted_counter = 0
110
- for obj in locators :
134
+ filtered_locators = []
135
+ if filter_str and isinstance (filter_str , str ):
136
+ for loc in locators :
137
+ if filter_str in loc :
138
+ filtered_locators .append (loc )
139
+ else :
140
+ filtered_locators = locators
141
+
142
+ for obj in filtered_locators :
111
143
try :
112
- parent = cmds .listRelatives (obj , parent = True ) or []
113
- cmds .delete (parent [0 ])
144
+ loc_transform = cmds .listRelatives (obj , parent = True ) or []
145
+ cmds .delete (loc_transform [0 ])
114
146
deleted_counter += 1
115
147
except Exception as e :
116
148
logger .debug (str (e ))
117
- feedback = FeedbackMessage (quantity = deleted_counter ,
118
- singular = 'locator was' ,
119
- plural = 'locators were' ,
120
- conclusion = 'deleted.' ,
121
- zero_overwrite_message = 'No locators found in this scene.' )
122
- feedback .print_inview_message ()
149
+ if verbose :
150
+ feedback = FeedbackMessage (quantity = deleted_counter ,
151
+ singular = 'locator was' ,
152
+ plural = 'locators were' ,
153
+ conclusion = 'deleted.' ,
154
+ zero_overwrite_message = 'No locators found in this scene.' )
155
+ feedback .print_inview_message ()
123
156
124
157
except Exception as e :
125
158
errors += str (e ) + '\n '
@@ -129,4 +162,8 @@ def delete_all_locators():
129
162
if errors != '' :
130
163
print ('######## Errors: ########' )
131
164
print (errors )
165
+ return deleted_counter
166
+
132
167
168
+ if __name__ == "__main__" :
169
+ logger .setLevel (logging .DEBUG )
0 commit comments