"optionVar" of python ???

917 views
Skip to first unread message

Glom De

unread,
Feb 3, 2014, 12:21:44 PM2/3/14
to python_in...@googlegroups.com
hi guys
as you know ,maya command "optionVar"  can store variables in disk, 
are there any functions or Module of python can do same thing like Maya cmd "optionVar"

googled , but did not find any clue。。。。。
anyone can give me some info about that, thanks a lot!!!

Chad Dombrova

unread,
Feb 3, 2014, 12:29:41 PM2/3/14
to python_in...@googlegroups.com, python_in...@googlegroups.com
Check out the shelve module. It is part if the standard library. 

Sent from Mailbox for iPhone


--
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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e2adb779-0a00-4426-9e28-283a387856c9%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

aevar.gu...@gmail.com

unread,
Feb 3, 2014, 1:07:20 PM2/3/14
to python_in...@googlegroups.com
  Here you go

  And it´s as easy as it sounds, just store your options with maya.cmds.optionVar and it gets saved into your home account, it accepts a standard query=True flag to get default options and so forth.  Presumably you are coming up empty on a google search as there isn’t really much more to it than is on that page.

  However, If you are looking for something that will stick into the files check out maya.cmds.fileInfo instead, whatever you save through that will get embedded into the .mb or .ma file you save out rather than only save the preference on your account.

  Hope this helps, have some snippets and notes around in a pile somewhere but you should hopefully get all you need from the link above or typing in:

import maya.cmds
help(maya.cmds.optionVar)

Count Zer0

unread,
Feb 3, 2014, 11:43:09 PM2/3/14
to python_in...@googlegroups.com
PyMel gives you easy access to them too:


And you should be using PyMel anyway if you're moving past MEL anyway.

-jason

Chad Dombrova

unread,
Feb 4, 2014, 12:48:13 AM2/4/14
to python_in...@googlegroups.com, python_in...@googlegroups.com
I *think* that Glom was looking for something similar to optionVars for use outside of Maya, but I could be wrong. 

Sent from Mailbox for iPhone


--
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_m...@googlegroups.com.

Marcus Ottosson

unread,
Feb 4, 2014, 2:46:06 AM2/4/14
to python_in...@googlegroups.com
I take it you are looking to store data from code onto disk for retrieval later with libraries external to Maya, or even outside of Maya.

This is usually a two-step process:
1. Serialization, which means to make a set of data - in this case, internal to Python - writeable to disk and retrievable later.
2. Writing to disk.

Depending on your needs, you may not need serialization and can simply write data structures directly to disk; e.g. if you are looking to store text.

>> with open(file, 'w') as f:
>>     f.write(my_text)
>>
>> with open(file, 'r') as f:
>>     retrieved_text = f.read()

Serialization comes in many flavours and are relevant if you're looking to store more complex types, such as list, dict or arbitrary data structures of your own or Python's making.

The shelve module as suggested above is appropriate if the data you're storing involves any objects other than the plain-old-data types, such as string, int, bool etc.

If you're needs are simpler, an alternative to shelve is json, also included in the standard library, but capable of storing only plain-old-data.

Both shelve and json come with their own writing mechanism, but in the case of json, the resulting serialized data is of type string and can be passed around and modified in code before being written out by other means.

>> serialized_data = json.dumps(my_data)
>> with open(file, 'w') as f:
>>    f.write(serialized_data)

Which brings me to the next important topic of which json and shelve are good examples.

One results in human-readable data and the other does not. Generally, human-readable data lacks the performance of binary equivalents but make up for it in that they can be inspected and edited by humans. It all depends on your needs, really.

See here for a summary of what Python has to offer in this aspect




For more options, visit https://groups.google.com/groups/opt_out.



--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Feb 4, 2014, 4:07:55 AM2/4/14
to python_in...@googlegroups.com
A few more bits about shelve vs json. shelve is actually not a serialization format specifically, but rather a key-value store that serializes its values using pickle. It stores like a database and has the added benefit of letting you modify in-memory, piecemeal over time, until it gets flushed to disk. Basically it acts just like optionVar, by being accessible like a dictionary, as opposed to needing to dump your entire structure to and from disk with each change. 

And also, json has a "dump" variant:

with open(fname, 'w') as f:
    # writes directly to your file
    json.dump(data, f)

If you use Qt at all, there is also a really nice class called QSettings which acts similar to the shelve module, but also has the benefit of knowing the cross-platform location to store user preferences, and is completely safe for concurrent reads/writes between threads/processes. 
  





Marcus Ottosson

unread,
Feb 4, 2014, 4:29:41 AM2/4/14
to python_in...@googlegroups.com
Ah, good point. I assumed shelve was more or less identical to pickle, but clearly haven't used it enough to tell the difference.



For more options, visit https://groups.google.com/groups/opt_out.



--
Marcus Ottosson
konstr...@gmail.com

Glom De

unread,
Feb 4, 2014, 4:32:21 AM2/4/14
to python_in...@googlegroups.com
thanks nice guys!!!
Json works very well

thanks!  ;-)
Reply all
Reply to author
Forward
0 new messages