1 import maya.OpenMaya 
as om
 
    5 __all__ = [ 
'profilerToJSON', 
'profilerToCSV', 
'profilerFormatJSON']
 
   18 def profilerToJSON(fileName, useIndex, durationMin):
 
   20         fileName : name of file to write to disk 
   21         useIndex : write events using index lookup to category and name lists 
   22         durationMin : only write out events which have at least this minimum time duration 
   25             Sample code to extract profiler information and write to file in JSON format 
   28             > profilerToJSON('profiler_indexed.json', True, 0.0) # Index without a duration clamp 
   29             > profilerToJSON('profiler_nonIndexed.json', False, 10.0) # Non-Indexed with duration clamp 
  101     stripped = 
lambda s: 
"".join(i 
for i 
in s 
if 31 < ord(i) < 127)
 
  103     eventCount = om.MProfiler.getEventCount()
 
  107     file = open(fileName, 
"w")
 
  115     file.write(
"\t\"version\": 1,\n")
 
  117     file.write(
"\t\"eventCount\": " + str(eventCount) + 
",\n")
 
  119     file.write(
"\t\"cpuCount\": " + str(om.MProfiler.getNumberOfCPUs()) + 
",\n")
 
  123     om.MProfiler.getAllCategories(categories)
 
  124     asciiString = json.dumps(categories, 
True, 
True)
 
  126         file.write(
"\t\"categories\": " + asciiString + 
",\n")
 
  130     for i 
in range(0, eventCount, 1):
 
  131         eventName = om.MProfiler.getEventName(i)
 
  132         eventName = eventName.decode(
'ascii', 
'replace')
 
  133         eventName = stripped(eventName)
 
  134         if eventName 
not in nameDict:
 
  135             nameDict[eventName] = len(nameDict)
 
  137         nameString = json.dumps(nameDict.keys(), 
True, 
True)
 
  138         file.write(
'\t\"eventNames\" : ' + nameString + 
",\n")
 
  142     file.write(
'\t\"events\": [\n')
 
  145     for i 
in range(0, eventCount):
 
  147         duration = om.MProfiler.getEventDuration(i)
 
  148         if duration > durationMin:
 
  149             eventsWritten = eventsWritten + 1
 
  151             eventTime = om.MProfiler.getEventTime(i)
 
  152             eventName = om.MProfiler.getEventName(i)
 
  153             eventName = eventName.decode(
'ascii', 
'replace')
 
  154             eventName = stripped(eventName)
 
  156                 eventNameIndex = nameDict.keys().index(eventName)
 
  159             if om.MProfiler.getDescription(i):
 
  160                 description = om.MProfiler.getDescription(i)
 
  162             eventCategory = om.MProfiler.getEventCategory(i)
 
  163             eventCategoryName = om.MProfiler.getCategoryName(eventCategory)
 
  165                 eventCatagoryIndex = categories.index(eventCategoryName)
 
  167             threadDuration = om.MProfiler.getThreadDuration(i)
 
  169             threadId = om.MProfiler.getThreadId(i)
 
  171             cpuId = om.MProfiler.getCPUId(i)
 
  173             colorId = om.MProfiler.getColor(i)
 
  181             file.write(
'\"time\" : ' + str(eventTime) + 
', ')
 
  183                 file.write(
'\"nameIdx\" : ' + str(eventNameIndex) + 
', ')
 
  185                 file.write(
'\"name\" : \"' + eventName + 
'\", ')
 
  186             file.write(
'\"desc\" : \"' + str(description) + 
'\", ')
 
  188                 file.write(
'\"catIdx\" : ' + str(eventCatagoryIndex) + 
', ')
 
  190                 file.write(
'\"category\" : \"' + eventCategoryName + 
'\", ')
 
  191             file.write(
'\"duration\" : ' + str(duration) + 
', ')
 
  192             file.write(
'\"tDuration\" : ' + str(threadDuration) + 
', ')
 
  193             file.write(
'\"tId\" : ' + str(threadId) + 
', ')
 
  194             file.write(
'\"cpuId\" : ' + str(cpuId) + 
', ')
 
  195             file.write(
'\"colorId\" : ' + str(colorId) + 
'')
 
  199     file.write(
"\t\"eventsWritten\": " + str(eventsWritten) + 
"\n")
 
  203 def profilerFormatJSON(fileName, fileName2):
 
  205         fileName : name of file to read 
  206         fileName2 : name of file to write to 
  209             Simple utility code to read a JSON file sort and format it before 
  210             writing to a secondary file. 
  213             > profilerFormatJSON('profilerIn.json', 'profilerFormatted.json') 
  216     file = open(fileName, 
"r") 
  220     result = json.load(file)
 
  223     dump = json.dumps(result, sort_keys=
True, indent=4)
 
  225     file2 = open(fileName2, 
"w")
 
  238 def profilerToCSV(fileName, durationMin):
 
  240         fileName : name of file to write to disk 
  241         useIndex : write events using index lookup to category and name lists 
  242         durationMin : only write out events which have at least this minimum time duration 
  245             Sample to output profiler event information only to CSV format. 
  247             > profilerToCSV('profiler.csv', 0.0) 
  261     stripped = 
lambda s: 
"".join(i 
for i 
in s 
if 31 < ord(i) < 127)
 
  263     eventCount = om.MProfiler.getEventCount()
 
  267     file = open(fileName, 
"w")
 
  271     csvWriter = csv.writer(file, quoting=csv.QUOTE_NONNUMERIC)
 
  276     head = ( 
'Event Time', 
'Event Name', 
'Description', 
'Event Category', 
'Duration', 
'Thread Duration', 
'Thread Id', 
'CPU Id', 
'Color Id' )
 
  277     csvWriter.writerow(head)
 
  279     for i 
in range(0, eventCount):
 
  281         duration = om.MProfiler.getEventDuration(i)
 
  282         if duration > durationMin:
 
  284             eventTime = om.MProfiler.getEventTime(i)
 
  285             eventName = om.MProfiler.getEventName(i)
 
  286             eventName = eventName.decode(
'ascii', 
'replace')
 
  287             eventName = stripped(eventName)
 
  290             if om.MProfiler.getDescription(i):
 
  291                 description = om.MProfiler.getDescription(i)
 
  293             eventCategory = om.MProfiler.getEventCategory(i)
 
  294             eventCategoryName = om.MProfiler.getCategoryName(eventCategory)
 
  296             threadDuration = om.MProfiler.getThreadDuration(i)
 
  298             threadId = om.MProfiler.getThreadId(i)
 
  300             cpuId = om.MProfiler.getCPUId(i)
 
  302             colorId = om.MProfiler.getColor(i)
 
  304             row = ( eventTime, eventName, description, eventCategoryName, duration, threadDuration, threadId, cpuId, colorId )
 
  306             csvWriter.writerow(row)
 
  312 def initializePlugin(obj):
 
  315 def uninitializePlugin(obj):