borisb2 Posted May 22, 2015 Share Posted May 22, 2015 (edited) Quick question: I'm running a lot of python snippets (creating specific nodes, ROPS etc.) through a custom menu in houdini. The external python-files are sourced in the MainMenuCommoin.xml via <label>Submit Caching Job (Non Sequential)</label> <scriptPath>Y:/_scripts/houdini/submitCachingJob.py</scriptPath> So far so good. Now I want to create a button on a node in houdini that also calls one of these external scripts. But here I'm a little stuck as I can't figure out how to actually source an external python-script via the buttons callback-script. In these files there are no functions to import, just plain commands to create/connect/set something in houdini. What would be an easy way to source an external python file? Thanks a lot. Edited May 22, 2015 by borisb2 Quote Link to comment Share on other sites More sharing options...
Stalkerx777 Posted May 22, 2015 Share Posted May 22, 2015 Having all your code in global namespace of a module without a functions, usually a bad practice. If you can edit this file, make a simple main() function in it. Also, you can try to use python's execfile build-in. Quote Link to comment Share on other sites More sharing options...
borisb2 Posted May 27, 2015 Author Share Posted May 27, 2015 Thanks. But even if I'd wrap all commands in a function, how would I source that external py-file in the buttons callback script? .. the file is not sitting in the users directoy but on the server. Guess I have to look at the execfile function? Quote Link to comment Share on other sites More sharing options...
dennis.albus Posted May 29, 2015 Share Posted May 29, 2015 You could try something like this in your callback script. import sys; sys.path.append("/path/to/your/python/module"); import your_module; your_module.main() As Alexey mentioned make sure your code is wrapped into functions otherwise it will only be run once when the module is imported. Cheers, Dennis Quote Link to comment Share on other sites More sharing options...
borisb2 Posted May 29, 2015 Author Share Posted May 29, 2015 hmm ... trying: import sys; sys.path.append("Y:/_scripts/houdini/setMantraOutput"); import setMantraOutput but I get "No module named setMantraOutput".. The file sits here: Y:/_scripts/houdini/setMantraOutput.py what am I doing wrong? Quote Link to comment Share on other sites More sharing options...
Stalkerx777 Posted May 29, 2015 Share Posted May 29, 2015 hmm ... trying: import sys; sys.path.append("Y:/_scripts/houdini/setMantraOutput"); import setMantraOutput but I get "No module named setMantraOutput".. The file sits here: Y:/_scripts/houdini/setMantraOutput.py what am I doing wrong? Read the basics of Python modules. You need to append a path to a folder, not to a file, where python can find your module by name. Quote Link to comment Share on other sites More sharing options...
borisb2 Posted May 29, 2015 Author Share Posted May 29, 2015 damn it .. caught unprepared .. (I'm in the middle of a production) works.. thanks.. sorry Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.