Assuming that your 401k allows for loans at all, and assuming that it does not place restrictions on why you are taking the loan, yes, you can do that.

But I thought it only fair to let you know what some potential penalties might be IF the worst happens, since such a loan cannot be discharged in the BK and you would continue to be responsible for it IF you lose your job.