Hi @Trav, thanks for the support. I got it running. I will post my code here, just in case anybody wants to have some matplotlib-styles colorbars in his Rhino document.
Yet, it works only up to 8 colorstops (which is sufficient for me), than it behaves buggy.
import matplotlib.pyplot as plt
import numpy as np
import Rhino
import ghpythonlib.treehelpers as th
from System.Drawing import Color
from matplotlib.colors import LinearSegmentedColormap
"""
Create a colorbar in Rhino matching a definition in Matplotlib
Inputs of the Grasshopper-Node (all set to Item access and no TypeHint)
crv (Rhino.Geometry.Curve)
A closed curve used as the outline for the colorbar
cmap_name (str)
Name of the cmap in matplotlib
number_of_segments (int)
How many points are sampled. Note: WORKS ONLY PROPERLY UP TO 8
trigger_and_bake (bool)
Run and bake to Rhino
"""
ghenv.Component.Message = "ColorbarMatplotlibStyle"
def analyze_crv(crv):
"""
Simplified analysis of the curve to determine start and endpoints for the gradient.
Expand this function, if you need a gradient that is not on Rhino.Geometry.Plane.WorldXY
or a direction that is not Rhino.Geometry.Vector3d(0,1,0)
"""
if not crv.IsClosed:
raise ValueError("Expected a closed curve ")
bbox = crv.GetBoundingBox(True)
sp = bbox.Min
ep = Rhino.Geometry.Point3d(sp.X, bbox.Max.Y, bbox.Max.Z)
print(type(crv))
return crv, sp, ep
def sample_colors(cmap_name, number_of_segments):
colors = []
# Add custom colormap or get the one specified
if cmap_name in ["Plaxis", "RedGreen", "GreeenRed"]:
cmap = LinearSegmentedColormap.from_list("GreenToRed", [(0,1,0), (1,0,0)])
else:
cmap = plt.get_cmap(cmap_name)
segments = np.arange(number_of_segments)
segments = [float(i/(len(segments)-1)) for i in segments]
for value in segments:
rgb = cmap(value)
rgb = [rgb[0], rgb[1], rgb[2]]
rgb = [float(i*255) for i in rgb]
color = Color.FromArgb(*rgb)
colors.append(color)
return colors, segments
def create_color_gradient(colors, segments, sp = Rhino.Geometry.Point3d(0,0,0), ep = Rhino.Geometry.Point3d(0,100,0)):
color_gradient = Rhino.Display.ColorGradient()
color_gradient.GradientType = Rhino.Display.GradientType.Linear
color_gradient.StartPoint = sp
color_gradient.EndPoint = ep
cstops = []
for ind, c in enumerate(colors):
stop = segments[ind]
cstop = Rhino.Display.ColorStop(c, stop)
cstops.append(cstop)
color_gradient.SetColorStops(cstops)
return color_gradient
def add_hatch(crv, gradient):
if "Solid" in [i.Name for i in Rhino.RhinoDoc.ActiveDoc.HatchPatterns]:
hatch_pattern_ind = [i.Name for i in Rhino.RhinoDoc.ActiveDoc.HatchPatterns].index("Solid")
else:
hatch_pattern_ind = Rhino.RhinoDoc.ActiveDoc.HatchPatterns.Add(Rhino.DocObjects.HatchPattern.Defaults.Solid)
hatch = Rhino.Geometry.Hatch.Create([crv], hatch_pattern_ind, 0., 1)[0]
# Create and set attributes
attr = Rhino.DocObjects.ObjectAttributes()
attr.Name = "Hatch_with_gradient"
if gradient:
hatch.SetGradientFill(gradient)
# Bake to document
hatch_id = Rhino.RhinoDoc.ActiveDoc.Objects.AddHatch(hatch, attr)
return hatch, hatch_id
if trigger_and_bake == True:
crv, sp, ep = analyze_crv(crv)
colors, segments = sample_colors(cmap_name, number_of_segments)
color_gradient = create_color_gradient(colors, segments, sp, ep)
hatch, hatch_id = add_hatch(crv, color_gradient)
hatch.SetGradientFill(color_gradient)
Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.Redraw()