maya API ?? reading an external json file

372 views
Skip to first unread message

Todd Widup

unread,
May 18, 2021, 11:34:54 AM5/18/21
to python_in...@googlegroups.com
me writing maya plugins has been limited to mostly utility nodes and an occasionally deformer.  I am working on something a bit bigger and what would I use to allow the plugin to read an external user specified file, either a txt or json?  also, been wondering, is there a way to have a C++ plugin run python at all?

--
Todd Widup
Creature TD / Technical Artist
todd....@gmail.com

Marcus Ottosson

unread,
May 18, 2021, 12:50:06 PM5/18/21
to python_in...@googlegroups.com

what would I use to allow the plugin to read an external user specified file, either a txt or json?

You could make a string attribute on the node, and set MFnAttribute::isUsedAsFilename = true, which will let users browse for a file.

Once you’ve got access to a path from C++, have a look at RapidJSON or nlohmann/json for a fast or convenient option, in that order. They’re both header-only and work just fine with something like Maya. You’ll likely get more options from others, because there are just so many options here. If you drill down into more specifics about your requirements, e.g. should it be human-readable? Does it need to be JSON? Does it need to be small? Network friendly? Is the data large, like a pointcache or small like a set of attributes? Will you be serialising data? Would you need something that can be deserialised into the same data structure? Etc.

is there a way to have a C++ plugin run python at all?

Yes, you can either call on Maya’s Python from C++ via MGlobal::executePythonCommand("print('hello world!')"); or you can embed another Python yourself and call that. The latter would have the benefit of not polluting the global Python namespace and memory, and is something you could use to spin up multiple Python interpreters in parallel if your code is performance sensitive (though I’d question why you’d turn to Python in that case).


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CABBPk35P29DnsEqw3Chjv%2Biy0GuHkRFDOaf-jJx_o5moTA1e%2Bg%40mail.gmail.com.

Todd Widup

unread,
May 18, 2021, 2:00:30 PM5/18/21
to python_in...@googlegroups.com
Thanks Marcus, 
the txt file will be just a bunch of values and matrices ... it might be binary it might be ascii..not 100% yet, depends how big the files get..... was leaning towards JSon for easy of use/setup
the python Q was purely curiosity 

Alok Gandhi

unread,
May 18, 2021, 2:10:37 PM5/18/21
to python_in...@googlegroups.com
As a side note, for generic use (outside of maya) you can use boost python to call python from C++ and vice versa:




Justin Israel

unread,
May 18, 2021, 2:28:50 PM5/18/21
to python_in...@googlegroups.com
On Wed, May 19, 2021 at 6:10 AM Alok Gandhi <alok.ga...@gmail.com> wrote:
As a side note, for generic use (outside of maya) you can use boost python to call python from C++ and vice versa:

Boost is a super heavy dependency. These days pybind11 might be a better choice if your project is C++ centric:

It's header-only so you don't have to worry about boost version conflicts.

And if you have a python-centric project, cython is pretty good for being able to call into C++. It also produces a library that has no runtime link dependency to anything since it transpiles to C and only links against Python. 
 

Alok Gandhi

unread,
May 18, 2021, 2:53:55 PM5/18/21
to python_in...@googlegroups.com
Thanks Justin! pybind definitely seems interesting.

And yes, Cython is definitely great. I have used it in production previously for some generic algorithms implementations where performance was needed in python centric api interface. And it is super easy to write too.

Marcus Ottosson

unread,
May 18, 2021, 3:21:58 PM5/18/21
to python_in...@googlegroups.com

pybind11 is great, but it is increeeedibly slow to compile. Like 20 seconds for a handful of bound methods. It’s insane.

That aside, I’ve got a recent project that might make a good example of what it is and how it can be used to re-create the Maya Python API.


Justin Israel

unread,
May 18, 2021, 3:29:54 PM5/18/21
to python_in...@googlegroups.com
On Wed, May 19, 2021 at 7:21 AM Marcus Ottosson <konstr...@gmail.com> wrote:

pybind11 is great, but it is increeeedibly slow to compile. Like 20 seconds for a handful of bound methods. It’s insane.

That aside, I’ve got a recent project that might make a good example of what it is and how it can be used to re-create the Maya Python API.


Nice one! Well, slow compile time is probably better than carrying around a boost dependency and dealing with symbol conflicts :-)
 

Todd Widup

unread,
May 22, 2021, 8:04:37 PM5/22/21
to python_in...@googlegroups.com
hey  quick follow up on this...finally got a bit of time to start on this more...but getting an odd issue...or it could be a normal thing.

code for adding :

poseFilePath = tAttr.create("PoseFile", "psf", MFnData::kString);
tAttr.setUsedAsFilename(true);
tAttr.setStorable(true);
tAttr.setKeyable(false);
addAttribute(poseFilePath);
attributeAffects(poseFilePath, outputMatrix);


in my compute :

if (plug != outputMatrix)
{
if (plug == poseFilePath)
{
fileUpdate = false;
MStreamUtils::stdOutStream() << "update " << std::endl;
}
return MS::kUnknownParameter;
}



if (fileUpdate == false)
{
MPlug filePath(myNode, poseFilePath);
char file = filePath.asChar();
MStreamUtils::stdOutStream() << "bob " << std::endl;
MStreamUtils::stdOutStream() << file << std::endl;

fileUpdate = true;
}


So the issue :  when I set the file, it doesnt run the compute at all.  it will only run compute when one of the other attrs is changed.  What should I do to get it to run when the file is updated or set?

Marcus Ottosson

unread,
May 23, 2021, 5:11:27 AM5/23/21
to python_in...@googlegroups.com

It would only be computed outputMatrix is pulled, so a first step is testing that.

cmds.setAttr(yourNode + ".PoseFile", "changed.txt", type="string")
# Compute not called yet..
cmds.getAttr(yourNode + ".outputMatrix")
# Compute called

If that doesn’t happen, it’s possible strings aren’t part of normal evaluation. Try making the attribute a setInternalValue instead. It’ll notify you whenever the value changes, which is probably what you want anyway, since it can only ever be updated manually by the user or Python (rather than a connection). If that doesn’t work, then another option is to implement MNodeMessage::addNodeDirtyPlugCallback to check for it yourself.


Todd Widup

unread,
May 23, 2021, 6:06:57 PM5/23/21
to python_in...@googlegroups.com
Thanks marcus, will give that a try

Reply all
Reply to author
Forward
0 new messages