Jessie gave some really good example of how to simply using the pickle module.
But it seemed like you took only the call to the pickle module and ditched the
rest ;-)
There is absolutely no reason to use more than one file here. The point of
using pickle is that you can store entire object structures, and not multiple
lists in different files that must be joined back up again with nested for
loops.
Trying to change as little as possible from your last example...
#########
import pickle
### Exporting
values = []
sel=cmds.ls(sl=1,mat=1)
for materials in sel:
attr = materials + '.diffuseColorAmount'
diffColorAmmount = cmds.getAttr(attr)
data = (attr, diffColorAmmount)
values.append(data)
with open( "c:/MayaStore/MaterialsValue.p", "wb" ) as f:
pickle.dump(values, f)
### Importing
with open( "c:/MayaStore/MaterialsValue.p", "rb" ) as f:
valList = pickle.load(f)
for data in valList:
attr, val
cmds.setAttr(attr, val)
#########
In this example what we do is store the attribute and the value together as a
tuple. This all goes into one file as a list of tuples. Then we can read back
that list, and pull out the tuples already matched up and call setAttr. This
immediately makes it more flexible because you can store any attribute name and
value and call setAttr with those objects.
You can get more flexible than this by using structures like a dict to maintain
more information in each item.
On May 24, 2013, at 8:06 AM, Daz wrote:
> Heya
>
> Aaaa the pickle module. I remember it now ! There is also cPickle if I'm not
> wrong which is a lot faster but then again... I should be fine for my basic
> stuff..
>
> In any case I got it saving and loading which is great. But I naturally hit a
> wall with re-assigning the data back to objects (materials in my case).
>
> Can any1 have a look at it and let me know where I go wrong?
>
> I think its because I try to iterate through list with another list so that
> my changes that I load end up being the last iterated value rather than what
> it should be, is there a way to somehow sync it ? Atm I only got 1 value but
> what will I do if there is RGB or a texture plug in to it, oh noez I'm
> screwed :D
>
> Anyway thanks for pickle hint! :)
>
> import pickle
>
> ### Exporting
> values = []
>
> sel=cmds.ls(sl=1,mat=1)
> for materials in sel:
> diffColorAmmount= cmds.getAttr(materials+'.diffuseColorAmount')
> values.append(diffColorAmmount)
>
> clearfileValue = open("c:/MayaStore/MaterialValues.p", 'w')
> clearfileSel = open("c:/MayaStore/MaterialSel.p", 'w')
> clearfileValue .close()
> clearfileSel.close()
> pickle.dump(values, open( "c:/MayaStore/MaterialsValue.p", "wb" ) )
> pickle.dump(sel, open( "c:/MayaStore/MaterialsSel.p", "wb" ) )
>
>
> ### Importing
>
> valList = pickle.load( open( "c:/MayaStore/MaterialsValue.p", "rb" ) )
> selList = pickle.load( open( "c:/MayaStore/MaterialsSel.p", "rb" ) )
>
> for objects in selList:
> for NewVal in valList:
> cmds.setAttr(objects+ '.diffuseColorAmount', NewVal)
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.