>>>I don’t think it requires godly purposes. A priest in mortal sin can consecrate, even, if he (however obscurely) intends to “do what the Church does.”<<<
That gets into a bit of psychology. “Whether the objective is to consecrate as the Church intends,” in this case moral character of the priest is immaterial, doesn’t enter into the question of whether there is a correct intention.
I think there is certainly a matter of it being one of intent to consecrate. This is a question that the Church might have answered already.
So, for instance, a priest playing a part in a movie where he actually has the prescribed form and matter of the Sacrament, does not actually consecrate, because he did not intend a Consecration, just a simulation.
Whereas, a senile impaired priest *could* consecrate, if he had enough cognitive clarity to know and intend that he was doing what he had always done thought the years of his priesthood.
Of course I don't know all the answers about "iffy" cases. I would hardly know how to begin.
If you know of any online articles that deal with this, I'd love to look them up and learn more.