计算Maya 2020中一个函数的打印次数。

huangapple go评论78阅读模式
英文:

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})

huangapple
  • 本文由 发表于 2023年4月7日 04:08:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定