Finalizing
Here is a bit more complex example to show you advanced use of the regular expressions
Our final color syntaxer | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
from PySide.QtGui import * from PySide.QtCore import * from shiboken import wrapInstance from maya.OpenMayaUI import MQtUtil class Rule(): def __init__(self, fg_color, pattern='', bg_color=None, bold=False, italic=False): self.pattern = QRegExp(pattern) self.form = QTextCharFormat() self.form.setForeground(QColor(*fg_color)) if bg_color: self.form.setBackground(QColor(*bg_color)) font = QFont('Courier New', 9) font.setBold(bold) font.setItalic(italic) self.form.setFont(font) class StdOut_Syntax(QSyntaxHighlighter): def __init__(self, parent, rules): super(StdOut_Syntax, self).__init__(parent) self.parent = parent self.rules = rules def highlightBlock(self, text): # applying each rules for rule in self.rules: pattern = rule.pattern # regexp pattern index = pattern.indexIn(text) while index >= 0: # loop through the text to find matches len = pattern.matchedLength() # length of the match self.setFormat(index, len, rule.form) # we apply the format to the match index = pattern.indexIn(text, index + len) self.setCurrentBlockState(0) def wrap(): i = 1 while i: try: se_edit = wrapInstance(long(MQtUtil.findControl('cmdScrollFieldReporter%i' % i)), QTextEdit) # we remove the old syntax and raise an exception to get out of the while assert se_edit.findChild(QSyntaxHighlighter).deleteLater() except TypeError: i += 1 # if we don't find the widget we increment ' except (AttributeError, AssertionError): break rules = [Rule((212, 160, 125), r'^//.+$', bold=True), Rule((185, 125, 255), r'^#.+$', italic=True), Rule((255, 175, 44), r'^(#|//).*(error|Error).+$')] StdOut = StdOut_Syntax(se_edit, rules) return StdOut StdOut = wrap() |
A simple translation of the above regular expressions would be ;
- line starting with // until the end of the line
- line starting with # until the end of the line
- line starting with # OR // and contains the word error or Error until the end of the line
Here is the result (move the mouse above the image to see result);
You can test new rules in real time with the two following lines ; write your rules then copy them in your syntax.py to have them everytime you launch Maya !
StdOut.rules.append(Rule((255,255,255), r'^.*?Result.*?$', bold=True)) StdOut.rehighlight()
Will add a rule to colorize each line which contains the word Result in white bold
Author: mlouala
Submitted: 2017-01-07 07:22:24 UTC
Tags: Color, script editor, syntax, script, and syntax highlighting
Software: Maya
Views: 5,762
Related Items
-
Universal UV Randomization script for Maya 1.1.0 (maya script)
$20.00 (USD) -
Render Window / viewport Batch Render Script for Maya 1.0.0 (maya script)
$20.00 (USD) -
UV Texture Editor Tools Ur-edition for Maya 2.1.2 (maya script)
$39.00 (USD) -
Nightshade UV Editor Pro for Maya 2.1.3 (maya script)
$39.90 (USD) -
Christmas Color Ball 3D Model
$20.00 (USD) -
3D Thumbnail Generator (batch script) for Maya 0.3.1 (maya script)
$20.00 (USD) -
Attr Editor for Maya 1.3.0 (maya script)
$25.00 (USD) -
Color T-Shirt 3D Model
$38.00 (USD) -
Samsung Galaxy S6 All Color Pack 3D Model
$49.99 (USD)