So I am making a few scripts on Maya 2020 (python 2.7). At some point I need to run some functions that do bulk operations on different assets, and they all output what they do in the script editor. I.E when deleting unused materials, I use this script:
import pymel.core as pm
pm.mel.MLdeleteUnused()
Which outputs something like this:
delete "anisotropic1";
delete "blinn1";
delete "standardSurface1";
// Error: line 0: Non-deletable node 'standardSurface1' cannot be deleted. //
I'd love to be able to print out a count of the materials deleted, which I think the easiest way would be to somehow count the prints? And that would allow me to use the same thing on other functions that also print similar outputs.
I can't really interact with MLdeleteUnused() AFAIK, and ideally I'd also like to count only the lines that indeed deleted something (Maya 2020 have this bug where it tries to delete materials that shouldn't be, leading to error and I don't want my script to require an update to fix that bug).
I would redirect your print statements to a file, you could then read them in and count the lines with delete
import sys
import pymel.core as pm
import re
sys.stdout = open('output.txt','a')
pm.mel.MLdeleteUnused()
cnt = 0
with open('output.txt', 'r') as lines:
for line in lines:
if re.match(r'delete.*', line):
cnt += 1
print(f'Number of Deletes:{cnt})