I experienced muscle spasms after my hip replacement surgery, and want to second (and third.....

) the suggestions to try magnesium. In fact, years later I still take it each night, as I found it also helped with restless legs and with helping me relax to sleep better.
Are your spasms related to a specific surgery, an ongoing issue, or just out of the blue? No matter the cause, they are so painful, and once they take hold it's hard to manage the pain.
After my hip replacement, when I first stood up out of the chair or the car, I gave myself a minute to gently stretch and kind of work the kinks out before I charged off walking. When riding in a car I tried to do ankle rolls and gentle leg flexes to get ahead of the tightening. I continue to do these when I ride long distances in a car or plane.
Another supplement to ask your doctor about is turmeric/curcumin, which is an excellent anti-inflammatory. It helps with pain relief and a number of other issues. While it has been well researched, it's always good to make certain that supplements are compatible with your other medications.
I found that the muscle spasms, "log leg", and feeling like I was sitting on a rock or hard tennis ball on my bottom, gradually left. Sure hope the same happens to you. Take care.