Grasshopper

algorithmic modeling for Rhino

Hi everyone!

I have imported a vrml file from maya into rhino 5 (I had to pass through rhino 4 to keep the vertexes color, don't know why btw..)

I would like to read the value of the color for each face of the mesh, but apparentely rhinoscriptsyntax.MeshVertexColors doesn't give me an output I can read and use , and same goes when I try to use rhinoscriptsyntax.ColorHLSToRGB command

look:

import rhinoscriptsyntax as rs

rs.Command("_selmesh")

rs.Command("unweld 0")

rs.UnselectAllObjects


rs.AddLayer("MainMesh")
#'pick Mesh which is unwelded at 0
strObject = rs.GetObject("Select mesh", 32)
#'store mesh in base layer
rs.ObjectLayer(strObject,"MainMesh")
#' get the face vertices of the mesh
arrFaces = rs.MeshFaces(strObject, True)
#'get the vertex colors of the mesh
color = rs.MeshVertexColors(strObject)

i = 0

arrFace = []
arrHLS = []
arrFaceVertices2 = []


while i <= len(arrFaces)-1:
''''''arrFace.append(arrFaces[i])
''''''arrFace.append(arrFaces[i+1])
''''''arrFace.append(arrFaces[i+2])
''''''arrFace.append(arrFaces[i+3])
''''''arrHLS.append(rs.ColorRGBToHLS(color[i]))

''''''print(color[i])
''''''print arrHLS

'''''' i = i + 4

''''''arrFace = []
''''''arrHLS = []
''''''arrFaceVertices2 = []

############

The result I get is :

Color [A=255, R=55, G=55, B=55]
[<Rhino.Display.ColorHSL object at 0x00000000000001F7 [Rhino.Display.ColorHSL]>]
Color [A=255, R=55, G=55, B=55]
[<Rhino.Display.ColorHSL object at 0x00000000000001F8 [Rhino.Display.ColorHSL]>]
Color [A=255, R=55, G=55, B=55]
[<Rhino.Display.ColorHSL object at 0x00000000000001F9 [Rhino.Display.ColorHSL]>]
Color [A=255, R=59, G=59, B=59]
[<Rhino.Display.ColorHSL object at 0x00000000000001FA [Rhino.Display.ColorHSL]>]
Color [A=255, R=55, G=55, B=55]
[<Rhino.Display.ColorHSL object at 0x00000000000001FB [Rhino.Display.ColorHSL]>]

But if I try to get color[i][0] I get an error, how can I access to the numbers RGB or the HLS one as numbers?

Thanks a lot!

V.

Views: 3877

Replies to This Discussion

Hi Vince!!!

Color variable type is an hexadecimal number wich represents all 3 RGB components. If you use rhinoscriptsynthax you should extract them separately with rs.ColorRedValue(color), rs.ColorBlueValue(color), rs.ColorGreenValue(color). For example, here's a script that writes the Red component value on each of the mesh's points:

import rhinoscriptsyntax as rs

myMesh = rs.GetObject("select a mesh", rs.filter.mesh)
vCol = rs.MeshVertexColors(myMesh)
vPt = rs.MeshVertices(myMesh)

count = 0

for pt in vPt:
    co = vCol[count]
    rs.AddText(str(rs.ColorRedValue(co)),pt)
    count+= 1

try it out and let me know if this works. Oh, and... are you still in London?

Hi Alessio! Thanks for your answer, I am getting something better but still I don't understand how the data are stored inside MeshVertexColors (why when I print them I get not a list but the "Rhino.Display.ColorHSL object" thing) and while I can extract red green and blue I can't do it for hue luminance and saturation (there is no rs.ColorHueValue for example, while when I was using rhinoscript the hue was the first element of the list of rs.ColorRGBToHLS)

Here:


color = rs.MeshVertexColors(strObject)

while i <= len(arrFaces)-1:

      

      print color[i]

      red = rs.ColorRedValue(color[i])
      blue = rs.ColorBlueValue(color[i])
      green = rs.ColorGreenValue(color[i])

      print "red is", red
      print "blue is", blue
      print "green is", green

      hlsvalue = rs.ColorRGBToHLS([red,blue,green])

      print "hls is equal to " , hlsvalue


################


And what I get printed is


Color [A=255, R=55, G=55, B=55]
red is 55
blue is 55
green is 55

hls is equal to Rhino.Display.ColorHSL


So is there any way I can use the data stored inside hls variable???


Thanks again

V.


p.s. I'll be back in London on Thursday

Actually I overcome that problem using python conversion hls to rgb, but still there is some kind of problem that doesn't allow me to get the proper results.

The first cube was completely painted black, the second has just that white spot. But the vertex color is not assigned correctly (is there a point where I mess with the order?)

Any suggestions?

The script is :

import rhinoscriptsyntax as rs
import colorsys


rs.Command("_selmesh")
rs.Command("unweld 0")
rs.UnselectAllObjects

strObject = rs.GetObject("Select mesh", 32)
arrFaces = rs.MeshFaces(strObject)
color = rs.MeshVertexColors(strObject)

rs.EnableRedraw (False)

i = 0
arrFace = []


while i <= len(arrFaces)-3:

arrFace.append(arrFaces[i])
arrFace.append(arrFaces[i+1])
arrFace.append(arrFaces[i+2])
arrFace.append(arrFaces[i+3])

print(color[i])

red = rs.ColorRedValue(color[i])
blue = rs.ColorBlueValue(color[i])
green = rs.ColorGreenValue(color[i])

realcolor = colorsys.rgb_to_hls(red, green, blue)

rs.AddText(str(realcolor[1]),arrFaces[i],5000)

i = i + 4
arrFace = []

Attachments:

Right now I cannot test your scripts but from what I read I have a question: why are you using mesh faces? Color is stored in vertices, and they are stored in a single dimension array. Faces then are a separate array of indices that point to the vertices array to say which is which in building the mesh faces.

To sample (and change) the mesh vertex colors you only need to sample its points, no need to use faces (or is there something I am missing in your steps?).

Regarding RGB to HLS right now I cannot make tests, maybe in the next days (or maybe someone else can help in the meantime).

See us in London then (I'll be there april 26-30, I'll message you in a while)!

Hi again

I am using mesh faces because I wanted to average the vertices color in order to assign a "color property" to every face to influence a further tesselation. Using meshfaces on the unwelded mesh allows me to have for each faces the exact four vertices belonging to it, and then averaging their values. Somehow it seems that in my script I am loosing the correct connection between vertices and faces, so there should be another way to do it.

Any suggestions?

Regarding RGB to HLS using the colorsys.rgb_to_hls works well, so I just gave up the rhinoscriptsintax command, which seems to have some problem (it doesn't work as the help file says).

(see you next week then!)

Ok, I solved this way if anybody is interested.

At first I used this set of commands:

strObject = rs.GetObject("Select mesh", 32)
arrFaces = rs.MeshFaces(strObject)
arrVertices = rs.MeshVertices(strObject)
arrColors = rs.MeshVertexColors(strObject)

than in a loop with the mesh corner points (arrpoints)

vertex = arrpoints[i]
indexColor = rs.PointArrayClosestPoint (arrVertices, vertex)
color = arrColors[indexColor]

So I could reference the correct color of every mesh corner

Than I just averaged them

cheers

Hi Vincenzo,

I've gotten your script to work when using your mesh model, but for some reason, I got an error when I build my mesh by Meshbox. The message is following:

Message: index out of range: 0

Traceback:  line 26, in <module>, "C:\Users\Documents\Processing\Python Colors\samplecolortest.py"

I am using Rhino 5. Any idea what's going wrong? 

Also by the way, could you upload your latest script to let me have a try? Thanks!

I figured that out it, because the new meshbox has no color information on its vertex so I need first assign some colors onto those points. However, I still not quite follow your script on "than in a loop with the mesh corner points (arrpoints)", is there any information I missed?

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service