def add_dot(self, pos, col='black', radius=1.0, outline=None, thickness=None, operation=None, render_hint=None): """Draw a circle on this pixmap. Args: pos (QPoint): centre point col (str): dot colour radius (float): dot radius outline (QPen): apply outline pen thickness (float): line thickness operation (str): compositing operation render_hint (RenderHint): add render hint """ from psyhive import qt _pos = qt.get_p(pos) _col = qt.get_col(col) _brush = QtGui.QBrush(_col) # Set outline if thickness: _pen = QtGui.QPen(qt.get_col('Black')) _pen.setWidthF(thickness) elif not outline: _pen = QtGui.QPen(_col) _pen.setStyle(Qt.NoPen) elif isinstance(outline, QtGui.QPen): _pen = outline elif isinstance(outline, six.string_types): _out_col = qt.get_col(outline) _pen = QtGui.QPen(_out_col) else: raise ValueError(outline) _pnt = HPainter() _pnt.begin(self) _pnt.setBrush(_brush) _pnt.setPen(_pen) if render_hint: _pnt.setRenderHint(render_hint) _pnt.set_operation(operation) _pnt.drawEllipse(_pos.x() - radius, _pos.y() - radius, radius * 2, radius * 2) _pnt.end()
def add_square(self, pos, size, col='black', thickness=None): """Draw a square. Args: pos (QPoint): square position size (QSize): square size col (str): square colour thickness (float): line thickness """ from psyhive import qt _pos = qt.get_p(pos) _size = qt.get_size(size) _rect = QtCore.QRect(_pos, _size) _brush = QtGui.QBrush(qt.HColor(0, 0, 0, 0)) _col = qt.get_col(col) _pen = QtGui.QPen(_col) if thickness: _pen.setWidthF(thickness) _pnt = qt.HPainter() _pnt.begin(self) _pnt.setPen(_pen) _pnt.setBrush(_brush) _pnt.drawRect(_rect) _pnt.end() return _rect
def add_circle(self, pos, col='black', radius=10, thickness=None, operation=None, pen=None): """Draw a circle on this pixmap. Args: pos (QPoint): centre point col (str): line colour radius (int): circle radius thickness (float): line thickness operation (str): compositing operation pen (QPen): override pen """ from psyhive import qt _pos = qt.get_p(pos) _col = qt.get_col(col) _pen = pen or QtGui.QPen(_col) if thickness: _pen.setWidthF(thickness) _rect = QtCore.QRect(_pos.x() - radius, _pos.y() - radius, radius * 2, radius * 2) _pnt = HPainter() _pnt.begin(self) _pnt.set_operation(operation) _pnt.setPen(_pen) _pnt.drawArc(_rect, 0, 360 * 16) _pnt.end() return _rect
def add_polygon(self, pts, col, outline='black', thickness=1.0): """Draw a polygon on this pixmap. Args: pts (QPointF list): polygon points col (QColor): fill colour outline (str|None): outline colour (if any) thickness (float): line thickness """ from psyhive import qt if outline: _pen = QtGui.QPen(outline) _pen.setCapStyle(Qt.RoundCap) if thickness: _pen.setWidthF(thickness) else: _pen = QtGui.QPen() _pen.setStyle(Qt.NoPen) _col = qt.get_col(col) _brush = QtGui.QBrush(_col) _poly = QtGui.QPolygonF() for _pt in pts: _pt = qt.get_p(_pt) _poly.append(_pt) _pnt = HPainter() _pnt.begin(self) _pnt.setBrush(_brush) _pnt.setPen(_pen) _pnt.drawPolygon(_poly) _pnt.end()
def set_col(self, val): """Apply a colour value to this attribute. Args: val (HColor): colour value """ _col = qt.get_col(val) cmds.setAttr(self, *_col.to_tuple(mode='float'))
def get_col(col): """Get an OpenMaya colour object. This can be used to get a colour from a name (eg. Cyna, IndianRed) or a QColor. Args: col (str|QColor): colour to create Returns: (MColor): OpenMaya colour """ _q_col = qt.get_col(col) return om.MColor(_q_col.to_tuple('float'))
def add_rounded_rect(self, pos, size, col='White', bevel=5, anchor='TL', pen=None, outline=True): """Draw a rounded rectangle on this pixmap. Args: pos (QPoint): position size (QSize): rectangle size col (str): rectangle fill colour bevel (int): edge bevel anchor (str): position anchor point pen (QPen): override pen outline (bool): show outline Returns: (QRect): draw region """ from psyhive import qt if isinstance(col, QtGui.QPixmap): _col = col else: _col = qt.get_col(col) _brush = QtGui.QBrush(_col) _rect = _get_rect(pos=pos, size=size, anchor=anchor) # Set pen _pen = None if pen: _pen = pen elif not outline: _pen = QtGui.QPen(_col) _pen.setStyle(Qt.NoPen) _pnt = qt.HPainter() _pnt.begin(self) if _pen: _pnt.setPen(_pen) _pnt.setBrush(_brush) _pnt.drawRoundedRect(_rect, bevel, bevel) _pnt.end() return _rect
def add_rect(self, pos, size, col='white', outline='black', operation=None, anchor='TL', thickness=None): """Draw a rectangle on this pixmap. Args: pos (QPoint): position size (QSize): rectangle size col (str): rectangle colour outline (str): outline colour operation (str): overlay mode anchor (str): position anchor point thickness (float): line thickness """ from psyhive import qt _col = qt.get_col(col) _brush = QtGui.QBrush(_col) _rect = _get_rect(pos=pos, size=size, anchor=anchor) # Set outline if outline: _pen = QtGui.QPen(outline) else: _pen = QtGui.QPen() _pen.setStyle(Qt.NoPen) if thickness: _pen.setWidthF(thickness) _pnt = HPainter() _pnt.begin(self) _pnt.set_operation(operation) _pnt.setPen(_pen) _pnt.setBrush(_brush) _pnt.drawRect(_rect) _pnt.end() return _rect
def add_line(self, pt1, pt2, col='black', thickness=None, operation=None, pen=None, verbose=0): """Draw a straight line on this pixmap. Args: pt1 (QPoint): start point pt2 (QPoint): end point col (str): line colour thickness (float): line thickness operation (str): compositing operation pen (QPen): override pen (ignores all other pen attrs) verbose (int): print process data """ from psyhive import qt _pt1 = qt.get_p(pt1) _pt2 = qt.get_p(pt2) # Get pen if pen: _pen = pen else: _col = qt.get_col(col) _pen = QtGui.QPen(_col) _pen.setCapStyle(Qt.RoundCap) _pen.setJoinStyle(Qt.RoundJoin) if thickness: _pen.setWidthF(thickness) lprint("COL", _col, verbose=verbose) _pnt = HPainter() _pnt.begin(self) _pnt.setPen(_pen) _pnt.set_operation(operation) _pnt.drawLine(_pt1.x(), _pt1.y(), _pt2.x(), _pt2.y()) _pnt.end()
def set_col(self, col, verbose=0): """Set this node's main col attr. Args: col (str|tuple|QColor|HPlug): colour to apply (an existing path will be applied as a texture) verbose (int): print process data """ if isinstance(col, hom.HPlug): lprint("CONNECTING PLUG", col, verbose=verbose) col.connect(self.col_attr) return if isinstance(col, six.string_types) and os.path.exists(col): lprint("APPLYING FILE TEXTURE", col, verbose=verbose) self.apply_texture(col) return # Apply colour as value _col = qt.get_col(col) lprint("APPLYING COLOUR", _col, verbose=verbose) cmds.setAttr(self.col_attr, *_col.to_tuple(mode='float'), type='double3')
def add_path(self, pts, col='black', thickness=None, pen=None): """Draw a path on this pixmap. Args: pts (QPoint list): list of points in path col (str): path colour thickness (float): line thickness pen (QPen): override pen (ignores all other pen attrs) """ from psyhive import qt # Set pen if pen: _pen = pen else: _col = qt.get_col(col) _pen = pen or QtGui.QPen(_col) _pen.setCapStyle(Qt.RoundCap) if thickness: _pen.setWidthF(thickness) _brush = QtGui.QBrush() _brush.setStyle(Qt.NoBrush) # Make path object _path = QtGui.QPainterPath() _path.moveTo(qt.get_p(pts[0])) for _pt in pts[1:]: _path.lineTo(qt.get_p(_pt)) _pnt = HPainter() _pnt.begin(self) _pnt.setPen(_pen) _pnt.setBrush(_brush) _pnt.drawPath(_path) _pnt.end()
def _add_psyhive_btn(label, icon, cmd, tooltip, add_dots=True, verbose=0): """Add styled button to PsyHive shelf. Args: label (str): button label icon (str): button icon name cmd (fn): button command tooltip (str): button tooltip add_dots (bool): add speckled dots to button background verbose (int): print process data Returns: (str): button element """ global _BUTTON_IDX # Set name/tmp_file lprint('ADDING', label, verbose=verbose) _name = 'PsyHive_' + label for _find, _replace in [('/', ''), (' ', ''), ('\n', '')]: _name = _name.replace(_find, _replace) _tmp_file = '{}/pixmaps/{}.png'.format(tempfile.gettempdir(), _name) _rand = str_to_seed(_name) lprint(' - NAME', _name, verbose=verbose) # Get colour _cols = ['RoyalBlue', 'CornflowerBlue', 'DodgerBlue'] _col_name = _rand.choice(_cols) _col = qt.get_col(_col_name) lprint(' - COL NAME', _col_name, verbose=verbose) # Draw base _pix = qt.HPixmap(32, 32) _pix.fill('Transparent') _col = _col.whiten(0.3) _pix.add_rounded_rect(pos=(0, 0), size=(32, 32), col=_col, outline=None, bevel=4) _col = _col.whiten(0.3) _pix.add_rounded_rect(pos=(2, 2), size=(28, 28), col=_col, outline=None) if add_dots: for _ in range(8): if _rand.random() > 0.3: _pos = qt.get_p([int(33 * _rand.random()) for _ in range(2)]) _rad = ValueRange('2-6').rand(random_=_rand) _alpha = ValueRange('80-100').rand(random_=_rand) _col = QtGui.QColor(255, 255, 255, _alpha) _pix.add_dot(pos=_pos, radius=_rad, col=_col) # Add icon if icon: _pix.add_overlay(icon, pos=qt.get_p(15, 2), resize=12, anchor='T') # Add text _lines = label.split('\n') for _jdx, _line in enumerate(_lines): _r_jdx = len(_lines) - _jdx - 1 _pix.add_text(_line, pos=qt.get_p(16, 31 - _r_jdx * 7), font=_get_btn_font(), anchor='B') _pix.save_as(_tmp_file, force=True) lprint(' - TMP FILE', _tmp_file, verbose=verbose) _btn = ui.add_shelf_button(_name, image=_tmp_file, command=cmd, parent='PsyHive', annotation=tooltip) lprint(verbose=verbose) _BUTTON_IDX += 1 return _btn
def add_text(self, text, pos=(0, 0), anchor='TL', col='white', font=None, size=None, verbose=0): """Write text to the image. Args: text (str): text to add pos (tuple|QPoint): text position anchor (str): text anchor col (str|QColor): text colour font (QFont): text font size (int): apply font size verbose (int): print process data """ from psyhive.qt import get_p, get_col lprint("Adding text", text, verbose=verbose) _window = self.window() _pos = get_p(pos) _x, _y = _pos.x(), _pos.y() _w, _h = _window.width(), _window.height() if anchor == 'BL': _rect = QtCore.QRect(_x, 0, _w - _x, _y) _align = Qt.AlignLeft | Qt.AlignBottom elif anchor == 'BR': _rect = QtCore.QRect(0, 0, _x, _y) _align = Qt.AlignRight | Qt.AlignBottom elif anchor == 'B': _rect = QtCore.QRect(0, 0, 2 * _x, _y) _align = Qt.AlignHCenter | Qt.AlignBottom elif anchor == 'C': _rect = QtCore.QRect(0, 0, 2 * _x, 2 * _y) _align = Qt.AlignHCenter | Qt.AlignVCenter elif anchor == 'L': _rect = QtCore.QRect(_x, 0, _w, 2 * _y) _align = Qt.AlignVCenter | Qt.AlignLeft elif anchor == 'R': _rect = QtCore.QRect(0, 0, _x, 2 * _y) _align = Qt.AlignRight | Qt.AlignVCenter elif anchor in ('T', 'TC'): _rect = QtCore.QRect(0, _y, 2 * _x, _h) _align = Qt.AlignHCenter | Qt.AlignTop elif anchor == 'TL': _rect = QtCore.QRect(_x, _y, _w, _h) _align = Qt.AlignLeft | Qt.AlignTop elif anchor == 'TR': _rect = QtCore.QRect(0, _y, _x, _h - _y) _align = Qt.AlignRight | Qt.AlignTop else: raise ValueError('Unhandled anchor: %s' % anchor) if font: self.setFont(font) elif size is not None: _font = QtGui.QFont() _font.setPointSize(size) self.setFont(_font) # Draw text self.setPen(get_col(col or 'white')) self.drawText(_rect, _align, text)