python viewport through specified camera ... error

   4973   5   1
User Avatar
Member
201 posts
Joined: July 2005
Offline
H16.0.736 Windows7

I have a 123.py file that imports another module. In that module, I have some code that is suppose to set the viewport to look out a specific camera. The code looks something like this:

import toolutils
defCam = hou.node(“/obj/defCam”)
scene_view = toolutils.sceneViewer()
viewport = scene_view.curViewport()
viewport.setCamera(defCam)

* assume defCam has been already been setup

If I run this snippet from Python shell inside Houdini there is no issue. However, when it runs during initial launch of Houdini (double click on desktop icon), I get the following error:

Error running Python code:
Traceback (most recent call last):
File “C:<hidden_path>/Documents/houdini16.0/scripts/123.py”, line 20, in <module>
import startup
File “<hidden_path>/houdini/scripts/python\startup.py”, line 27, in <module>
scene_view = toolutils.sceneViewer()
File “CPROGRA~1/SIDEEF~1/HOUDIN~1.736/houdini/python2.7libs\toolutils.py”, line 73, in sceneViewer
primarypane = hou.ui.paneTabOfType(hou.paneTabType.SceneViewer, index)
File “CPROGRA~1/SIDEEF~1/HOUDIN~1.736/houdini/python2.7libs\hou.py”, line 46554, in paneTabOfType
return _hou.ui_paneTabOfType(*args)
NotAvailable: Not available in this context.
No desktops are available


Any idea why it runs fine when inside Houdini but fails during startup? Is there a better way to achieve this? TIA
Cheers,
Rob
Digital Supervisor | Stargate Studios Toronto
User Avatar
Member
2625 posts
Joined: June 2008
Offline
My guess is that 123.py runs before the interface is even built. So you can't reference functionality that does not exist yet.

123.py is out of context compared to the user interface.

Maybe try placing your code inside 456.py instead?
Edited by Enivob - Sept. 28, 2017 16:58:49
Using Houdini Indie 20.0
Windows 11 64GB Ryzen 16 core.
nVidia 3050RTX 8BG RAM.
User Avatar
Member
8037 posts
Joined: Sept. 2011
Offline
The startup script in the pipeline tools at the studio where I work spawns a thread that waits for houdini's ui to be available, then executes code that requires the ui.
User Avatar
Member
2625 posts
Joined: June 2008
Offline
Hmm..that actually works?
I assume then a thread is spawned inside 123.py and then 123.py exits so Houdini can continue on.
Then in the thread I imagine a loop hangs out with a TRY/EXCEPT attempting to access a portion of the UI interface. When the TRY works, the loop exits and then the rest of the code can run with a valid context established.
Using Houdini Indie 20.0
Windows 11 64GB Ryzen 16 core.
nVidia 3050RTX 8BG RAM.
User Avatar
Member
201 posts
Joined: July 2005
Offline
RESOLVED …

Moved code into another py file and imported that from 456.py (didn't need to use 123.py after all). Also found I needed to add hou.uitriggerUpdate() call. So the code looks like this:

def_cam = hou.node(/obj/defCam)
hou.ui.triggerUpdate()
hou.ui.waitUntil(lambda: len(hou.node("/obj").children()) > 0)
scene_view = toolutils.sceneViewer()
viewport = scene_view.curViewport()
viewport.setCamera(def_cam)
Cheers,
Rob
Digital Supervisor | Stargate Studios Toronto
User Avatar
Member
1 posts
Joined: Jan. 2016
Offline
You can run your functions in 123.py if you use execute deferred.

def foo():
    print 'hi'

import hdefereval
hdefereval.execute_deferred(foo)
  • Quick Links