Rhino Compute, Coloring Surfaces and baking

Hi all,

I have a rhino compute ghx script where I try to take curves and turn them into surfaces and color them. The way I have done it so far is turning them into meshes and using ‘mesh colors’ which has been working fine however meshes are too large and I want to try and bake surfaces with colors instead. There is no gh component I am aware of that will do that so I am using a custom python component from the Rhino website, however this uses active doc and guids to colorise the surfaces and compute runs headless so this doesnt work. Any way around this or another method so I can color surfaces in?

Attached here is the ghx compute script.
zoning.ghx (130.8 KB)

Here is the code for the python component.

import rhinoscriptsyntax as rs
import scriptcontext
import Rhino


if B:

    print(type(G))  # debug message to Python output

    # we obtain the reference in the Rhino doc
    doc_object = scriptcontext.doc.Objects.Find(G)

    if doc_object is not None:
        print(type(doc_object))

        attributes = doc_object.Attributes
        print('the type of attributes is: ' + str(type(attributes)))  # debug message to Python output

        geometry = doc_object.Geometry
        print('the type of geometry is: ' + str(type(doc_object)))  # debug message to Python output

        # we change the scriptcontext
        scriptcontext.doc = Rhino.RhinoDoc.ActiveDoc

        # we add both the geometry and the attributes to the Rhino doc
        rhino_brep = scriptcontext.doc.Objects.Add(geometry, attributes)
        print('the Rhino doc ID is: ' + str(rhino_brep))  # debug message to Python output

        # set print color if provided
        if C:
            rs.ObjectColor(rhino_brep, color=C)

    # we put back the original Grasshopper document as default
    scriptcontext.doc = ghdoc
    a = G

Thanks

I’m not sure if this is of any help, but it’s straightforward to recolour Grasshopper geometry with a Custom Preview component, and turning Preview off on any other component containing geometry that might obscure the result.

I use rs.ObjectColor to recolour Rhino Objects, but I also adjust things with rs.ObjectColorSource, rs.ObjectPrintColorSource, rs.ObjectPrintWidthSource and rs.ObjectPrintWidth, and set PrintDisplay mode on.

Hi James,

I don’t think custom preview will work because it doesn’t output any geometry, it just previews colors on the viewport. I need something that will color geometry and then output that geometry with the color, similar to ‘mesh colors’, however with meshes they slow down the compute process because large curves have lots of vertices and faces so I want to try surfaces which will be less of a load.

Thanks

@stevebaer
@AndyPayne
@fraguada

Any advice on this matter would be greatly appreciated.

You could use a headless doc?

How would I go about this? @fraguada

in csharp it would be:

var doc = Rhino.RhinoDoc.CreateHeadless(null);

https://developer.rhino3d.com/api/rhinocommon/rhino.rhinodoc/createheadless

I have been using Pythons rhino3dm library for sending to compute and creating the polygons of the shapes. Is there any way I can replicate a headerless doc in a Python env? @fraguada

you can create a headless doc in a python script node, add your surfaces, color them, etc.

Not sure I really understand what you mean, a ‘python script node’ as in running a python script in csharp?

You are running python scripts within grasshopper correct? You can create a headless doc within the python script component.

I am running python scripts on vscode using streamlit as a host. I am using the rhino3dm library to generate curves and then sending those curves to gh scripts using compute to colorize etc.

For colorising it I am running trying to run it via a python component on a gh file unless there is a better way.

That is a fine way of doing it.

You need to do the equivalent of this in your python script component inside the gh definition. I am not a python expert, hence why I post a csharp snippet. But the same should work inside the python script component.

The “headless doc” is the doc you can use to add your surfaces with object attributes in a headless environment like compute. Does that make sense?

I think I understand, however, there is no api references on creating headless docs in Python, I just tried doing it randomly and it didn’t work either so I’m not sure what the solution is.