If he’s already in trouble with the court, I would go in and talk to the district attorney or prosecutor & explain the situation.
They may be willing to help even though they are the ones who would be going for a guilty verdict.
The judge would probably have to sign off on the actual order, but most judges take the recommendation of the DA/prosecutor very seriously.
(In most places, you can’t get in to talk to the judge because they’re supposed to be impartial, but you shouldn’t have any problem getting an appt to speak to the prosecutor. I’ve done it in the past for other issues when a family member has been in trouble, not for mental illness - just for being stupid.)
I’m surprised the attorney you’re working with hasn’t suggested filing some type of motion with the court to do this - or went to talk to the prosecutor himself. All the lawyers know the prosecutors - they talk like old friends in the courtroom.