Maya API questions

I have a few questions dealing with the Maya API:

			
MObject obj;
list.getDependNode(i, obj);
MFnDependencyNode dNode(obj);
cerr << dNode.name().asChar() << endl;

When I call dNode as a MFnDependencyNode, how is it returning itself back as a reference (dNode)? How am I able to use dNode as a class when it acts as a (From what I understand) method When I am passing it obj…

Thanks!

I don’t know if I understand your question well but when you call:

MFnDependencyNode dNode(obj);

you create an instance of MFnDependencyNode, passing an “obj” as an argument to it’s constructor.
So - MObject is just a handle to some place in the memory and MFnDependencyNode is a class that knows how to manage these data.

[QUOTE=kroopson;30832]I don’t know if I understand your question well but when you call:

MFnDependencyNode dNode(obj);

you create an instance of MFnDependencyNode, passing an “obj” as an argument to it’s constructor.
So - MObject is just a handle to some place in the memory and MFnDependencyNode is a class that knows how to manage these data.[/QUOTE]

Thanks, yes I do understand that, but how am I initializing dNode with member variables? From what I understand I am creating a structure and passing it ‘obj’? What I am curiouse is how I am both passing obj and setting dNode as a variable, that’s what confuses me, because I don’t understand how I am initializing dNode (Unless it’s a property).

Thanks again!

Maya’s API is a bit weird; it is a variant on the traditional object hierarchy. You typically interact with undifferiated ‘maya objects’ or MObjects. You might know that a given MObject is a scene transform or a node or something - but the MObject is just a wrapper around an object pointer in C++. So you need to create a more narrowly typed object using the MObject as an argument in order to get at all the functions and so on that you want to work on. So the typical routine is to get an MObject from something and then pass that object to an MFN of some kind, like the MFnDependencyNode() in your example. That gives you back an class instance which has all the functions and properties of the MFn class but still points at the same thing in the Maya scene that your MObject pointed at.

It’s roughly like doing casts from a base class to a derived class in a typed language

[QUOTE=Theodox;30837]Maya’s API is a bit weird; it is a variant on the traditional object hierarchy. You typically interact with undifferiated ‘maya objects’ or MObjects. You might know that a given MObject is a scene transform or a node or something - but the MObject is just a wrapper around an object pointer in C++. So you need to create a more narrowly typed object using the MObject as an argument in order to get at all the functions and so on that you want to work on. So the typical routine is to get an MObject from something and then pass that object to an MFN of some kind, like the MFnDependencyNode() in your example. That gives you back an class instance which has all the functions and properties of the MFn class but still points at the same thing in the Maya scene that your MObject pointed at.

It’s roughly like doing casts from a base class to a derived class in a typed language[/QUOTE]

Great thank you so much, it’s still a little confusing but I just think I have to put some time into it to fully understand. (“MObject as an argument in order to get at all the functions” I don’t understand how an argument is passed by just initializing a variable)

Have a good one, I do understand a little more!

It’s how the underlying C++ works, but it’s not a Python thing so it’s not surprising that it doesn’t feel natural. You’re basically doing C++ in another syntax. In most languages other than Python it would be something like

   
MObject *fred;
MFnMesh *fred_as_mesh = Cast<MFnMesh>(fred);

where you have to tell the language that it is OK to treat the chunk of memory that fred points at can safely be interpreted as a mesh. You’re not ‘initializing’ something – fred already exists in memory . You’re just telling the computer ‘it’s ok to treat this memory this way’. Maya just provides that functionality in a constructor.