Question on how to make a generic Custom Maya API Node

python
maya

#1

Hi All,

I would like to find out if anyone has suggestions on how to create a script that can easily be edited to create a custom Maya node with just adding in some arbitrary values of any type.

Basically, I would want to be able to specify an attribute type (string type, float type, ect), and to be able to fill in the values for those types and have the script generate a custom node easily.

Excited to hear back any suggestions.


#2

Do you need this to be its own node type? Or do you just want an empty node you can add things to?


#3

At this point of time, node type doesn’t really matter. More trying to make a system that can easily create custom nodes that can be made with a script to generate them with custom attributes as needed.


#4

In that case, most people use the network node and add custom attributes.

Creating one is as just cmds.createNode('network'); you can add attributes using cmds.addAttr()


#5

Thank you for your response @Theodox, I tried out the network node and realized that it’s not really the kind of functionality that I was hoping for. I think I didn’t explain properly what I wanted either.

I wanted to find out if I could have a .json file that acts as a “template” to fill in the data of a custom Maya API node as well as to be able to loop over multiple “templates” that will generate multiple custom nodes at the same time. I’m trying to see if there are alternative ways to create my own nodes without having to hard code a custom node?

I look forward hearing back from you.


#6

Could probably build a code generator that would parse the json files and write out a class for each of the custom nodes.


#7

What you want is doable up to a point – however you won’t actually get much benefit over simply using cmds to add custom attributes.

If you want nodes with predictable properties, you’ll basically need to have an MPxNode class for every type of node. That is good for searching – each nodetype will be unique – and for predictability, since you can then rely on the presence of named properties on your nodes. However, once you start dynamically adding properties you won’t be able to rely on the presence of different attributes – effectively you’ll have to use your custom nodes as if they were networks with custom attributes.

You can definitely write some code to make it eaiser to generate new plugin node types faster, but in the end custom nodes want to be predictable and determinate, where custom attributes are more general but less predictable. There’s not going to be much of a sweet spot in the middle, although maybe you could do a mix of a small number of base types with a larger variety of custom attributes. I’m pretty sure the only opportunity to register a new node type is when the plugin that defines it is loaded (I could be wrong about that – but that’s how I rememember it.) You need to call OpenMaya.MFnPlugin.registerNode to tell Maya about the existence of the node, and that requires a handle to the plugin. It might be possible to implement a separate command in the same plugin to create and register a new node based on, say, a json file – but you’d have to run that script every time the plugin loads and you’d have to handle the case of json files moving or changing between runs.

You will also need to decide how you want to distribute things: user’s won’t get access to your custom nodes if they don’t have the plugin(s) which define them. Network + custom attributes will not have any external dependencies.

In general, if you want the nodes to be configurable on the fly I’d stick with networks and custom attributes. MPxNodes work better as static code – that way you have built in mechanisms for version changes and updating out-of-date data.


#8

You remember correctly.


#9

I feel like this guys github project is what you are after in regards to creating a plugin using a json file.

-Sean


#10

That is kind of what I had in my head, except generating a python plugin not C++. Mostly so that it could be loaded without needing a pesky compiler.


#11

If I understood correctly what you are trying to do you should look at https://www.amazon.com/Practical-Programming-Python-Robert-Galanakis/dp/1849694729/ref=mp_s_a_1_1?ie=UTF8&qid=1530779544&sr=8-1&pi=AC_SX236_SY340_FMwebp_QL65&keywords=maya+programming

The last chapter is about this.
It builds a basic structure with a bit of metaprogramming using python’s type() and the factory pattern to build MpxNodes without all the boilerplate at runtime.
Might be what you’re looking for


#12

That’s Charles Wardlaw, I think – he’s very good.

OTOH the github says it depends on CMAKE and is linux/OSX only right now. He’s in film, a lot of those guys are Linux only.


#13

Thank you very much for sharing. I actually have this book and was about two chapters away. It definitely has the approach what I am looking for in general. It will be a great reference to work off of.


#14

Sounds like you’re after something like our MetaData API to bind, return and deal with node specific data under a managed system. Everything we do withing Red9 runs this metaData api under the hood and it’s particularly good at managing and building up rigging networks. The base of the API is open source and also on the Exchange App site:

Lots of demo videos on our vimeo page too:

https://vimeo.com/manage/videos/search/metadata, the Develop Conference talk might be of interest

All of the attr handling for dealing with, encoding and decoding complex data to attrs on the nodes is also dealt with internally, great for storing things like zeroposes out to the nodes :wink:

cheers

Mark