Yes. The funds in the 401k are not even property of the bankruptcy estate, so you may do what you wish with them, including taking out a loan.
Mark J. Markus, Attorney at Law
It's probably going to be ok so long as at least 30 days has passed from the time of your creditor's meeting and your 401k is exempt.
There could be several other factors at play and you should really speak with a bankruptcy attorney in your state to determine if this is a wise course of action.
Please do not accept the preceding as legal advice. The answer is provided for informational purposes. I would encourage you to seek an attorney licensed and practicing in your state for a more complete answer.