Yes, this is a mitigating factor that the judge should actually take into consideration.  Before sentencing, a defendant should request a "presentence investigation report" so that he can let the court and probation department know his/her condition. If the mental illness was related to the commission of the alleged offense, a defendant may have other remedies, like not being guilty by reason of insanity.