selecting vertex based off its color

46 views
Skip to first unread message

James Kim

unread,
Dec 19, 2018, 2:36:25 PM12/19/18
to Python Programming for Autodesk Maya
Hi there,
I'm trying to figure out how to select a vertex based off its color value.
so far this is what i have but 

vNum = cmds.polyEvaluate(v=True)
vertexList = cmds.ls('pCube1.vtx[:]',flatten=True)


for i in vertexList:
if cmds.polyColorPerVertex(r=0.5):
cmds.select(i)

any suggestions? Thank you

Michael Boon

unread,
Dec 19, 2018, 5:05:37 PM12/19/18
to Python Programming for Autodesk Maya
polyColorPerVertex returns a value, so you need to compare that to 0.5.
You'll want something like:
for v in vertexList:
   
if cmds.polyColorPerVertex(v, q=True, r=True) == [0.5]:
        cmds
.select(v, add=True)

Also, polyColorPerVertex gives a RuntimeError sometimes, I think when the vertex has no color, so you'll want to handle that too.

James Kim

unread,
Dec 19, 2018, 5:49:42 PM12/19/18
to Python Programming for Autodesk Maya
Thank you so much! It worked!

James Kim

unread,
Dec 23, 2018, 4:35:24 AM12/23/18
to Python Programming for Autodesk Maya
So i've come across an interesting problem. 
it seems maya can only select vertexes with color index 1.0 or 0.0. i cannot seem to get the code to select from any ranges inbetween. I've tried the other flags on the polyColorPerVertex and nothing seems to work.

as an example

import maya.cmds as cmds


sel = cmds.ls(sl=True)[0]
vNum = cmds.polyEvaluate(v=True)
vList = cmds.ls('pPlane1.vtx[:]', flatten = True)

for i in vList:
if cmds.polyColorPerVertex(i, q=True, r=True)==[1.0]:
cmds.select(i,add=True)

will select all the verts with color info 1.0

but

import maya.cmds as cmds


sel = cmds.ls(sl=True)[0]
vNum = cmds.polyEvaluate(v=True)
vList = cmds.ls('pPlane1.vtx[:]', flatten = True)

for i in vList:
if cmds.polyColorPerVertex(i, q=True, r=True)==[.456]:
cmds.select(i,add=True)

will give me nothing. 

Thank you

Michael and Amanda

unread,
Dec 23, 2018, 5:49:12 AM12/23/18
to python_in...@googlegroups.com
I'm off work for a couple of weeks so can't verify anything, but you can do some checks to find out the cause.

Each time through your loop, instead of using polyColorPerVertex in the if statement, assign its result to a variable, then print it out, then use it in the if statement. Something like:
col = cmds.polyColorPerVertex(i, q=True, r=True)[0]
print col, col == 0.456
if col == 0.456:
    etc

That will tell you if you're actually getting the colours you expect, and also tell you if Python thinks the value is close enough to 0.456 that it considers them equal. It may be that you need to check if col > 0.456-0.001 and col < 0.456+0.001 because it might be really close but not quite equal.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/cv7E_-VVtdQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/9f8fd2fd-ffd9-4112-8a79-42ec1ce9399f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Kim

unread,
Dec 23, 2018, 5:53:57 AM12/23/18
to Python Programming for Autodesk Maya
Thats what i thought so i used the actual query value before but it still wouldnt work. I'll try your method as well and see the results. 

Marcus Ottosson

unread,
Dec 23, 2018, 6:22:02 AM12/23/18
to python_in...@googlegroups.com
if cmds.polyColorPerVertex(i, q=True, r=True)==[.456]:

I suspect a precision problem, where e.g. [0.4560001] == [0.4560002] would return False. Try clamping it before comparing.

if map(round, cmds.polyColorPerVertex(i, q=True, r=True), [2]) == map(round, [0.456], [2]):

This’d compare the two values using two decimal places. round is a native Python operator, that takes two values, round(0.123, 2) and returns the first at the second number of decimal places, in this case 0.12. You could also int(value * 100) as an alternative.


On Sun, 23 Dec 2018 at 10:53, James Kim <james...@gmail.com> wrote:
Thats what i thought so i used the actual query value before but it still wouldnt work. I'll try your method as well and see the results. 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/77a1c17b-26ca-439e-b3af-fdc2cb79d21d%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages