Set object color

I am trying to set the color of a cylinder I created using python. Cylinder is created, but the color does not change.

Here is the script. Any help is appreciated.


import Rhino
import utility as rhutil
import scriptcontext
import System.Guid
import re
import math
import time
import sys

import System.Windows.Forms.DialogResult
import System.IO.File

import object as rhobject

from scriptcontext import doc

import Rhino.UI
import rhinoscriptsyntax
import System.Drawing.Color
import System.Enum
import System.Array
import System.Windows.Forms
from view import __viewhelper

import os

def AddCylinder(base, height, radius, cap=True):
    """
    Adds a cylinder-shaped polysurface to the document
    Parameters:
      base = The 3D base point of the cylinder or the base plane of the cylinder
      height = if base is a point, then height is a 3D height point of the cylinder. The height
        point defines the height and direction of the cylinder. If base is a plane, then height
        is the numeric height value of the cylinder
      radius = radius of the cylinder
      cap[opt] = cap the cylinder
    Returns:
      identifier of new object if successful
      None on error
    """
    cylinder=None
    height_point = rhutil.coerce3dpoint(height)
    if( height_point!=None ):
        #base must be a point
        base = rhutil.coerce3dpoint(base)
        if( base!=None ):
            normal = height_point-base
            plane = Rhino.Geometry.Plane(base, normal)
            height = normal.Length
            circle = Rhino.Geometry.Circle(plane, radius)
            cylinder = Rhino.Geometry.Cylinder(circle, height)
    else:
        #base must be a plane
        base = rhutil.coerceplane(base)
        if( base!=None ):
            circle = Rhino.Geometry.Circle(base, radius)
            cylinder = Rhino.Geometry.Cylinder(circle, height)
    if( cylinder==None ): return scriptcontext.errorhandler()
    brep = cylinder.ToBrep(cap, cap)
    id = scriptcontext.doc.Objects.AddBrep(brep)
    if( id==System.Guid.Empty ):
        return scriptcontext.errorhandler()
    return id
    """
    scriptcontext.doc.Views.Redraw()
    """

x = 20.
y = 30.
thickness = .5
radius = .5
base = Rhino.Geometry.Point3d(x, y, thickness)
top = Rhino.Geometry.Point3d(x, y, thickness+.1)
scyl = AddCylinder(base, top, radius)

rhinoscriptsyntax.ObjectColor(scyl,(25,50,255))

@elaner,

try this:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
    
def DoSomething():
    x = 20.0
    y = 30.0
    thickness = 0.5
    radius = 0.5
    base = Rhino.Geometry.Point3d(x, y, thickness)
    top = Rhino.Geometry.Point3d(x, y, thickness+.1)
    scyl = rs.AddCylinder(base, top, radius)
    rs.ObjectColor(scyl,(255,0,0))
    
if __name__=="__main__":
    DoSomething()

c.

1 Like

Thanks, Clement. A bit more info I just discovered: The color does change in wireframe view but not in rendered view. Do I need to use texture mapping to see color in a rendered view? Does python scripting support texture mapping?

Yes, python supports texture mapping. But to see the color in RenderedViewport you might create a new material and assign it a material color. The method:

rs.ObjectColor(scyl,(255,0,0)) 

does change the object color. Below is an example doing the same with a material color:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
    
def DoSomething():
    x = 20.0
    y = 30.0
    thickness = 0.5
    radius = 0.5
    base = Rhino.Geometry.Point3d(x, y, thickness)
    top = Rhino.Geometry.Point3d(x, y, thickness+.1)
    scyl = rs.AddCylinder(base, top, radius)
    
    # add new material and get its index
    material_index = rs.AddMaterialToObject(scyl)
    
    # assign material color
    rs.MaterialColor(material_index, color=(255,0,0))
    
if __name__=="__main__":
    DoSomething()

c.

2 Likes

That works! Thank you, clement.