PDA

View Full Version : API Q: calling support functions from the compute method


tredeger
09-27-2006, 11:06 AM
I'm rather new to coding plug-ins through the C++ API. I'm writing a custom node and all the examples I've seen pretty much encapsulate all the calculation and performance functionality completely in the 'compute' method of the node.

I've also read in David Gould's book some confusing passages about not being able to write custom function sets.

I would like to make the code more modular and write some functions that will be called by the compute function during it's operation. This is especially important because I need to call one function recursively, which is AFAIK impossible to do with inline code. My confusion is somewhat general and may stem from some overall misconceptions on my part about how Maya exposes object data for access and manipulation. It all seems rather convoluted to me.

My question is this: in general, how should I approach writing supporting functions to be called by 'compute'? And how should I handle data access? Should the function by default operate on the same object as compute or do I need to pass it some kind of reference? Is that allowed? Can I just call the same methods that I would be calling in 'compute' or do i need to tell it about those methods somehow? Should I get all the data in compute and pass it to the sub-function or get the data handles in the sub-function directly? This last part confuses me with respect to when I am dealing with real references to the data or just copies of the data. As I understand it, I never deal with the data directly.

Yeah, these questions are DEFINITELY showing off my ignorance vis a vis the object model, so thanks mightily for any wisdom you can shed on these issues. I've only just started tackling the API and it's rather daunting. My terminology is probably all wrong too.

cheers

Robert Bateman
09-27-2006, 04:13 PM
I've also read in David Gould's book some confusing passages about not being able to write custom function sets.

I disagree with David Gould on this one. It is possible to write your own function sets. However, in your case you don't want to be using function sets in compute at all..... function sets are only useful for your own mel commands, and custom exporters - definately not useful for compute!

I would like to make the code more modular and write some functions that will be called by the compute function during it's operation.

That's entirely acceptable - just make sure it's not done as a function set! You want functions that take input values from data handles, and return values which you can set on output data handles....

There is only one law when it comes to compute, any inputs must come from the data block, and all outputs must be set via handles in the data block. You can call any functions you want in compute so long as

1. They only recieve input values from the handles within the data block that compute recieved
2. They do not manipulate or read any other parts of Maya's state (calling any mel functions, or accessing data from iterators etc).
3. All values evaluated are returned back to compute so the values can be set in the data block.

Generally speaking, if you are using a function set within a compute, you've gone wrong! (though there are exceptions, such as attaching an MFnMesh to an input mesh attr, or using an MFnAttribute derived class to access the plug data provided from the input handles).

passing references to Data handles to other functions is entirely allowed. If you are in a situation where your node MUST speak to other nodes within it's compute, you can call setMPSafe(false) within the postConstructor for a node (though ideally, you really want to avoid that if possible).

tredeger
09-27-2006, 04:51 PM
I disagree with David Gould on this one. It is possible to write your own function sets. However, in your case you don't want to be using function sets in compute at all..... function sets are only useful for your own mel commands, and custom exporters - definately not useful for compute!


I think I misunderstood Function Sets for functions in general. Silly me.


That's entirely acceptable - just make sure it's not done as a function set! You want functions that take input values from data handles, and return values which you can set on output data handles....

There is only one law when it comes to compute, any inputs must come from the data block, and all outputs must be set via handles in the data block. You can call any functions you want in compute so long as

1. They only recieve input values from the handles within the data block that compute recieved
2. They do not manipulate or read any other parts of Maya's state (calling any mel functions, or accessing data from iterators etc).
3. All values evaluated are returned back to compute so the values can be set in the data block.

passing references to Data handles to other functions is entirely allowed. If you are in a situation where your node MUST speak to other nodes within it's compute, you can call setMPSafe(false) within the postConstructor for a node (though ideally, you really want to avoid that if possible).


Thanks a ton for the help. Sorry if I sound dense, but I'd just like to clarify:

I can't just pass the data block along to my called function can I? I have to pass copies of the values, get them back and apply the actual changes inline with compute? OR if the supporting function is being called by compute, does that count as making the change to the data block from within compute?

Given that I am recursively moving across a bunch of plugs, I'd rather change their values and set them clean as I go, withing the sub-function. Otherwise, I have to create some kind of data builder that gets passed back out to compute and write code in compute to go through and parse and act on the builder?

I think maybe I don't understand handles very well either. Could you maybe give an example of getting a handle from the data block in compute and passing that to a sub-function? I am not quite clear how I should think of handles, references, and copies of values stored in variables. Do handles let me both get and set the values I'm working with or do I need to actually set the values by some other means. Gosh, I feel sooooo naive. Thanks for the patience.

cheers,
j

CGTalk Moderation
09-27-2006, 04:51 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.