Calling mu's glyph.drawNameValuePairs() from python?

While working on a python plugin, I was wondering if I could call the nice drawNameValuePairs() mu function? In one of the MU Plugin's I have found I have seen examples things like:



setupProjection(domain.x, domain.y);

let margin  = state.config.bevelMargin,
    x       = _x + margin,
    y       = _y + margin,
    tbox    = drawNameValuePairs(expandNameValuePairs(attrs),
                                 fg, bg, x, y, margin)._0,
    emin    = vec2f(_x, _y),
    emax    = emin + tbox + vec2f(margin*2.0, 0.0);

if (_inCloseArea)
    drawCloseButton(x - margin/2,
                    tbox.y + y - margin - margin/4,
                    margin/2, bg, fg);

this.updateBounds(emin, emax);

Is there a way to do something similar in python using eval() calls? Or would my best bet to just try and do this sort of thing in python gl directly?





  • 0
    Alan Trombla

    Hi Robyn,

    Yes there are several ways to do this in python:

    1. You can do everything through rv.runtime.eval(), but this gets messy and complicated (since it's hard to get info back from eval(), all the evals happen in the same (global) namespace, etc.
    2. You can import PyOpenGL and do whatever.  This has all the usual disadvantages of importing compiled python modules into RV (compiler/library mismatches, etc) and obviously means you can't leverage any existing Mu utility functions.
    3. You can import the symbols you need from Mu into Python and use them there.

    I think (3) is a good way to go when you don't have many symbols to import _and_ the types of args and return values are not too complicated (otherwise they may not go back and forth between Mu and Python nicely.

    Here's a specific example for drawNameValuePairs:

    First you need to get the MuSymbol constructor from the pymu module:

    from pymu import MuSymbol

    Then, assume you're in the render() method of your Python MinorMode (so you have a RenderEvent "event").  Then this code should do the job:

        MuSymbol("glyph.glSetupProjection")(int(event.domain()[0]), int(event.domain()[1]))
        names = [('name1', 'val1'), 
                 ('name2', 'val2'),
                 ('name3', 'val3')]
        fgColor = (0.75, 0.75, 0.75, 1.0)
        bgColor = (0.0, 0.0, 0.0, 0.75)
        xPos    = 300
        yPos    = 200
        margin  = 15
        MuSymbol("glyph.drawNameValuePairs")(names, fgColor, bgColor, xPos, yPos, margin)

    In production code, I'd move the MuSymbol calls to the constructor of the Mode, since there's no reason to look up the symbols every render ...

    Hope that helps,



  • 0
    Robyn Rindge

    Wow, this is helpful. I'll give it a shot. Thanks!