Independent Computation Thread

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 03 March 2012   #1
Independent Computation Thread

Dear All,
I've been working with Maya API for a couple of months and I have a question about threading. I don't know much about the threading basics though. Just want to know if the case below is possible and how:

Say a node has expensive calculations which takes a long time. I want to create an independent thread that does the computation and on completion, updates the node internal data with results. Meanwhile I want to be able to continue working with maya. Just like the background rendering works.

Any response will be appreciated.

Thanks
 
Old 03 March 2012   #2
Consider the following. Create the node. In the node compute create your computation thread, and return a temporary result to maya. In the computation thread your can't interact with maya, except for the thread safe structures. When your thread finishes, it should signal maya to update the node, for example through the commandPort, or it should pop-up a message box, and you'll do the update manually.
 
Old 03 March 2012   #3
Ok. Two things:
One, you said:
In the computation thread your can't interact with maya, except for the thread safe structures.


Does this mean that I can't even update the node class's members? e.g. an array that holds the shape geometry. I want the thread to calculate the geometry and then update the node class member.

And two, I've created threads in a command plugin using maya's own thread api and while the thread is doing computation, maya is not free. I can't interact with it! so how should I create an independent thread? Os native threading? or maya's api has this feature?

I know threading is complicated and I'm asking for too much information but any keywords and tips will help me find my way around this.
 
Old 03 March 2012   #4
Research result

Ok. I figured it out... see the code below:

using QT library QtConcurrent class, I create a separate concurrent process on object creation, that belongs to object and runs an internal class method.

Now, while I'm working on maya doing whatever I do, the class function is doing it's own job.

Awesome!!

#include <tools.h>

#include <QThread>
#include <QString>
#include <qtconcurrentrun.h>

using namespace QtConcurrent;

class testNode : public MPxNode {
public:
testNode() {
continueWorking = true;
f1 = run( this, &testNode::concurrent );
};
~testNode() {
};
static void * creator() { return new testNode; };
static MStatus initialize();
virtual MStatus compute( const MPlug&, MDataBlock&);

void concurrent(){
while(contWorking) {
Sleep(3000);
MGlobal::displayInfo( MString("I'm still working!\n" ) );
}
};

public:
bool continueWorking;
QFuture<void> f1;

static MObject input;
static MObject output;
static MObject outputB;
static MTypeId typeId;
};
 
Old 03 March 2012   #5
Yes, but where do you tell the compute that the calculation is done, and it can retrieve the data?
Where do you update the node in the end of the calculation, so the compute would be called?
The way you did it is for MPxCommand.
 
Old 03 March 2012   #6
Right. The function is a class method so I guess modifying the class data would be possible. I've to test it though. I'm trying to find a way to trigger a Maya compute request, by manually setting a plug dirty. Don't really want to use events and commands and stuff. There should be a way to do this...

Let me know If you have any ideas.
 
Old 05 May 2012   #7
Did you find an easy solution?
 
Old 06 June 2012   #8
Just wondering if you've solved it by now....
__________________
~~~~
Jasper Brekelmans
Senior Character/Mocap TD, custom Tool/Pipeline development
j@brekel.com
http://www.brekel.com
twitter: @brekelj
 
Old 06 June 2012   #9
Nah, I didn't have time for it, although I had some ideas similar to those above.
 
Old 06 June 2012   #10
Guess I'm in a similar boat, got an MPxLocatorNode that spawns a thread (using win32 threads) that requests data from a network socket.

Got to the point where it updates one of the input plugs on the node but having trouble to get it to compute the output.

Even when I connect an object to the output attribute, or request it in the draw function it doesn't update.

Maybe because I set the input plug from another function than compute so I have to resort to using MPlug instead of a dataBlock, and I don't seem to be be able to do a setClean then.
__________________
~~~~
Jasper Brekelmans
Senior Character/Mocap TD, custom Tool/Pipeline development
j@brekel.com
http://www.brekel.com
twitter: @brekelj
 
Old 06 June 2012   #11
If I understand your problem correctly, you are not able to trigger an update.

Forget the threads for a second. If you connect the world matrix of a dummy locator to an attribute in your node, and set it to affect the output geometry of your node. Does moving the dummy node triggers an update?
 
Old 06 June 2012   #12
In fact if I drag on the input node manually in the channel box it updates the output correctly.
But doing it internally from the code it doesn't work.

I've attached the code in case anyone is interested.

The included mel script will create a little scene, notice how dragging the input parameter updates the null as well as drawing of dots.
Setting 'streaming' attribute to 'true' doesn't update correctly until it's set back to 'false' again.
Attached Files
File Type: zip NodeUpdateTests.zip (5.3 KB, 3 views)
__________________
~~~~
Jasper Brekelmans
Senior Character/Mocap TD, custom Tool/Pipeline development
j@brekel.com
http://www.brekel.com
twitter: @brekelj
 
Old 06 June 2012   #13
Give me a day or two to chew on this.
 
Old 06 June 2012   #14
Thanks, any help is appreciated while I also chew myself
And if it can also help out others even for the better.
__________________
~~~~
Jasper Brekelmans
Senior Character/Mocap TD, custom Tool/Pipeline development
j@brekel.com
http://www.brekel.com
twitter: @brekelj
 
Old 06 June 2012   #15
I think it's working. I set the input plug inside the thread to make the plugin dirty:

http://svn.code.sf.net/p/mymayaplug...odeUpdateTests/

Also note that d'tor is called only after deleting the node and flushUndo().
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 12:52 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.