C++ speed/efficience in Maya

Hey guys,

Could somebody help me in some doubt?

How faster C++ could be in Maya, compared to python API in general?

Is this something that worth to work with for what kind of programs (generally)?

Thanks in advance!

C++ plugins are obviously going to be faster than python, but are usually not necessary unless you need to do something like, for example, write a custom deformer that has to operate on a lot of vertex data while updating in realtime to support viewport playback at 30+ frames per second. Python is plenty sufficient for most things. I like to think of the C++ API as a different tool for more specific problems.

My recommendation is to build your tools and plugins in python first, and then move individual plugins to C++ if / when performance becomes a noticeable issue. Sometimes C++ plugins will do a better job of solving specific problems, but they come with tradeoffs. They take more time to develop, and you have to recompile them for each version of Maya. It’s simpler to not deal with them unless you have to, unless the goal is simply to learn. The main thing is getting a handle on what tools you have at your disposal and solving problems the most efficient way. There have been times I wrote a test plugin in python and realized it was fast enough for my purposes, so I skipped re-implementing it in C++.

TLDR: Don’t worry about speed too much and don’t feel like you have to use C++ unless you’ve run into a real (not hypothetical) speed issue with one of your tools, or you need access to functionality only available in C++. Most of the time, python will be able to handle what you need.

hey @JoDaRober!

Thanks a lot for all information you wrote!

I am trying to develop a tool that work on all vertices, edges and faces of a high res mesh.
So this tool, at the step it is now, developed in python, is taking about 80 seconds to do its job in a mesh with 100K faces.
I would like to try to learn C++ so I can see how far I can go with this, and try to shrink down this 80 seconds.
I don´t know the difficulty level of the language, but maybe I will look for it.

Thank you very much for your help! :slight_smile:

JoĂŁo

1 Like

I would recommend this course if you’re wanting to learn the C++ API: https://www.cgcircuit.com/tutorial/introduction-to-the-maya-api

1 Like

Thank you very much, @JoDaRober!

Im not sure if I could start with this one, because it says as prerequisites “intermediate level of C++”.
But thanks a lot for the suggestion. Someday I will join this training.

I very much agree with what JoDaRober said.

But I wanted to add, there’s a couple tricks that can really speed up stuff in python so you don’t have to use C++ as often. Depending on what you’re doing, getting the data all at once (or getting it through the OpenMaya api) and processing it yourself can save a bunch of time.

So the question is: what are you doing with the faces, verts, and edges?

1 Like

Hi @tfox_TD,

Yes, I noticed that. I started this tool using only cmds… so it took 33 minutes to complete the task in a mesh of 10k faces.

So I changed everything to python API, and also started to work with vectors and math instead of default constraints of Maya, like aimConstraint in order to get upvectors pointing to locators placed near from vertices.

So this time shrinked down from 33 minutes in 10k, to 80 seconds in a 100k faces mesh.

Basically, I need get normals from each vertice, set some upvectors for each one, move vertices, link some locators, reposition locators, link locators… Everything in 4 meshes. This is why its taking too long.

Im gonna try to learn C++ step by step so I can see if it help me in shrink even more this time. :slight_smile:

thanks a lot for all help @tfox_TD and @JoDaRober!

1 Like

Just make sure the cmds.spaceLocator creation isn’t whats taking up the majority of the 80 seconds, if you go c++ I’m not sure how to speed that up, unless you write your own locator command?

1 Like

hey @dive!

Thanks for the suggestion!! But the locator creation is not taking too much, because I do not create one per vertex. :slight_smile: I can reutilize most of them.
But thanks for flagging!

1 Like

Do you have numpy installed for maya? It can be a pain to get set up, but it’s really fast at this kind of stuff.

1 Like

Each python API call has a translation cost that can add up, especially if your iter loops contain a lot of API instructions.

Btw, learning to write efficient Maya Python API code can help teach you a few important lessons about writing efficient C++ Maya code too. Same goes for learning to write efficient Numpy code in Python. That can help prepare you for the C++ Eigen lib if you plan to go deeper than what the Maya API offers.

1 Like

@tfox_TD I don´t have Numpy here, also I don´t know how to install.
A beginner question: if I use Numpy for build a tool, the user will need to install something “extra” in his Maya? He will need to install Numpy in his machine too?

thanks a lot for the tips @Crispy4004!
Maybe I will try to explore deeper maya programming, so its good to know that python helps to learn C++. :slight_smile:

thanks!

Yeah, if you use numpy in your tool, the user will have to have it installed as well.
When Maya uses Python3 that won’t be as much of an issue. But right now you have to find a special compile of numpy and install it in a special way on every machine that will use it in maya.

And that sounds like something you probably don’t want to do, then.

1 Like

Like @tfox_TD mentioned, you can definitely extend the python performance by moving Maya data to Numpy, so long as you understand how to write efficient Numpy operations.

Depending on the task at hand though, you can still get quite a bit more performance out of C++. I’d wager though that most Tech Artists out there don’t encounter that need in their daily work. The deeper you get into R&D land though, the more important it becomes. Not to mention there is still a decent amount of the Maya API not exposed to python.

1 Like

Understood @tfox_TD… thanks for explanning.

I think:
if Numpy is a very common feature, which artists are used to have installed in their machines, or they are able to install it easy, I don´t see a problem to make it as a prerequisite to run the tool…

but if its hard to install and not too much common between “non-programmers”, I think the tool would become not accessible… :slight_smile:

understood @Crispy4004, thanks a lot for the information… I think Im gonna try to explore more deep! :slight_smile: