FROM Detective Alonzo Harris in Training Day to Dirty Harry, the movies have not been short of bad lieutenants.

Perhaps none, however, can hold a charge sheet to Scottish Detective Sergeant Bruce Robertson of Irvine Welsh's Filth fame.

How awful is he? James McAvoy, the actor who brings him to life in the forthcoming film of the same name, describes Robertson as homophobic, racist, sexist and misogynist. And that's just for starters.

Jon S Baird, the Scots director in charge of bringing Bruce from page to screen, therefore has a tough sell on his hands.

"Outrageous", "uncomfortable" and "harsh" are just some of the words he uses to describe the scenes that show Robertson's decline, fall, and sex life, but he says them all with a laugh in his voice. That's Filth, a blend of comedy and emotional carnage. Casting McAvoy was critical, says Baird, to bringing the audience on side with such an appalling character.

"We opened the film in a particular way, with a bit more of Bruce's charm than his real darkness. You have to buy into him at the beginning, take a deep breath and say I'm going to go with you. Scottish people are quite good at that, our humour is very tuned in.

"But that being said, with any other actor I don't think it would have worked. It takes a special talent like McAvoy's, who himself has got an amazing sense of humour and is an incredibly intelligent guy, to bring that off the page."

It's also important he adds, that the audience should understand what has made Robertson the man he is. "Bruce couldn't just be this one-dimensional bad guy. What would be the point?"

Baird met Welsh in 2008 at the book launch for Crime, the sequel to Filth. Deal eventually done, Welsh left Baird alone to write the screenplay. He stayed involved, taking meetings with financial backers and others and travelling to Cannes to generate interest. "He was instrumental in creating a buzz for the project before we got it made." The two did not know for sure that they had found their lead until an initial meeting with McAvoy.

"When he walked in he did look very young and Irvine whispered to me, 'He looks 15, how's he going to play Bruce?" About 30 seconds after he started talking about this character he totally changed into this grizzled, middle-aged detective before our eyes."

The shoot was something of a homecoming for the Aberdeen-born Baird, who started out as runner on films after leaving university. After writing and directing in television, a short film of his, It's a Casual Life, led to his first feature, Cass, about English football hooligans.

Baird had tried to get into film school but did not have enough in the way of experience. While he still wishes he had been accepted, he sees the value of coming up a different way.

"It doesn't matter if it's the film industry or whatever industry, if you can speak to the guys on the ground and say, 'I've done what you are doing, I appreciate it, but give me a bit more', that's a great benefit."

Baird, 40, who is married and has a daughter, divides his time between Aberdeen and London, where he has lived for 16 years. Filth, he says, is a "proudly Scottish film". It was filmed here, in Edinburgh, Glasgow and Stirling, with sojourns in Germany, Sweden and Belgium. It has a strong Scottish cast apart from McAvoy that includes Gary Lewis, Shirley Henderson, and John Sessions. Also starring are Jamie Bell (an honorary Scot after Hallam Foe) and Jim Broadbent. The crew was predominantly Scottish too.

"That's why we've decided to release it in Scotland a week before the rest of the UK. We are proud that we made it here and we want to give back a bit."

Among the surprises in store is a mystery guest star. Being careful not to give too much away, Baird had written a scene that required a song. He called Welsh and asked if he had heard of this particular tune. It turned out it was a favourite of "Irv's" on karaoke nights. Even better, he had a contact for the singer.

"We got in touch and I said this is a mad request but would you like to be in what was probably the most mental scene in the whole film singing your own song? He was great."

Set in the run-up to Christmas, Filth features some of the best/worst festive pop known to man.

"We like to say it's guilty pleasure music. It's the kind of stuff you are not really supposed to like but there is a bit of foot tapping going on. The reason we did it is because Bruce Robertson is kind of a guilty pleasure in places. Regardless of the most despicable acts a lot of the time you can't help wanting to know what is going to happen next to this guy."

Baird has no regrets that the film has been given an 18 certificate. "You can't compromise with Irvine Welsh," he says. "If we had made this a 15 we would have shot ourselves in the foot because the book is so uncompromising."

Filth opens in Scotland on September 27