英文:
Count the number of prints from a function on Maya 2020
问题
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:
删除 "blinn1";
删除 "standardSurface1";
// 错误:行 0: 无法删除不可删除节点 'standardSurface1' //
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).
英文:
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).
答案1
得分: 2
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('delete.*', line):
cnt += 1
print(f'Number of Deletes:{cnt}')
英文:
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})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论