Rob McElhenney and Kylie Shea in It’s Always Sunny in Philadelphia. Photo: FXX

When It’s Always Sunny in Philadelphia ended its 13th season on Wednesday night, you’d be forgiven for wondering, Wait, did I switch channels? Not only does “Mac Finds His Pride” contain heartfelt dialogue — like Mac admitting to Frank, “I don’t know where I fit in as a gay man and it’s starting to get to me. I’m not feeling very proud.” — but it ends with a five-minute, show-stopping contemporary-dance number featuring Rob McElhenney and professional ballerina Kylie Shea.

Yes, that Rob McElhenney, the creator of Sunny who also plays Mac.

In the finale, Frank (Danny DeVito) is trying to convince Mac to prance on the gang’s gay pride parade float, but Mac isn’t feeling it. In his own way, Frank tries to help: “I never really got you and, to be honest, now that you’re gay I get you even less. Nothing against it, I just don’t get it.” Nevertheless, he vows to help Mac find his pride, which involves coming out of the closet to his imprisoned father, Luther (Gregory Scott Cummins).

But Mac can’t open up to his father, at least, not with words. Which is a good thing because if he did, it would sound something like what he threw at Frank when he tried to explain his internal struggle: “There’s like this storm inside of me and it’s been raging my whole life, and I’m down on my knees, and I’m looking for answers, and then God comes down to me and it’s a very hot chick and she pulls me up and we start dancing.”

That sounds like typical Mac nonsense, but the surprise comes when Mac ultimately manifests his internal musings into a full dance number that he performs in prison for his father, Frank, and the other inmates. His father walks off in the middle of it, leaving Mac in tears on the dance floor. (Mac in tears!) But Mac’s mermaid/goddess/partner pulls him off the floor and they continue, her support inspiring him to own his truth. The episode ends with Frank’s awestruck words, “Oh my God, I get it. I get it.”

Here’s the inside story of how Sunny pulled off such an unexpected finale surprise, according to McElhenney, his dance partner Kylie Shea, the choreographers behind the sequence, Danny DeVito, and the episode’s director.

The Idea

If you’re a regular Sunny viewer, you know that Mac looks a little different this season. He’s extremely buff and takes off his shirt every chance he gets, even though the gang could care less about his muscles and abs. McElhenney says he decided to beef up Mac because of a trend he noticed over the last few years.

“Every time I turned on a movie or a TV show, a man was taking his shirt off and he was always insanely ripped,” he said. “And I don’t mean like Marvel superhero characters — insurance salesmen, marketing executives, and chicken delivery people. These actors read that they’re gonna be in a sex scene and they spend four months at the gym and starve themselves. It’s a really fascinating aspect of the culture right now that I thought would be funny to explore on our show, where a character did all of that work and everyone is actively unimpressed.”

At the end of the finale, it’s revealed that Mac’s in the best shape of his life because he’s been practicing his dance piece for months. The idea for Mac to express himself through dance emerged early in the season, as the writers discussed different ways to explore Mac’s sexuality after he came out of the closet last season. “We got a really overwhelming emotional response from the LGBTQ community last year,” McElhenney said. “I took it seriously and I felt it would be completely unexpected to have this much more emotionally resonant end to the season. You would expect that Mac would express himself through the art of contemporary dance and it go horribly wrong, until you realize that’s not the direction we’re taking.”

The only obstacle to pulling it off is, admittedly, a big one: McElhenney says he can’t dance. Or, at least, he thought he couldn’t. But that didn’t stop him, according to the finale’s director, Todd Biermann, who has been friends with McElhenney since they were children. “Rob marches to his own drummer,” Biermann said. “He’s always trying to challenge himself in ways I don’t think most people would. He’s not a terribly athletic person, but he got in insane shape for that. I can’t say I’m surprised by it, but I was certainly amused and entertained and very impressed.”

Photo: FXX

The Training

When Mac gained 50 pounds to become “Fat Mac” in the seventh season of Sunny, it wasn’t complicated: McElhenney ate five 1,000-calorie meals a day and met his goal in five months. This challenge was different and required not just a total body transformation, but also learning to move in a new way. To get started, McElhenney called on celebrity trainer Arin Babaian who has worked with Channing Tatum and Joseph Gordon Levitt.

His new diet consisted of no alcohol, no eating after 7 p.m., and no carbs or sugar. He also lifted weights six days a week, ran three miles a day, and stretched for an hour a day — and all that was before he also started training a few times a week for a couple of hours a day with choreographers Alison Faulk and Leo Moctezuma.

“I worked with Arin to get into good enough cardio shape to be able to dance for two hours a day and we did a lot of lifting and also a lot of stretching,” McElhenney said. “I’m not a very limber person, so I stretched for like an hour a day just to be able to move my body in those ways which I was never able to do before and will never be able to do again.”

