Querying displaySmoothness from the API

Querying displaySmoothness from the API

FirespriteNate
Advocate Advocate
616 Views
6 Replies
Message 1 of 7

Querying displaySmoothness from the API

FirespriteNate
Advocate
Advocate

You can query a nurbsCurve "display smoothness" in MEL/Python using the displaySmoothness command:

cmds.displaySmoothness('curve1', q=True, pointsWire=True)
# Result: [5]

However, I'm writing an MPxLocator using the Python API2.0 which has an input attribute that takes a nurbsCurve connection as input, and I'd like to know it's current display smoothness value so I can modify how the locator draws. I can't figure out where to query this data in the API, and I can't seem to use something like any of the MGlobal.executeCommand variants, as I don't get any values returned (at least not in the Py API).

 

Does anyone know if I can query display smoothness in the API (in any way, even via API1.0) or if there is some other, better way to make my locator draw lines with the same resolution as an input curve?

0 Likes
Accepted solutions (1)
617 Views
6 Replies
Replies (6)
Message 2 of 7

brentmc
Autodesk
Autodesk
Accepted solution

Hi,

There is no way in the API to get this value directly but you should be able to call the command from API 2.0 with a little bit of work.

In API 2.0 MGlobal has an executeCommandStringResult method. The issue is it only returns string values so you would need to define a MEL helper function to return the display smoothness as a string.

 

global proc string getCurveDisplaySmoothnessAsString(string $object)
{
    int $result[] = `displaySmoothness -q -pointsWire $object`;
    return (string)$result[0];
}

 


Then you can call this from Python as follows:

 

import maya.api.OpenMaya as om
om.MGlobal.executeCommandStringResult(r'getCurveDisplaySmoothnessAsString curve1')

 

 

Note: I think it may also be possible to get the result directly in API 1.0 using MCommandResult. The code would be very similar to this example I found online:
https://python.hotexamples.com/examples/maya/OpenMaya/MCommandResult/python-openmaya-mcommandresult-...

Brent McPherson
Principle Engineer
Message 3 of 7

FirespriteNate
Advocate
Advocate

Wow, it works! it's kinda hacky, but it works and that's the main thing. I think what I'm actually doing with it is probably pretty hacky anyway, so I can't really complain 😄 

Many thanks again Brent, some very useful info there, cheers.

0 Likes
Message 4 of 7

FirespriteNate
Advocate
Advocate

I also tried your second suggestion about using MCommandResult in API1.0 and that also works great!

It's even better than the first suggestion really, as it's closer to my initial attempt, and means I don't have to make a dummy MEL function!

Thanks a lot for the great info.

 

Message 5 of 7

brentmc
Autodesk
Autodesk

Would be cool if you posted the API 1.0 solution here for the benefit of anyone who stumbles across this post in the future. 🙂

Brent McPherson
Principle Engineer
0 Likes
Message 6 of 7

FirespriteNate
Advocate
Advocate

sure, can do, it's very similar to the code you linked to:

 

import maya.OpenMaya as om1

def getCurveSmoothness(curveName):
    """Returns the DisplaySmoothness value for the given <curveName>."""
    melCmd = 'displaySmoothness -q -pointsWire {}'.format(curveName)
    MCmdResult = om1.MCommandResult()
    om1.MGlobal.executeCommand(melCmd, MCmdResult, False, False)
    # (displaySmoothness returns an array of ints, even with only one curve)
    result = om1.MIntArray()
    MCmdResult.getResult(result)
    # but we only want to return the first (only checking one curve)
    return result[0]

 

This is called from my maya.api.OpenMaya code like so:

 

        plug = om.MPlug(node, CurveMetadataView.inputCurve)
        srcPlug = plug.source()
        if srcPlug.connectedTo(True, True):
            srcNode = srcPlug.node()
            srcNodeDag = om.MFnDagNode(srcNode).partialPathName()
            res = getCurveSmoothness(srcNodeDag)

 

 

But now I've posted this, I'm thinking.. why do I even need to use the API if I'm just calling MEL? Can't I just use cmds in my plugin code?, like this:

 

from maya import cmds

def getCurveSmoothness(node):
    """Returns the DisplaySmoothness value for the given <curveNode>."""
    value = cmds.displaySmoothness(node, q=True, pointsWire=True)
    return value[0]

 

 

 

0 Likes
Message 7 of 7

brentmc
Autodesk
Autodesk

But now I've posted this, I'm thinking.. why do I even need to use the API if I'm just calling MEL? Can't I just use cmds in my plugin code?, like this:

Doh. I had assumed there was a reason you did not want to use the command directly. 🙂 

Brent McPherson
Principle Engineer