As a matter of personal policy, I've tried to avoid derivative works in writing music. There are exceptions to this rule, because there are always going to be exceptions. There's The Destroyer, based on Gordy's Tam Olyn character from Soldier, Ask Not. But Gordy liked and encouraged that sort of thing. There's The Songs of Distant Earth, which is actually based on the back story that Arthur C. Clarke created for his novel of the same name.
I've also written (with or without daisy_knotwise) a few songs to existing tunes, including Son of a Son of Isildur (which answers in my mind the question of "What if Jimmy Buffett were a big Tolkien fan?") and Working the Convention Blues, which lifts a Jim Croce tune to good effect. But I mostly don't go there, because the Supreme Court's decision in Acuff-Rose v. 2 Live Crew has made such a mess of U.S. parody law that it's hard to be sure that you're landing on the right side of it. (Although I'm convinced that the small quoted bits of melody in My Husband, the Filker pass Souter's scratch-and-sniff test, so that'll be on the next album.)
I mostly don't write to other folk's tunes because I can create quite enough tunes by myself, thank you very much. The only reason to write to someone else's tune is if you have something to say that can't be said well in any other way. See Buffett/Tolkien above.
And the same applies to writing in someone else's fictional universe. The only reason that I would need to go there is if there's something that I need to say that can't be said any other way. That's seldom the situation I find myself in and I tend not to go there, if for no other reason than the extreme nastygram that one of my filking friends got from an author's lawyer many years ago. And it's not as if some ideas can't have the serial numbers filed off of them and still work just fine in song.
But I find myself in a different situation when writing sketch comedy for stage. There are a lot of funny things that you can do with familiar characters thrown into an absurd setting, as Mad Magazine has demonstrated over the years. So I find myself writing (or co-writing) bits like The Honeyearthers, the lost musical episode Babylon 5: A Late Delivery from Broadway, the written-after-seeing-The Phantom Menace Star Wars 2: A Long Day's Journey Into Jedi Knight, My Daddy Can (Two children are taunting each other in the park, where the boy's father is the strongest man on Earth, while the girl's father is the smartest man on Earth. And when Clark and Bruce show up to collect the kids at the end of the bit, you find out that they weren't kidding.), and so on.
Of course, part of the essence of comedy is playing it straight. So while we engage in parody, we don't use the old Mad trick of inserting Captain Krock for Captain Kirk. I tend to think it's funnier when you aren't changing the names. And when Norton or Delenn hit the stage ("John, I have terrible news!") and they get a round of applause from the audience, you know that the audience has bought into the willing suspension of disbelief that is making the whole thing work.
But that's comedy and probably parody and probably legal. I think.
The whole thing's frustrating, though, because the Constitution talks about preserving copyright on works for a "limited time". And that time has become so long -- and will probably be extended again by some future Congress that's busy following the money -- that works that are currently in copyright may never fall into the public domain.
So while Disney was free to strip mine Grimm's Fairy Tales and Brer Rabbit, we may never be free to strip mine anything that Disney's done.
And I have a problem with that.
Just to complete this essay, I'll note that I've had my music used by other filkers. And that's generally fine by me, as long as I get credit. Thus, I complained to the editors of the NESFA Hymnal Volume 2 when I noticed that Leslie Fish's Destroyer in the Diadem was listed as being to "Original Music". Well, yes, it was, but it was my original music, so I asked to be credited for it in the next printing. And another filker managed to lift almost the entire melody of One Last Battle, changing only the last line of the verse. I brought it to her attention and she shrugged it off. However, when it showed up in a song book, I mentioned it to the publisher, asking to be credited in future printings. (It's my first song that stuck. I'm rather attached to it. :) )
I've managed to inadvertently lift from my good buddy, Clif Flynt, twice. The first line of the melody for Gunga Wells is damned near identical to Clif's Mama Rosa's. We've chalked that up to there being a finite number of notes in the universe.
On another occasion, I seem to have created a tune for Martha Keller's Brady's Bend that Clif tells me is identical to one that he wrote that I don't recall ever hearing. I actually put the tune together after listening to Anne Passovoy beat out the rhythm on the back of her guitar, just speaking the words. That one's marked down as "parallel evolution" and Clif has ceded credit to me, since no one remembers him singing his version and the version that I sang appears to have stuck.
That's all. For now. :)