from odbAccess import *
from abaqusConstants import *
import os
os.chdir('.')

odb = openOdb('Plastic-Stress.odb')
fw = open('Plastic-Stress.dtp','w')
print("NUM STEP", len(list(odb.steps.keys())), file=fw)

for step_name in list(odb.steps.keys()):
    step = odb.steps[step_name]
    step_num = step.number
    print("LOADSTEP", step_num, file=fw)
    num_frames = len(odb.steps[step_name].frames)
    for i in range(num_frames): 
        print("SUBSTEP", i, file=fw)
        frame = odb.steps[step_name].frames[i]
        for fk in list(frame.fieldOutputs.keys()):
            if len(fk) >= 1 and fk[0:1] == 'S' :
                stress = frame.fieldOutputs[fk]
                field = stress.getSubset(position=NODAL)
                fieldValues = field.values
                if len(fieldValues) == 0:
                    print("STRESS ELEMENT NODAL", file=fw)
                    field = stress.getSubset(position=ELEMENT_NODAL)
                    fieldValues = field.values
                    for foS in fieldValues:
                        strs = foS.data
                        print(foS.elementLabel,"  ",foS.nodeLabel,"  ",float(strs[0]),"  ",float(strs[1]),"  ",float(strs[2]),"  ",float(strs[3]),"  ",float(strs[4]),"  ",float(strs[5]), file=fw)
                else:
                    print("STRESS", file=fw)
                    for foS in fieldValues:
                        strs = foS.data
                        print(foS.nodeLabel,"  ",float(strs[0]),"  ",float(strs[1]),"  ",float(strs[2]),"  ",float(strs[3]),"  ",float(strs[4]),"  ",float(strs[5]), file=fw)
fw.close()
