Skip to content

Instantly share code, notes, and snippets.

@rBrenick
Last active October 27, 2021 01:04
Show Gist options
  • Save rBrenick/9cead4aaf17f0b0910a4ab4fe8ec60be to your computer and use it in GitHub Desktop.
Save rBrenick/9cead4aaf17f0b0910a4ab4fe8ec60be to your computer and use it in GitHub Desktop.
Manual python parsing of an .atom file and extracting attribute information
from maya import cmds
class ReturnCode:
anim_layer_found = "anim_layer_found"
def preview_frame_from_atom(atom_path, preview_first_frame=True):
"""
Manual parse through the .atom file and figure out what a frame looks like
preview_first_frame: True for the first frame, False for the last frame
###################################
# Example .atom file content
#
# keys legend:
# index 0: frame
# index 1: value
dagNode {
anim translate.translateX translateX 0;
animData {
keys {
12 0 auto auto 1 0 0;
}
}
###################################
"""
anim_layers_found = False
with open(atom_path, "r") as fp:
next_block_contains_key_data = False
current_ctrl = ""
current_attr = ""
pose_data = {}
previous_line = ""
for line in fp.readlines():
if line.startswith("animLayers {"):
anim_layers_found = True
break
if next_block_contains_key_data:
# if True, grab the first key found in key block
if preview_first_frame:
key_value = line.lstrip().split()[1]
pose_data[current_ctrl][current_attr] = float(key_value)
next_block_contains_key_data = False
# otherwise, keep going until we reach the end, then grab the last key data from block
elif "}" in line:
key_value = previous_line.lstrip().split()[1]
pose_data[current_ctrl][current_attr] = float(key_value)
next_block_contains_key_data = False
if "anim " in line:
current_attr = line.lstrip().split()[2]
if "dagNode {" in previous_line:
current_ctrl = line.lstrip().split()[0]
pose_data[current_ctrl] = {}
if "keys {" in line:
next_block_contains_key_data = True
previous_line = line
if anim_layers_found:
return ReturnCode.anim_layer_found
for ctrl, attr_data in pose_data.items():
for attr_name, attr_val in attr_data.items():
full_attr = "{}.{}".format(ctrl, attr_name)
if not cmds.objExists(full_attr):
cmds.warning("Attribute not found: {}".format(full_attr))
continue
cmds.setAttr(full_attr, attr_val)
##################################################
# Example Window hooked up to above function
import pymel.core as pm
class ExamplePreviewAtomWindow(object):
def __init__(self):
# Make a new window
win_title = "Example Preview Atom Window"
win_title_safe = win_title.replace(" ", "_")
if pm.window(win_title_safe, q=True, exists=True):
pm.deleteUI(win_title_safe)
window = pm.window(win_title_safe, title=win_title, toolbox=True, backgroundColor=(0.200, 0.300, 0.30))
window.show()
with pm.verticalLayout() as main_layout:
self.root_folder_grp = pm.folderButtonGrp(changeCommand=self.update_file_list)
self.preview_first_frame = pm.checkBox("Preview First Frame", value=True)
self.atom_list = pm.textScrollList(selectCommand=self.run_preview_on_selected_atom)
main_layout.redistribute(1, 1, 6)
def run_preview_on_selected_atom(self):
selected_atom_name = self.atom_list.getSelectItem()[0]
atom_path = os.path.join(self.root_folder_grp.getText(), selected_atom_name)
preview_frame_from_atom(atom_path, self.preview_first_frame.getValue())
def update_file_list(self, *args):
self.atom_list.removeAll()
for file_name in os.listdir(self.root_folder_grp.getText()):
if ".atom" not in file_name.lower():
continue
self.atom_list.append(file_name)
if __name__ == "__main__":
win = ExamplePreviewAtomWindow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment