Bake component in ghPython - add extra options?

I just discovered a wonderful set of ghPython scripts and in one of them I need to add some extra functionality.

More exactly, into the script listed bellow I need to add two additional options:

  • input for “layerColor”
  • Make “obj” input to accept more object types like text and dimensions.

4-8. Baking Input Objects with Attributes
http://atlv.org/education/ghpython/

http://atlv.org/education/ghpython/python_attributes8.gh

# ================

#  bake objects with attributes

#  input type - obj: GeometryBase, objName: str, layerName: str, displayColor: Color, plotColor: Color, plotWeight: float, ambientColor: color, diffuseColor: Color, emissionColor: Color, specularColor: Color, transparency: float, shine: float, wireDensity: int, bake: bool (Item Access)

 import scriptcontext as sc
 import System.Drawing as sd
 import Rhino.DocObjects as rd
 import Rhino.Geometry as rg
 import Rhino
 
 sc.doc = Rhino.RhinoDoc.ActiveDoc
 if bake and obj is not None:
     attr = rd.ObjectAttributes()
     
     if objName is not None: # setting object name
         attr.Name = objName
     
     if layerName is not None: # setting layer
         if rd.Layer.IsValidName(layerName):
             layerIndex = sc.doc.Layers.Find(layerName, True)
             if layerIndex < 0: # if the layer doesn't exist
                 layer = rd.Layer()
                 layer.Name = layerName
                 layerIndex = sc.doc.Layers.Add(layer)
             attr.LayerIndex = layerIndex
      
     if displayColor is not None: # setting display color
         attr.ColorSource = rd.ObjectAttributes.ColorSource.PropertyType.ColorFromObject
         attr.ObjectColor = displayColor
     
     if plotColor is not None: # setting plot color
         attr.PlotColorSource = rd.ObjectAttributes.PlotColorSource.PropertyType.PlotColorFromObject
         attr.PlotColor = plotColor
     
     if plotWeight >= 0: # setting plot weight
         attr.PlotWeightSource = rd.ObjectAttributes.PlotWeightSource.PropertyType.PlotWeightFromObject
         attr.PlotWeight = plotWeight
     
     if ambientColor is not None or diffuseColor is not None or emissionColor is not None or specularColor is not None or transparency >= 0 or shine >=0: # setting material
         materialIndex = sc.doc.Materials.Add()
         material = sc.doc.Materials[materialIndex]
         if ambientColor is not None:
             material.AmbientColor = ambientColor
         if diffuseColor is not None:
             material.DiffuseColor = diffuseColor
         if emissionColor is not None:
             material.EmissionColor = emissionColor
         if specularColor is not None:
             material.SpecularColor = specularColor
         if transparency >= 0:
             material.Transparency = transparency
         if shine >= 0:
             material.Shine = shine
         material.CommitChanges()
         attr.MaterialSource = rd.ObjectMaterialSource.MaterialFromObject
         attr.MaterialIndex = materialIndex
     
     if wireDensity > 0 or wireDensity == -1: # setting wire density
         attr.WireDensity = wireDensity
     
     # bake by type
     type = obj.ObjectType
     if type == rd.ObjectType.Point:
         sc.doc.Objects.AddPoint(obj.Location, attr)
     elif type == rd.ObjectType.Curve:
         sc.doc.Objects.AddCurve(obj, attr)
     elif type == rd.ObjectType.Surface:
         sc.doc.Objects.AddSurface(obj, attr)
     elif type == rd.ObjectType.Brep:
         sc.doc.Objects.AddBrep(obj, attr)
     elif type == rd.ObjectType.Mesh:
         sc.doc.Objects.AddMesh(obj, attr)
     else:
         print "object type "+str(type)+ " is not baked"
 
# ================
1 Like

I found this thread in my quest to work around the bleeding edge deficiencies and glaringly obvious flaws in R6. This snippet of Python looks very promising except that when I open that GH file, not only is the Cone not visible in the Rhino wiindow but any other geometry I add to the GH canvas also isn’t visible!?? How absurd is that!? Another in a very long list of wild goose chases in the pursuit of basic functionality that is sorely missing from Grasshopper itself. :frowning:

python_attributes8

Two years ago I cobbled together a simple Python ‘bake to layer’ component from examples found on the forum. It worked great at the time but has no ‘M’ input for material, depending as it did on the material being defined on each layer in the .3dm file. Now I’d rather avoid.that dependency, which is why I’ve extended the C# code for creating materials in GH that are suitable for viewing and baking with Custom Preview.

I’d like a Bake to Layer component that accepts the same ‘G’ and ‘M’ inputs as Custom Preview. I might eventually figure it out myself but would rather focus on my own project instead of coding GH “enhancements” that should have been built in years ago.

I would post my old Python Bake to Layer component but it appears to be broken now. :frowning:

I’m aware that some plugins like Elefront offer this kind of functionality. Why must I become dependent on a plugin for a basic, low level capability like this that should be built into Grasshopper? SERIOUSLY?

Hi Joseph - visibility settings are file-settings. That particular file - for some reason - has the preview setting set to “Don’t draw any preview geometry”…

image
-wim

Doh! :man_facepalming: OMG, thank you. I’m punchy from chasing wild geese. By the way, this code fragment 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

I’ll be continuing these thoughts in this thread: