I found this post with a script accomplishing what I'm trying.
The only problem is that this is node based while I've tried, and made stresses work, in an element based method. But it should be possible to achieve my goal (objective function for optimisation) nonetheless.
However, I'm still...