TORONTO - Terry Gilliam has endured so many obstacles on his films, his life story could be the stuff of some cosmic filmmaker's wild fable about a hamstrung artist, titled "The Curse of Gilliam."

He fought epic battles with studio bosses on "Brazil" and "The Brothers Grimm." His "Don Quixote" fantasy with Johnny Depp shut down just days into shooting.

His latest, "The Imaginarium of Doctor Parnassus," seemed doomed after star Heath Ledger died midway through the shoot. Gilliam initially felt he had to scrap the film, in which Ledger plays a shady charity fundraiser who falls in with a theatrical troupe that travels modern London in a horse-drawn, medieval-looking contraption.

"Star dies in the middle of the movie. What are you doing? This doesn't happen. What movie has carried on in that situation?" Gilliam said in an interview over lunch at the Toronto International Film Festival, where "Doctor Parnassus" played in advance of its theatrical release Friday.

"I thought, there is karma, and it hates me. I don't believe in a god, but karma, whatever that is, is out there to get me."

Resigned to letting another unfinished film go, Gilliam found his collaborators would not let him off so easily, insisting "you're going to finish this film, and that's it. It's Heath's last show. We're not going to let that be buried," Gilliam said.

The solution: Depp, Jude Law and Colin Farrell stepped in to finish Ledger's role for three fantasy sequences as the character steps through a magic mirror. The end credits bear the loving dedication, "A film from Heath Ledger and friends."

Gilliam's affection for Ledger always is evident, even as he wisecracks about the strains of finishing the film without him.

"It had to be good enough to be Heath's last movie, so I hate that kind of pressure. I was moaning about Heath all the time. (He) goes and dies and sticks me with this one," Gilliam said, letting loose with one of his trademark piercing cackles.

"This is a lesson for all young actors. You don't turn up for work today, you've got three A-list actors waiting to take your job."

"Doctor Parnassus" stars Christopher Plummer in the title role, an ancient man who gained immortality and youth in deals with the devil (Tom Waits). Ledger's character becomes the key to a new bet Parnassus makes to keep his daughter (Lily Cole) out of the devil's grasp.

Gilliam wrote the screenplay with longtime collaborator Charles McKeown.

"I think he's all the characters, really, in his movies. He unscrews the top of his head, and they all crawl out at different times of the day and night," said Waits, who also had a small role in Gilliam's "The Fisher King."

"When you're working with him, it's easy, because they're all born out of him, and if you want to make any alterations, you're talking to the tailor. It's fun to do."

The lone American among five Brits in "Monty Python's Flying Circus," Gilliam, 69, was the man behind the insane animation interludes for the troupe's TV show and movies.

His solo career started with 1977's Lewis Carroll-inspired adventure "Jabberwocky" and hit an early peak with 1981's "Time Bandits," a commercial success even though it was released by a small distributor after Hollywood studios passed on it.

"The studios couldn't recognize the finished film as being something that people would want to watch," Gilliam said. "That was kind of the first one. I was convinced that all my prejudices against Hollywood were confirmed, basically. And then 'Brazil.' That was it."

A tragedy disguised as a comic cousin to George Orwell's "1984," "Brazil" came four years later and languished unreleased and unseen at Universal, which did not like Gilliam's bleak ending.

Gilliam screened the film in secret for reviewers and ran a full-page ad in Hollywood trade paper Variety asking Universal studio boss Sid Sheinberg when he was going to release the film. Universal finally relented and released "Brazil" after the Los Angeles Film Critics Association picked it as the year's best movie.

"I rail against the system because this little village of frightened people controls what the world sees, and I don't like that. I just hate the limitations they basically impose on the world, the mirror they impose on the world," Gilliam said. "It makes me crazy."

Gilliam ran into production problems and huge cost overruns on 1988's box-office dud "The Adventures of Baron Munchausen," but he scored critical and commercial successes on two 1990s Hollywood films, "The Fisher King" and "Twelve Monkeys."

"Fisher King" star Robin Williams said Gilliam is a great collaborator for actors and other creative people, but "if you're producing his movies, you're always in the position of being the enemy. He's always said that he needs an adversarial position to kind of drive him. ...

"He's an animator at heart, and he thinks if you can think it, you can do it. And then most people go, 'Yeah, except that may be a little expensive."'

Gilliam's first collaboration with Depp, 1998's "Fear and Loathing in Las Vegas," was a flop. But Gilliam and Depp hit it off, the actor signing on for "The Man Who Killed Don Quixote," a production that folded after six days amid an illness to co-star Jean Rochefort and freak occurrences that included a monster storm washing the film crew's equipment away.

The director moved on to "The Brothers Grimm," starring Ledger and Matt Damon. Gilliam and the film's backers, then-Miramax bosses Bob and Harvey Weinstein, were at odds throughout the shoot, the film hitting theatres a year behind schedule in 2005.

It took years of legal battles, but Gilliam has regained the rights to his "Don Quixote" script. He gave it a major rewrite and hopes to begin shooting again in 2010.

"Every reasonable, intelligent person says, 'Why are you wasting your life doing this thing?"' Gilliam said. "You have to be unreasonable. You have to be illogical. It seems to me, especially if you take on Quixote, you have to be mad. ...

"I've got other things I'd be quite happy doing, but I think I have a good chance of getting this one just out of the way, out of my life. And then I can move on and do other versions of people dreaming about the world the wrong way."