Looping over contextOption values in Python

   2558   2   1
User Avatar
Member
250 posts
Joined: 10月 2014
オフライン
Given a list of possible values for a single context option, is it possible to loop over these using Python, running hou.setContextOption() with each value, have the scenegraph update accordingly with whatever mutation is triggered by the new context option value, do something (like retrieve some information from the SG in that particular mutation, then rinse and repeat with the next value?

I can loop over a list of values with hou.setContextOption(), and I can confirm that internally Houdini is indeed setting the value on each iteration (hou.contextOption returns the expected result), but the scenegraph mutations don't resolve on each iteration.

I'm guessing I'm doing something I'm not supposed to be doing. And clearly I'm not expecting an instantaneous result as the mutations need to cook each time. But I thought I would ask anyway on the chance there is some method I can call in my loop that forces a scenegraph update before proceeding to the next contextOption-driven mutation. Essentially I want to query some aspect of the scenegraph for different mutation states, and I figured the most direct (naive?) route was to loop over each mutation and run the query.

(So far my result is the same whether run from a Python LOP or a Python shelf tool: the context options change correctly but seem to have no effect when they do.)
Edited by Tim Crowson - 2020年12月13日 22:35:44
- Tim Crowson
Technical/CG Supervisor
User Avatar
スタッフ
579 posts
Joined: 6月 2020
オフライン
Hi Tim,

Can you elaborate a bit more on where you're getting stuck?

I'm attaching a simple .hip file where a switch is driven by a context option. Then in a Python Shell I run this:

for i in range(2):
  hou.setContextOption('switchval', i)
  print hou.node('/stage/switch1').stage().GetPseudoRoot().GetAllChildren()

And get the following output:

[Usd.Prim(</HoudiniLayerInfo>), Usd.Prim(</sphere1>)]
[Usd.Prim(</HoudiniLayerInfo>), Usd.Prim(</cube1>)]

It also works for me when dragging the script to the shelf, but I do observe it doesn't work as a Python LOP.

- Rob

Attachments:
context_switch.hip (81.5 KB)

User Avatar
Member
250 posts
Joined: 10月 2014
オフライン
Thanks, Rob! I got it working now. I was using the wrong method to query the stage. I was still using editableStage() rather than just stage().

(And I can also confirm this does not work from a Python LOP, but only from the Python shell or a shelf tool)
Edited by Tim Crowson - 2020年12月14日 13:22:27
- Tim Crowson
Technical/CG Supervisor
  • Quick Links