Among the most common feature requests for Grasshopper over the last ten years is the ability to "bake" geometry to layers without going through a tedious manual process. As of January, 2020 (Rhino 6), this basic functionality is still missing, so with considerable effort, agony and distraction, I came up with this small set of open source Python components to do the job:
When you have a GH model with a dozen layers or more of geometry that need to be re-baked many times per day, the time, tedium and effort saved with a Bake component and a DelAll (Delete All by layer) component, as shown here, is truly incredible. It changes everything about the work flow. One click on the 'DELETE All' (by layer) button deletes all the geometry on the specified layers, another click on the`'Bake' button activates multiple Bake components (as many as you like) and voila! All the geometry created by GH is replaced in seconds.
The Bake component requires the second output from the related mMaterial component, 'rM' (Render Material). Though both 'M' (Display Material) and 'rM' are based on the same material definition internally, I wasn't able to derive 'rM' from 'M', unfortunately.
The DelAll component accepts a list of layer names and deletes all the geometry on the specified layers.
Each of the four groups in this model test and demonstrate different ways to use it. The first and last group set material source to 'By Object' ('mSrc' = 1), the second and third groups use 'By Layer' ('mSrc' = 0, the default). The second group assigns 'rM' to the specified layer. The third group doesn't have an 'rM' input so it doesn't affect the layer material, expecting it to be assigned manually in the Rhino file, before or after baking (the two "wooden" spheres). Using 'mSrc' = 1 ('By Object') doesn't affect the layer material either, it adds 'rM' to the list in the 'Materials' panel.
See 'Passing Geometry and Material between GH Models' below.
mMaterial_bake_to_layer_2020Jan30a.gh (39.4 KB)
As explained on the Create Material page, 'Display Material' ('M') can be passed through the Rhino 6 Data Output and Data Input components because apparently it consists of XML, a string. But 'Render Material' doesn't work that way. To circumvent this obstacle, a third output was added to the mMaterial component, 'JSON', to convert mMaterial input values to text for transmission via Data Output. Subsequent Grasshopper models use these JSON values from Data Input as input to a json_M component that replicates both 'M' and 'rM', so passing it alone enables both Custom Preview and baking geometry.
For example, in the first model below ('STEP1_mMaterial_bake_to_layer'), the orange group passes geometry (the cones), the layer name(s), JSON material and 'mSrc' (material source) via Data Output. The second model ('STEP2_mMaterial_bake_to_layer') manipulates the geometry and re-uses the other three parameters to display and bake it, as defined in 'STEP1'.
This video is a comparison between the slow, error prone standard method of baking vs. the open source Python Bake component that does the job in seconds.