When I was into ww kayaking, I knew of places you could buy sheets/blocks of gray minicell (closed cell) foam for outfitting the interior of kayaks. The stuff is attractive and durable, but may be to difficult to carve for gun fitting (a coping saw with a narrow blade works great, but that only works in 2 dimensions). It's a rather dense foam.
You might try
www.nrsweb.com. Or do a search for "minicell foam" or "outfitting foam"
One idea would be to use a coping saw to carve out the desired shapes from one layer, then glue that over another, uncut layer. The biggest problem would be that all the cutouts would be the same depth.
Of course, industrial supply companies like MSC will probably have a bewildering array of closed cell foam:
www.mscdirect.com. Come to think of it, I'd try them first before plunging into my wacky minicell idea.
I have no idea of any this will help, as I really don't know the general procedure for shaping foam or the specific type of foam needed for gun cases.