A few months later, McElhenney says he began his work with the choreographers with a simple sentence: “I have zero ability to dance.” But Faulk, who choreographed Magic Mike and worked on Sunny’s Magic Mike spoof last season, disagreed. “They were so patient. it got to the point where I was looking forward to working with them at the end of each day,” McElhenney said. “It didn’t feel like work or exercise that I didn’t want to do.”

Photo: FXX

The Dance

From the beginning, the purpose of the dance number was to communicate Mac’s inner turmoil to his father. But it took time for McElhenney to settle on the type of dance he wanted to do. “We weren’t sure what we wanted it to feel like,” he said. “Should it be sexy? Should it be funny? Should it be more hip-hop because break-dancing and hip-hop is more Alison’s specialty? It just didn’t feel like it was hitting all of the emotional bells that we wanted.”

He knew that he wanted it to be a partner dance, which is why Faulk brought in Moctezuma, her choreography partner of ten years. “Rob came out of the writers room saying he wanted it to represent the struggle, the push and pull, and that helped Leo and me to put the choreography together in a way that showed vulnerability and strength,” Faulk said. “The woman represents the light and the good and everything pure and amazing — and he’s the dark. So it’s basically a giant metaphor for being able to love and accept yourself.”

But making the intention of the piece clear wasn’t that important to McElhenney, since Mac is so confused about his sexuality. “We thought what would be funny about it is that it’s kind of confusing. We’ve set it up in the script that his explanation is also intensely confusing,” he said. “We realized if he couldn’t really verbalize what it is that’s happening to him, it doesn’t matter because he’s just hoping for someone — whether it’s God, his mom, or his dad, or his surrogate father Frank — to say that it’s all right to feel what I feel and be who I am.”

There was a practical reason for Mac to have a partner, too. “If I did it by myself and if I couldn’t pull it off, then it would just come across as funny,” McElhenney said. “If we got a trained dancer — in our case, incredible dancer and ballerina Kylie Shea — then hopefully everybody’s watching her. My purpose in the dance is to exalt her and make her look as good as possible.”

When they opened auditions for dancers, they wondered if they needed someone who was at the top of her technical game or should be more raw and emotional. “What we found with Kylie was that she just had everything,” he said. “I probably saw 15 dancers and they were all so incredible that I didn’t know how I was going to make the choice. But the one the three of us kept coming back to was Kylie.”

The audition consisted of about a minute and a half of the actual choreography, which the prospective dancers performed with different male dancers. During callbacks, the finalists auditioned with McElhenney. “We tried one of the lifts, just to see how we moved and how we vibed together,” Shea said. “I think everyone had the same reaction. We couldn’t believe he wasn’t a dancer. He wowed me from the very first day with his abilities as a dancer.”

McElhenney and Shea initially rehearsed to Nick Jonas’s “Close,” and though McElhenney liked it, something was off. “It felt more of an expression of sex and sexuality as opposed to the struggle to accept oneself and the natural beauty of that,” he said. At the time, he’d also become addicted to watching dance performances on YouTube after practice and fell for the idea of dancing to an instrumental song. McElhenney and the choreographers spoke with music supervisor Andy Gowan, who offered a handful of other music choices, including the song they landed on, Sigur Rós’s “Varúð.”

“Three minutes in, Alison and I looked at each other and knew this was the song,” McElhenney said. “But [Sigur Rós] are very protective of their songs, so we had to reach out to them personally and give a description of what we were planning on doing. They signed on immediately, which was great.”

Photo: FXX

The Rehearsal

For McElhenney to learn a few dance moves was one thing. To learn how to partner dance was an entirely different test, one that required establishing complete trust. Faulk and Moctezuma danced the routine over and over in front of McElhenney to prepare him for the hefty responsibility of being Shea’s partner. Then, he’d practice the steps with Faulk.

“Rob is naturally a caretaker, and that’s something you have to be as a partner. We can’t let our partner fall. It’s not about us as the male dancer; our goal is to make our partner look beautiful,” Moctezuma said. “He would get it on the first try with Alison and we would be like, What the hell?”

Shea, who spent three years partner dancing for a Seattle dance company, knows a thing or two about being dropped. But in the two-and-a-half months she rehearsed with McElhenney, she says it never even got close. She described the final jump in the dance — a running leap into McElhenney’s arms that they rehearsed hundreds of times — as “exhilarating.”

“You have to establish this blind trust, and you have to know your partner is there and will do everything in their power to protect you because you could get injured and your career would be over,” Shea said. “But the thing is, Rob instinctually has all of the qualities of an incredible dance partner. He’s the type that would throw himself on the floor just so you wouldn’t get hurt.”

