Discussion:
[Maya-Python] OptionVar is not saving/ loading correctly
likage
2018-08-14 19:13:15 UTC
Permalink
I am using `optionVar` to store inputs made within the maya ui commands.
And these storing will only be effected if the tool gui is closed (the
cross 'x' button within the window) and I am having issues with that...

I am having some issues with the loading and saving when using `optionVar`.

In my current code, I have set it to query the boolean value, where it read
it as False, when the UI is loaded, it checked the check box which is a
True value that is wrong...
Additionally, for the saving of inputs upon window closure, it does not
seems to be able to 'save' the data at all. And when relaunching the code,
I will get `# RuntimeError: Object 'chk_box' not found. # `

def testWin():
default_options = cmds.optionVar(
stringValue=(
("checkbox_state", False),
)
)

# This will returns me False
print cmds.optionVar(q="checkbox_state")


w = cmds.window()
col = cmds.columnLayout()
chk_state = bool(cmds.optionVar(q="checkbox_state"))
# But it will checked the checkbox though it should be False initially
chkBox = cmds.checkBox("chk_box", label="checkbox state Testing",
value=bool(chk_state))

btn = cmds.button('print', c=print_state)

return w

def print_state(*args):
state = cmds.checkBox("chk_box", query=True, value=True) # Returns me
the current state - True/ False
print state

def win_closure(*_):
#cmds.checkBox("chk_box", query=True, value=True) # Returns me nothing
cmds.optionVar(stringValue=("checkbox_state", cmds.checkBox("chk_box",
q=True, value=True)))
print "window " + w + " was deleted"

win = testWin()
cmds.scriptJob(uiDeleted = (win, win_closure), runOnce=True)
cmds.showWindow(win)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/4fc79cee-c18b-4c76-bb87-3dee2ee4f1a5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
2018-08-14 19:38:44 UTC
Permalink
In python, converting a string to a bool is going to return True if the
string is not empty. It doesn't do what you think it does, by interpreting
"True" to True.

Use an int instead:
http://help.autodesk.com/cloudhelp/2016/ENU/Maya-Tech-Docs/CommandsPython/optionVar.html#flagintValue

That will convert from True/False to 1/0 and back again.

Justin
Post by likage
I am using `optionVar` to store inputs made within the maya ui commands.
And these storing will only be effected if the tool gui is closed (the
cross 'x' button within the window) and I am having issues with that...
I am having some issues with the loading and saving when using `optionVar`.
In my current code, I have set it to query the boolean value, where it
read it as False, when the UI is loaded, it checked the check box which is
a True value that is wrong...
Additionally, for the saving of inputs upon window closure, it does not
seems to be able to 'save' the data at all. And when relaunching the code,
I will get `# RuntimeError: Object 'chk_box' not found. # `
default_options = cmds.optionVar(
stringValue=(
("checkbox_state", False),
)
)
# This will returns me False
print cmds.optionVar(q="checkbox_state")
w = cmds.window()
col = cmds.columnLayout()
chk_state = bool(cmds.optionVar(q="checkbox_state"))
# But it will checked the checkbox though it should be False initially
chkBox = cmds.checkBox("chk_box", label="checkbox state Testing",
value=bool(chk_state))
btn = cmds.button('print', c=print_state)
return w
state = cmds.checkBox("chk_box", query=True, value=True) # Returns me
the current state - True/ False
print state
#cmds.checkBox("chk_box", query=True, value=True) # Returns me nothing
cmds.optionVar(stringValue=("checkbox_state", cmds.checkBox("chk_box",
q=True, value=True)))
print "window " + w + " was deleted"
win = testWin()
cmds.scriptJob(uiDeleted = (win, win_closure), runOnce=True)
cmds.showWindow(win)
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/4fc79cee-c18b-4c76-bb87-3dee2ee4f1a5%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/4fc79cee-c18b-4c76-bb87-3dee2ee4f1a5%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0TZLs87em7UgSkt8WG_mD5FNZTvZbbDE27fZ1M92jzEg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
likage
2018-08-14 21:10:26 UTC
Permalink
Hi Justin, many thanks for the reply, that seems to have do the trick.

Wondering if you could also share any insights into `# RuntimeError: Object
'chk_box' not found. #` whenever I tried to relaunch/ rerun the code?
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e0ec520e-6b6c-4dae-9946-dfa83cd80b96%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
2018-08-15 04:21:48 UTC
Permalink
Post by likage
Hi Justin, many thanks for the reply, that seems to have do the trick.
Object 'chk_box' not found. #` whenever I tried to relaunch/ rerun the
code?
It is likely due to the very fragile approach of using literal names like
"chk_box" and "checkbox_state" between free functions. The problem is that
each time a window is created, those same ui elements could end up being
chk_box2, chk_box3, ... if they previously exist. Also for the optionVar
name, it is possible to clash with something else using such a simple name
as 'checkbox_state'. So it is best to namespace it with something more
unique related to your app name.

My suggestion is that you refactor this all into a single class that can
save the name of the check box that was returned when you create it. Then
you can reuse this name throughout your code instead of assuming it is
always called the same thing.
Post by likage
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/e0ec520e-6b6c-4dae-9946-dfa83cd80b96%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/e0ec520e-6b6c-4dae-9946-dfa83cd80b96%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2iW%2BAtBFMPdYfu8mbe%3D5CQDqCMXX49dgPY0Hu9Z2Pqug%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...