简体   繁体   中英

from a pickle file, restoring data in python

I'm having some problems with restoring the pose from a dictionary in Python. I just created a dictionary that has the data from an object and I saved this dictionary in a pickle file.

Now I want to grab all the data from this pickle file and restore it to another object. Can you advise me what is the best way?

This is my save_pose function:

def save_pose () :

     output = open('pose_dictionary.pkl', 'wb')
     pickle.dump(grab_pose(cmds.ls(selection=1), True), output)
     output.close()

     dict_file = open('pose_dictionary.pkl', 'rb')           
     dict_pkld = pickle.load(dict_file)
     dict_file.close()


     print dict_pkld

i just saved a dictionary in a pickle file, so when a print the dict_pkld it returns this

{u'left_pCube1_control': {u'translateX': {'value': 0.0, 'key': False}, u'translateY': {'value': 0.0, 'key': False}, u'translateZ': {'value': 0.0, 'key': False}, u'scaleX': {'value': 4.1730065104412066, 'key': {'outTangent': u'auto', 'inTangent': u'auto', 'inAngle': 0.0, 'outAngle': 0.0}}, u'scaleY': {'value': 4.1730065104412066, 'key': {'outTangent': u'auto', 'inTangent': u'auto', 'inAngle': 0.0, 'outAngle': 0.0}}, u'visibility': {'value': True, 'key': False}, u'rotateX': {'value': 0.0, 'key': False}, u'rotateY': {'value': 0.0, 'key': False}, u'rotateZ': {'value': 0.0, 'key': False}, u'scaleZ': {'value': 4.1730065104412066, 'key': {'outTangent': u'auto', 'inTangent': u'auto', 'inAngle': 0.0, 'outAngle': 0.0}}}

I want now restore these data from this file to an object.

i'm having some problems with restoring the pose from a dictionary in Python

What problems exactly... If I had to guess I'd say that there is something wrong with your grab_pose function.

When I run the following script I don't get any errors.

import pickle

output = open('pose_dictionary.pkl', 'wb')
pickle.dump({"test":1}, output)
output.close()

dict_file = open('pose_dictionary.pkl', 'rb')           
dict_pkld = pickle.load(dict_file)
dict_file.close()

print dict_pkld

Docs on pickle.load :

Read a string from the open file object file and interpret it as a pickle data stream, reconstructing and returning the original object hierarchy.

In your function you both save and restore the object. you should split these operations (and use with ):

filename = 'pose_dictionary.pkl'

def save_pose(data):
     with open(filename , 'wb') as output:
         pickle.dump(data, output)

def restore_pose():
     with open(filename , 'rb') as dict_file:
         return pickle.load(dict_file)

data = grab_pose(cmds.ls(selection=1), True) #assumption, based on your code
save_pose(data)
#... 
data = restore_pose()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM