Ordered Menu populated by Menu Script evaluates many times

   4384   7   1
User Avatar
Member
120 posts
Joined: Jan. 2012
Offline
Hi,

I have a Null SOP, added Ordered Menu and used Menu Script to fill it up.

r = []
i = 0
for x in range(0,10):
    r.append(str(i))
    r.append(str('name_'+str(i)))
    i += 1
print 'test'
return r

All good, but it get evaluated 12 times!
Any clues why is that and how to get it populated once?

Thanks
Edited by tas3d - March 29, 2021 16:02:23
Michal Tas Maciejewski @ www.vfxtricks.com
User Avatar
Member
7741 posts
Joined: Sept. 2011
Online
tas3d
All good, but it get evaluated 12 times!
Any clues why is that and how to get it populated once?

Such is houdini. Use fast code in menus that's okay to be called hundreds of times in a row. Each menu event might evaluate the callback script dozens of times. For database/disk hits, use caching to prevent unnecessary actions.
User Avatar
Member
120 posts
Joined: Jan. 2012
Offline
jsmack
For database/disk hits, use caching to prevent unnecessary actions.

Disk queries are my reason for asking.
Could you elaborate idea of caching?
Michal Tas Maciejewski @ www.vfxtricks.com
User Avatar
Member
7741 posts
Joined: Sept. 2011
Online
tas3d
jsmack
For database/disk hits, use caching to prevent unnecessary actions.

Disk queries are my reason for asking.
Could you elaborate idea of caching?

There's probably a better way to do this, but in the module where the callback is defined, save the results of the query with some key that can be used to retrieve the results of the same query later, recording the time of the initial query. Then when the callback runs again it can use the cached result if it isn't too old. Otherwise, run the live query again. The cache can be short lived, like 30 seconds. This should prevent a menu callback from hitting the disk 10 times instead of once.
User Avatar
Member
900 posts
Joined: Feb. 2016
Offline
I was doing sort of the same trick and noticed huge improvements on a heavy menu script.
The issue was indeed that you don't know what iteration number is the current run of the menu script.
If there was a kwargs argument for that, it would be easier to load the cached menu after the first evaluation for the remaining iterations.
Edited by Andr - March 30, 2021 05:47:32
User Avatar
Member
120 posts
Joined: Jan. 2012
Offline
Thanks everyone for chiming in.

I ended up writing to tempfile results from first iteration, plus timestamp.
Next time its run, it will read this file and compare timestamp. If its close enough, will get the data from file.

Much faster now

Quite a lot of work just to workaround Menu Script. Oh oh.
Michal Tas Maciejewski @ www.vfxtricks.com
User Avatar
Member
8531 posts
Joined: July 2007
Offline
tas3d
I ended up writing to tempfile results from first iteration, plus timestamp.
maybe it'd be cleaner and faster to use this instead of file:
https://www.sidefx.com/docs/houdini/hom/hou/Node.html#setCachedUserData [www.sidefx.com]
https://www.sidefx.com/docs/houdini/hom/hou/Node.html#cachedUserData [www.sidefx.com]

this will save custom data into dictionary on your node
and it's not saved into the hip file, so literally for temp purposes like yours
Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
120 posts
Joined: Jan. 2012
Offline
oh, thats so awesome!
Didnt know about those methods. This will reduce disk load even more.
Thanks
Michal Tas Maciejewski @ www.vfxtricks.com
  • Quick Links