NEW YORK — Writing his HBO comedy “Crashing,” Pete Holmes found unlikely inspiration in … “Breaking Bad”?

“It’s about a dorky guy who gets in a world above his head, so it’s like a comedy ‘Breaking Bad,’ ” says Holmes, the show’s creator and star. “There is something weird: When I was writing it, I was like, ‘Walter White had to learn how to become [meth kingpin] Heisenberg, and Pete’s kind of learning how to sleep with a girl that’s not his wife.’ ”

Executive-produced by Judd Apatow — whose companion series “Girls” leaves HBO after six seasons this spring — “Crashing” (9:30 p.m. Sundays) is the semi-autobiographical story of a stand-up comedian named Pete (Holmes) who moves out after he walks in on his wife, Jessica (Lauren Lapkus), cheating with another man. Over the course of the eight-episode season, Pete struggles to book gigs and hone his craft while staying on the couches of funny friends Artie Lange, Sarah Silverman and T.J. Miller, who play themselves.

Holmes, 37, wrestled with the idea of a personal comedy for years, after candidly discussing his divorce with fellow comics on his Nerdist podcast “You Made It Weird with Pete Holmes.” But after his Conan O’Brien-produced TBS talk show was canceled in 2014, he flew to Los Angeles and pitched “Crashing” to Apatow on the set of Amy Schumer’s “Trainwreck.”

“When he pitched this idea, I said it was too sad,” says Apatow, sitting on “Crashing’s” Brooklyn set, which includes mock-ups of a seedy comedy club and drab motel room. But he eventually warmed to Holmes’ positive perspective. “A lot of comedians, especially in stories [about breakups], are really screwed-up people. This is more a person looking for the light but being in this dark comedy world.”

When Holmes got divorced at age 28, friends suggested he turn the story into a one-man show. But “at the time, I didn’t really do personal comedy,” he says. “I liked ‘Seinfeld,’ observational stuff, so I wasn’t really open to it.” Nearly a decade later and newly engaged, enough time has passed “to digest what happened and gain perspective, because it’s very important for me to do a show that represents both sides of [a breakup]. I feel if I did a show based on my divorce a year or two after, it might’ve been bitter, painting my ex-wife as some sort of villain and that’s absolutely not who she is.”

Holmes says his short-lived talk show opened him up to collaboration and changing things on the fly. Shooting a dryly awkward encounter between Pete and his ex-wife’s bohemian boyfriend, Leif (George Basil), Apatow regularly chimes in from the director’s chair to improvise dialogue with the actors, who struggle to avoid cracking up mid-scene.

“If you looked at the script, it was completely different,” Holmes says afterward. “But we know the essence of it, and that’s how we get along most.”

“Crashing” has also been rewarding on a personal level. “When you’re sitting at a table with someone pretending to be a character loosely based on your ex-wife and re-enacting loosely something that happened, it’s beyond therapy,” says Holmes, a former Chicagoan. “It’s draining in a way that I never could’ve anticipated, but there’s a catharsis to it as well.” Nowadays, “people want the whole comedian. They don’t just want your tweets, they want your biography, and if the comedian is willing to do that, that’s where it’s at.

“That’s what we’re doing here,” he adds, “but I know we’re doing it because I’m incredibly uncomfortable — and having the time of my life.”

Patrick Ryan, USA TODAY