The 34-year-old Last King of Scotland actor was cast to play bigoted and corrupt detective sergeant Bruce Robertson whose addictions to sex, drugs and alcohol are portrayed vividly throughout the film.
But Peterhead-born director Jon S Baird admitted he initially presumed McAvoy - who was brought up on a rough housing estate in Glasgow's Drumchapel - would be too "middle-class" for the role.
Recalling when he and producer Ken Marshall were first introduced to the father-of-one at a London hotel in early 2011, Mr Baird said: "I remember the day we met so well.
"Irvine, Ken and I were waiting for him to arrive, and in walked James wearing a baseball cap, looking about 15-years-old.
"Irvine and Ken left us together and as soon as we started talking about the character, James completely changed into this grizzled, middle aged cop."
He continued: "He started telling me about his tough upbringing and life in Glasgow growing up with his grandparents. I originally thought James was a middle-class Scotsman but he's definitely not. He's far edgier than people might first imagine.
"McAvoy's intelligence, humour and edge, but more importantly his comprehension of mental illness, immediately put him in a league of his own, and the hunt for Bruce Robertson had come to an end."
In the movie, McAvoy - also known for his roles in X-Men and Atonement - plays Robertson, a racist, homophobic and sexist Edinburgh policeman who will stop at nothing to get what he wants.