Writing the expression function

   5740   6   1
User Avatar
Member
9 posts
Joined: July 2008
Offline
Hi

I want to add the custom expression to Houdini. But my expression function needs some special structure, which would be created from the file.

I decided, that the best choice is to take the sample from toolkit and to change the callback function.

But now I have a problem. I don't know, where I can call the function which is creating the structure from the file. Because I don't understand when the functions from the sample are called and why. There is no “main” And I don't understand the order of the function calls.


Thank you very much.

P.S. I'm sure that this question is very basic, but I'm just starting with HDK so often I don't understand some easy things.

Attachments:
functions.rar (2.7 KB)

Best regards
User Avatar
Member
321 posts
Joined: July 2005
Offline
Sikorskiy
Hi

I want to add the custom expression to Houdini. But my expression function needs some special structure, which would be created from the file.

I decided, that the best choice is to take the sample from toolkit and to change the callback function.

But now I have a problem. I don't know, where I can call the function which is creating the structure from the file. Because I don't understand when the functions from the sample are called and why. There is no “main” And I don't understand the order of the function calls.
By definition, a callback is called “back” from someplace else when something triggers it. Thus, there is no main(), as main() is Houdini itself!

Try invoking it from the textport, e.g. echo `myfunc()`

When Houdini sees a function called myfunc, it will try to look it up in the symbol table of functions, and if your function correctly identifies itself to the expression function table manager, it will get called. Houdini handles it getting called from the command line, a channel reference, an HDA, etc. You just have to worry about what to return.

– Antoine
Antoine Durr
Floq FX
antoine@floqfx.com
_________________
User Avatar
Member
9 posts
Joined: July 2008
Offline
Antoine Durr
By definition, a callback is called “back” from someplace else when something triggers it. Thus, there is no main(), as main() is Houdini itself!

Try invoking it from the textport, e.g. echo `myfunc()`

When Houdini sees a function called myfunc, it will try to look it up in the symbol table of functions, and if your function correctly identifies itself to the expression function table manager, it will get called. Houdini handles it getting called from the command line, a channel reference, an HDA, etc. You just have to worry about what to return.

– Antoine

Sorry, but I didn't catch.. What happens, when I use my expression function in Houdini? Which function exactly starts to work?

You've mentioned myfunc(). But how can I add it to the “symbol table of functions” and how can I make it to identify itself correctly to the table manager?

And one more question. Is it reasonable to call the function creating my structure from CMDextendLibrary() (it adds callback function somewhere for identification) or it will be called every time, when I start Houdini? I need to create my structure only once and use it in the callback function each time.

Thank you.

May be these questions are very obvious for you, but I have no experience with writing any libraries and working with HDK too.
Best regards
User Avatar
Member
321 posts
Joined: July 2005
Offline
Sikorskiy
Sorry, but I didn't catch.. What happens, when I use my expression function in Houdini? Which function exactly starts to work?
I don't have the code in front of me, but it's in the demo HDK examples.
Sikorskiy
You've mentioned myfunc(). But how can I add it to the “symbol table of functions” and how can I make it to identify itself correctly to the table manager?
I think that's the CDMextendLibrary() you mention below.
Sikorskiy
And one more question. Is it reasonable to call the function creating my structure from CMDextendLibrary() (it adds callback function somewhere for identification) or it will be called every time, when I start Houdini? I need to create my structure only once and use it in the callback function each time.

Thank you.

May be these questions are very obvious for you, but I have no experience with writing any libraries and working with HDK too.
What you'd have to do is when your function is called, check to see if this structure exists externally. If not, create it; if it does, use it. However, this can be very inefficient, as your function could be inside an inner loop getting called 100's of times.

You might want to rethink the problem a bit, and have some OTL or python function create the external structure. The Houdini paradigm is that any external file that you create should be recreateable at will, e.g. a geometry file, or an image file, a voxel file, etc. Maybe create a ROP that writes out the data first, so that the user can restart from the beginning.

I'm a bit surprised that you're delving into the HDK at this early stage. There have been ever fewer reasons to go that deep, especially with the introduction of Python into H9.
Antoine Durr
Floq FX
antoine@floqfx.com
_________________
User Avatar
Member
9 posts
Joined: July 2008
Offline
Antoine Durr
What you'd have to do is when your function is called, check to see if this structure exists externally. If not, create it; if it does, use it. However, this can be very inefficient, as your function could be inside an inner loop getting called 100's of times.

You might want to rethink the problem a bit, and have some OTL or python function create the external structure. The Houdini paradigm is that any external file that you create should be recreateable at will, e.g. a geometry file, or an image file, a voxel file, etc. Maybe create a ROP that writes out the data first, so that the user can restart from the beginning.

I'm a bit surprised that you're delving into the HDK at this early stage. There have been ever fewer reasons to go that deep, especially with the introduction of Python into H9.

Thank you so much. You're very kind.

I succeeded with this task. CMDextendLibrary() acts as main(). This function is called when Houdini starts and there I create my structure. And in my callback function I'm just using it. A disadvantage is that the structure is created each time, when Houdini starts (and if I don't want to use my expression function in the project). And it's inefficient. But it works


And about Python. I'm not familiar with this language. I know C++ and I have some experience. But I don't know Python at all. So, I decided, that C++ way would be easier.


Thank you one more time for your advice.
Best regards
User Avatar
Member
8065 posts
Joined: July 2005
Offline
Sikorskiy
A disadvantage is that the structure is created each time, when Houdini starts (and if I don't want to use my expression function in the project). And it's inefficient. But it works

You could use a static object that only gets allocated upon the first function call.

eg.
MyClass *getMyObject()
{
static MyClass *my_object = NULL;
if (!my_object) my_object = new MyClass;
return my_object;
}


Or, if you want to guard against concurrency as well take a look at UT/UT_DoubleLock.h.
User Avatar
Member
9 posts
Joined: July 2008
Offline
edward
You could use a static object that only gets allocated upon the first function call.

eg.
MyClass *getMyObject()
{
static MyClass *my_object = NULL;
if (!my_object) my_object = new MyClass;
return my_object;
}


Or, if you want to guard against concurrency as well take a look at UT/UT_DoubleLock.h.

Yeah. Thank you. It's a good idea.

That's ok. I've used not static, but global variable.
Best regards
  • Quick Links