After a career dotted with ego trips, scandals and puzzling decisions, Rob Lowe has finally learned to get out of his own way.

The actor — still a heartthrob at 48 — also has found an unlikely home: as a leading man and producer for Lifetime movies about cops who go bad and divas who do wrong.

His latest is “Prosecuting Casey Anthony,” airing Saturday, in which he fails to convict the woman accused of killing her child. But speaking to The Post, it seems Lowe wants to convey the lessons of his life as much as he wants to convey the message of the film.

Looking back at the beginning of his career, which started when he was 15, one thing is clear.

“By that time, I was my son’s age — he is a freshman in college — I was already a jaded, war-torn Hollywood veteran,” Lowe says, calling from his dressing room on the set of his NBC series, “Parks and Recreation.” “Girls were knocking down the barricades, I was drinking, and I had no idea who I really was.”

You know about his days as a young matinee idol, when he had to bounce back from a sex scandal, which he did by mocking himself on “Saturday Night Live.” Lowe was just 22 in 1989 when he was caught in the first-ever celebrity sex tape scandal, with two girls, one of whom he later learned was underage at 16.

The scandal, however, turned out to be the most positive learning experience of his life, and led him to sobriety.

We all know about his return to A-list status when he played the idealistic deputy White House communications director Sam Seaborn on “The West Wing.” And if he had been able to set his ego aside, Lowe could have stayed on top then and gone straight from the Aaron Sorkin series into Sorkin’s feature films.

But Lowe became his own worst enemy, and it was never clearer than when he and the rest of the cast went to Washington, DC for a magazine cover shoot at the White House.

Martin Sheen showed up, and so did John Spencer. But Lowe, who was only blocks away in a hotel, backed out at the last minute. Reportedly, he wanted to be on the cover alone.

It was a tumultuous time for Lowe, and when he left the show — he quit after three seasons, but the show continued on triumphantly without him for three more years — rumors ran rampant that he did it over lack of airtime and rivalry with Sheen.

It was ironic and sad since Sheen had been a mentor to Lowe, who was friends with Martin’s son Charlie.

Lowe now says he left “The West Wing” because everyone got a raise and he didn’t. “And it just wasn’t right.

“How many times have people asked me why I left ‘The West Wing’? Did I ever regret it? Why did I do it, how did it happen?

“It is like saying to somebody who is ecstatically happy in their second marriage, ‘Do you regret your first divorce?’ There is no short answer that does not make it unduly terrible.”

“Brothers & Sisters” was Lowe’s next big TV conquest, and with his arrival came a ratings boost — but he left that show because he felt he was under-used. Although he stayed on the show for five seasons, some cynics speculated it was part of his old ego-driven pattern.

These days, Lowe — still chiseled and seemingly un-aged — has transformed his career again, sinking his perfect pearly whites into meatier and different types of roles in a series of Lifetime movies.

Last year he played Drew Peterson, the police officer who killed one of his wives, and is suspected in the disappearance of a second, in “Drew Peterson: Untouchable.”

And this Saturday, he plays Jeff Ashton, the Florida prosecutor who tried Casey Anthony for the murder of her daughter Caylee — and famously lost.

“Drew Peterson was a big swing for me,” he says, “and when it worked out so well, Lifetime wanted me to do something else. They said how about the Casey Anthony story?

“How did this team not get a conviction on a woman that all of America decided was guilty?” says Lowe. “That is what this movie is about.”