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"
# ================