'Bake to Layer' component with 'M' (Material) input?

@wim already pointed that out in the other thread.

So I looked at it again and tried to use some of that code but failed. Besides, as noted in my reply to @wim, there is a bug in that example. It doesn’t have a ‘try:/finally:’ pattern and so fails to restore ‘scriptcontext.doc’ as recommended, which causes very confusing behavior if there is an error:

finally:
    #we put back the original Grasshopper document as default
    scriptcontext.doc = ghdoc

Personally. based on decades of programming experience, the weird consequences of failing to restore ‘scriptcontext.doc = ghdoc’ indicate a sketchy, prone to failure coding pattern inherent to Rhino scripting - or is it confined to Python?.