Ordered Menu populated by Menu Script evaluates many times

   235   7   1
tas3d
Member
71 posts
Joined: Jan. 2012
Online
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 @ Weta Digital
jsmack
Member
4359 posts
Joined: Sept. 2011
Offline
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.
tas3d
Member
71 posts
Joined: Jan. 2012
Online
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 @ Weta Digital
jsmack
Member
4359 posts
Joined: Sept. 2011
Offline
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
573 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
tas3d
Member
71 posts
Joined: Jan. 2012
Online
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 @ Weta Digital
User Avatar
Member
5333 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
tas3d
Member
71 posts
Joined: Jan. 2012
Online
oh, thats so awesome!
Didnt know about those methods. This will reduce disk load even more.
Thanks
Michal Tas Maciejewski @ Weta Digital
  • Quick Links