evilbear Posted December 30, 2012 Share Posted December 30, 2012 Hi, All: I wrote a python script can import camera data from a text file, but I can't figure out how to run my script in houdini, I was using python shell try to source my script, I just can not get it to work. Could somebody teach me how to do it? Here is a part of my script, import os import string import hou def importMayaCamera(fileName): # check if file exists if not os.path.isfile(fileName): print 'file not found: %s' % fileName return # r means to read fileHandle = open(fileName,'r') data = fileHandle.readlines() header = data[0] cam = hou.node("/obj").createNode("cam", "camera1") Thank you Quote Link to comment Share on other sites More sharing options...
Erik_JE Posted December 30, 2012 Share Posted December 30, 2012 http://www.sidefx.co...2.1/hom/session Do like that. I fail to see the point of that script tho as it don't actually use any data from the file it reads? import osimport stringimport houdef importMayaCamera(fileName):# check if file existsif not os.path.isfile(fileName): print 'file not found: %s' % fileName return# r means to readfileHandle = open(fileName,'r')data = fileHandle.readlines()header = data[0]cam = hou.node("/obj").createNode("cam", "camera1")[/CODE]CODE tags are great for readability. Quote Link to comment Share on other sites More sharing options...
anim Posted December 30, 2012 Share Posted December 30, 2012 you can use your code inside of shelf button code so it will execute from shelf, of course you will need to call importMayaCamera() function there after defining as well or you can copy the python file into scanned scripts directory, then you can import it into houdini as module using import filename (without extension) it's advisable to do this for shelf tools as well since it is easier to manage one source module file than lots of shelf tools but you will be to import it into python shell, python session simply anywhere in houdini to call your functions from that file or don't copy it to scanned script directory and use load_source() function from imp module to create module from full path to your file not very flexible, but if you always know the path, it is an option or you can use your code in session module of current hip file as Erik suggested, but that will narrow it's usage only from within that file Quote Link to comment Share on other sites More sharing options...
evilbear Posted December 30, 2012 Author Share Posted December 30, 2012 http://www.sidefx.co...2.1/hom/session Do like that. I fail to see the point of that script tho as it don't actually use any data from the file it reads? import osimport stringimport houdef importMayaCamera(fileName):# check if file existsif not os.path.isfile(fileName): print 'file not found: %s' % fileName return# r means to readfileHandle = open(fileName,'r')data = fileHandle.readlines()header = data[0]cam = hou.node("/obj").createNode("cam", "camera1")[/CODE]CODE tags are great for readability.Thank you Erik, I will try,it's only part of my script, I didn't paste the whole script. Quote Link to comment Share on other sites More sharing options...
evilbear Posted December 30, 2012 Author Share Posted December 30, 2012 you can use your code inside of shelf button code so it will execute from shelf, of course you will need to call importMayaCamera() function there after defining as well or you can copy the python file into scanned scripts directory, then you can import it into houdini as module using import filename (without extension) it's advisable to do this for shelf tools as well since it is easier to manage one source module file than lots of shelf tools but you will be to import it into python shell, python session simply anywhere in houdini to call your functions from that file or don't copy it to scanned script directory and use load_source() function from imp module to create module from full path to your file not very flexible, but if you always know the path, it is an option or you can use your code in session module of current hip file as Erik suggested, but that will narrow it's usage only from within that file Thank you Tomas, I did put my file in script scan dictionary, and run the import module, and it's failed and said can't find module hou. could you explain more cleary on do it in shelf code? Quote Link to comment Share on other sites More sharing options...
anim Posted December 30, 2012 Share Posted December 30, 2012 (edited) since it is python script, you need to put it to scripts/python directory, like $HOME/houdini12.1/scripts/python other than that you probably did it correctly so if your file is named cam.py then shelf tool will be import camfileName = "C:\cam.txt"cam.importMayaCamera(fileName)[/CODE]of course you wouldn't have hardcoded fileName, but it's just proto codeEDIT:oh, and while testing the code it is useful to add reload(<modulename>) after import, so it will update if you've changed the source file even without restarting houdiniso it would be like[CODE]import camreload(cam)fileName = "C:\cam.txt"cam.importMayaCamera(fileName)[/CODE] Edited December 30, 2012 by anim 1 Quote Link to comment Share on other sites More sharing options...
evilbear Posted December 31, 2012 Author Share Posted December 31, 2012 It's working perfectly, thank you so much since it is python script, you need to put it to scripts/python directory, like $HOME/houdini12.1/scripts/python other than that you probably did it correctly so if your file is named cam.py then shelf tool will be import camfileName = "C:\cam.txt"cam.importMayaCamera(fileName)[/CODE]of course you wouldn't have hardcoded fileName, but it's just proto codeEDIT:oh, and while testing the code it is useful to add reload(<modulename>) after import, so it will update if you've changed the source file even without restarting houdiniso it would be like[CODE]import camreload(cam)fileName = "C:\cam.txt"cam.importMayaCamera(fileName)[/CODE] 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.