import maya.cmds as cmds
import json
# Specify your directory..
file_export_dir = 'C:\Users\abc\Desktop\export_test.txt'
all_data = {}
attributes = ['translateX', 'translateY', 'translateZ', 'rotateX', 'rotateY', 'rotateZ']
def get_frame_range():
  # get the frame range in the time slider
  start_frame = int(cmds.playbackOptions(query=True, min=True))
  end_frame = int(cmds.playbackOptions(query=True, max=True))
  return(start_frame, end_frame)
def export_keyframe(sel):
  attr_data = {}
  (start_frame, end_frame) = get_frame_range()
  for time in range(start_frame , (end_frame + 1)):
    #>>> print "Frames iteration : ", time
    cmds.currentTime(time)
    # Get the value in each attribute
    attr_data['frame'] = str(time)
    for attribute in attributes:
      attr_value = cmds.getAttr( sel + '.' + attribute)
      # Assign the value into the dict
      attr_data[attribute] = str(attr_value)
    # Store the data into the main
    all_data[sel] = attr_data
  # Write the overall data into file
  with open(file_export_dir, 'w') as outfile:
    outfile.write(json.dumps(all_data, indent=4, sort_keys=True))
   Â
def main():
  sels = cmds.ls(sl=True, l=True)
  if not len(sels):
    cmds.warning('Nothing is selected!')
   Â
    # Create the key and assign an empty value
    all_data[str(sel)] = None
    export_keyframe(sel)
{
  "|locator2": {
    "frame": "5",
    "rotateX": "2.0",
    "rotateY": "2.0",
    "rotateZ": "2.0",
    "translateX": "2.5",
    "translateY": "2.5",
    "translateZ": "2.5"
  }
}def export_keyframe(sel):
attr_data = {}
...
def export_keyframe(sel):
...
for time in range(start_frame , (end_frame + 1)):
attr_data = {}
...
--
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/4d49de7f-054c-497a-91e7-0179402f5045%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hi Justin, thanks for getting back..Â
{
  "|locator1": {
    {
      "frame": "1",
      "rotateX": "1.0",
      "rotateY": "1.0",
      "rotateZ": "1.0",
      "translateX": "1.5",
      "translateY": "1.5",
      "translateZ": "1.5"
    },
    {
      "frame": "2",
      "rotateX": "1.01",
      "rotateY": "1.01",
      "rotateZ": "1.01",
      "translateX": "1.51",
      "translateY": "1.51",
      "translateZ": "1.51"
    }
  }
}--
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/3b06a3cb-7fc2-4cf8-8d32-ee6e5e940a73%40googlegroups.com.
 {
  "|locator1": [
    {
      "frame": "1",Â
      "rotateX": "1.0",Â
      "rotateY": "1.0",Â
      "rotateZ": "1.0",Â
      "translateX": "1.5",Â
      "translateY": "1.5",Â
      "translateZ": "1.5"
    },
    {
      "frame": "2",Â
      "rotateX": "1.01",Â
      "rotateY": "1.01",Â
      "rotateZ": "1.01",Â
      "translateX": "1.51",Â
      "translateY": "1.51",Â
      "translateZ": "1.51"
    }
  ]
}
{Â
  "|locator1": {    "1": {      "frame": "1",      "rotateX": "1.0",      "rotateY": "1.0",      "rotateZ": "1.0",      "translateX": "1.5",      "translateY": "1.5",      "translateZ": "1.5"    },    "2": {      "frame": "2",      "rotateX": "1.01",      "rotateY": "1.01",      "rotateZ": "1.01",      "translateX": "1.51",      "translateY": "1.51",      "translateZ": "1.51"    }  }}
ÂThis works for me too.. It is just that it never occurs to me of using '1', 2' etc as the key for I am thinking it in terms of using the object's name to store the data..ÂNeed more practice with dictionary Â
def export_keyframe(sel):
...
all_data[sel] = {} # init empty time results
for time in range(start_frame , (end_frame + 1)):
attr_data = {}
cmds.currentTime(time)
# Get the value in each attribute
attr_data['frame'] = str(time)
for attribute in attributes:
attr_value = cmds.getAttr( sel + '.' + attribute)
# Assign the value into the dict
attr_data[attribute] = str(attr_value)
# Store the data into the main
all_data[sel][time] = attr_data # assign using time key
...
--
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/1cb4c70e-74ab-4af2-a149-129c7f0f77f9%40googlegroups.com.
Thanks, it works!I have a few questions..1. `all_data[sel] = {}`, why can't I wrote it as `all_data[sel] = None`? I tried and I got an error that states ''NoneType' object does not support item assignment'I asked this because since I have no values to append in the first place, wouldn't using None be more practical? Or, do correct me if I am wrong, since we are dealing with dictionaries, initializing an empty dict is then the correct way?
2. Do I still need to have `attr_data = {}` still needs to be placed under the `for time ... end_frame +1))` line? Would that not cause it to be loop continuously? P.S, Again, I tried placing it outside of the `for` statement as initially seen in my post, and it works too..
def test(broken=False): all_data = {} attr_data = {}  for i in xrange(5):  if not broken: attr_data = {}  for j, attr in enumerate(['a','b','c']): attr_data[attr] = i + j  all_data[i] = attr_data  print all_data
3. This is slightly unrelated but what can I do to refine writing logic process? There are times in which I seem to get it (but it is in a big chunk where it can be further refine), and times like this, where I am unable to..
--
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/ec6429bb-8cad-4509-8485-83974dfa3e87%40googlegroups.com.