McElhenney says the realization that he could actually pull off the routine struck him bit by bit. “We held off recording for as long as possible because once you see yourself doing it, you become much more insecure,” he explained. “But I remember when we recorded the first section of it and played it back, it was horrible. Just garbage. I almost thought I’d quit, but then I saw one moment that worked and I thought, If I can do that, I can build on it.” It also helped knowing it wasn’t a live performance, so “if we have to shoot this thing 100 times to get it right, we’ll shoot it 100 times but we’ll get it,” he said.

Then McElhenney decided he’d make the whole thing even harder. He had seen Faulk’s and Moctezuma’s piece in the Las Vegas Magic Mike Live show in which a pair dances in the rain, and found it “visually arresting and beautiful.” So he asked the choreographers, “Can we do that?”

Moctezuma replied: “Well, you can’t do that. That’s been done. But we can definitely create something specifically for you that has this water element.” They were also brutally honest — adding water would make it “exponentially more difficult,” Faulk said.

Still, McElhenney was not deterred. “Collectively, we all really just liked the metaphor of the storm that’s going on inside him and then the sun comes out,” Faulk said. “Visually, it’s just exciting to look at. It just feels dangerous and cool and makes it more of a struggle.”

Shea said she was warned about the water element in auditions, but “hearing about it and dancing in it are two different things.” He first day moving on the wet floor was “a slip-and-slide show,” she said. “It was almost like learning the dance all over again because, in the water, everything is different every single time. And also, the trust in the water was at a whole other level.”

Although there were never any injuries, everyone recalls a freak flip that happened during rehearsals once the rain had been added to a section of the dance they called “the death spiral.” Faulk practiced it first with McElhenney to make sure that Shea would be safe. He was supposed to swing her around, twirl her with one hand, and then let her go in the gushing water, but something unexpected happened when he moved his foot and lost the grip. As Faulk spun, McElhenney flipped forward, landing like a ninja to prevent her from falling. “It was insane!” said Shea, who watched it happen alongside Moctezuma.

“I don’t know what happened there, it was like holdover jujitsu training,” McElhenney recalled. “I lost my footing and I did some kind of bizarre flip and landed like Spider-Man. Unfortunately, the cameras weren’t rolling.”

Photo: FXX

The Filming

In June, seven months after McElhenney began training, the five-minute dance scene was finally filmed on a soundstage. Biermann hired a camera-crane operator from Dancing With the Stars because of his experience following movement. During the 12-hour shoot, they split the dance into sections and spent at least half the day filming with rain falling on the perimeter of the stage, but not on McElhenney and Shea. “Just to make sure we got the minimal aspect of it, we filmed it first like that,” Biermann said. “But at the end of the day, we wound up using overwhelmingly the stuff that was in the rain. It just looked so much more compelling and interesting when they were soaking wet.”

Most of the actual shots used in the episode came from the last three takes. “It was one of the most physically demanding days of dance I’ve experienced and I’ve done a lot of dance,” Shea said. “But the way we prepared made the shoot go very smoothly.”

As if watching Mac glide effortlessly in the water and power-lift his partner over his head wasn’t substantial enough of a departure, he also breaks down and cries in the middle of the dance. Seeing McElhenney get emotional made everyone else weepy in rehearsals. “I would tell him I can’t watch sad Rob and he would say, ‘I’m not sad, I’m just acting,’ but it was still too much,” Faulk said.

Biermann, who has directed over a dozen Sunny episodes, was impressed with the level of ambition McElhenney showed for the scene. “His goal was to figure out how to make people laugh and cry and really be touched,” he said. “You’ve always seen Mac brush off his father’s lack of interest in him and this is the first time you really saw that land with any kind of emotional impact.”

The scene ends with Frank finally understanding what Mac has been trying to share with him. DeVito was not present when McElhenney and Shea filmed the dance, but he, Cummins, and the actors playing the inmates watched it on a movie screen the day they shot their reactions.

“It’s just remarkable how little command Mac has of his intelligence,” DeVito says with a laugh. “He’s the most conflicted person I’ve ever met in my life. I thought it was brave thing for Rob to do this, to tackle this story in this way. I was very proud that he tackled the insides of Mac and I was very moved by the dance. Thank God for the woman, because she was brilliant! I could watch it all day long. I thought it was really, really sweet, man.”

DeVito joked that he wasn’t captivated by all of the lifts in the number because “the girl only weighed 36 pounds. [Rob] had to flex his muscles to make it look hard! You know, I don’t mean it. Rob doesn’t fool around. He was very dedicated to it and I was impressed with his technical prowess. He looked good doing it. I’ll tell you one thing, I don’t think I could ever do it. Leave it to Alvin Ailey.”

But did he look good enough that there’s more dancing in Mac’s future? “I still can’t dance!” McElhenney said. “I can do just that one routine. If I went to a club and I started dancing, I would probably try to do that routine that I’ve done at least a thousand times. I can only express myself through the art of contemporary dance very specifically to that song and that number.”