When I write music (which, admittedly, I don't do often), I try to think about the different aspects of the piece (tempo, rhythm, dynamics, instrumentation, form, etc.) individually, and consider the options each presents, and look for where I can be creative in each. Melodies and riffs come to me while making those decisions. I don't know if that makes sense or helps or whatever...