How to pack a python script and execute externally?

How to pack a python script and execute externally?

5rivamsi
Contributor Contributor
665 Views
5 Replies
Message 1 of 6

How to pack a python script and execute externally?

5rivamsi
Contributor
Contributor

How to pack a python script and execute externally so that the user may not have direct access to the code. I tried

"import folderName" and later "reload(folderName)" the code is executing correctly. but my code creates UI and check the state of some UI elements, and I'm getting errors when using the UI. (cannot find Checkbox,textFields, etc) . the Code is working fine when executed directly from the script editor or shelf.But not when using "reload(folderName)".

Please help.

0 Likes
666 Views
5 Replies
Replies (5)
Message 2 of 6

mcw0
Advisor
Advisor

Without seeing the code, it's difficult to say what the problem is.  Is your python file a collection of commands or do you have defined functions? If you have functions, just reloading might not be enough.  You might also need to run 

 

folderName.function()

 

You say you're able to work with the UI but get errors.  So running your code creates the UI.  It just can't find any of the widgets.  Are you hardcoding the names of the widgets or leaving it up to Maya?  It's always best to control the names when possible.  

0 Likes
Message 3 of 6

5rivamsi
Contributor
Contributor

I'm using defined functions. creation of UI is collection of commands (not defined function) the rest is. And I used naming for widget like. 

currState = mc.checkBox(val =1,en=1)

currStateVal = mc.checkBox(currState, v=1, q=1)

and in a defined function I used

if currStateVal:

do something.

error:checkbox128 not found.

0 Likes
Message 4 of 6

mcw0
Advisor
Advisor

What I meant by naming your widgets is this:

 

currState = mc.checkBox("myCHK", val =1,en=1)

 

Then you can test if your checkBox actually exists with:

 

mc.checkBox("myCHK", q=1, exists=1)

0 Likes
Message 5 of 6

5rivamsi
Contributor
Contributor

I made a UI and defined some functions for the buttons with python, saved it as init.py and when i Import its folder name the UI is built but the functions cannot be called. says. 'function name not defined'. but when I run it directly without using import it is working fine. and after running the code once, directly then the import is also working,Please help. I have to call it with import, without seeing whole code. 

0 Likes
Message 6 of 6

mcw0
Advisor
Advisor

I don't know which version of Maya you are running so that impacts which version of Python.  I've commented out 2 versions of a function.  Uncomment the one applicable to your case.  Save that out as "test.py".  And you can "import test" and "reload(test)".  But if you are in Python 3, you will need "from importlib import reload".

 

import maya.cmds as mc

''' Python 3
def Hello(*args):
print ("hi")
'''

''' Python 2
def Hello(*args):
print "hi"
'''

window = mc.window(width=200)
mc.columnLayout()
button = mc.button(label="Hello", command=Hello)
mc.showWindow(window)

0 Likes