Assign vertex color based on its position

1,159 views
Skip to first unread message

Aren Voorhees

unread,
May 1, 2017, 3:10:00 PM5/1/17
to Python Programming for Autodesk Maya
I have an old script that assigns vertex colors to a mesh, based on the vert's position (can be used to create worldspace gradients - useful for world position offset vertex animation in real-time applications).  Anyway, I'm working on re-writing this tool using the python API to hopefully make it quicker on dense meshes.  This is my first time really trying to use the api, so I'm kind of stumbling my way through it.  I've found a way to get the point positions and a list of vertex IDs on a given mesh.  However, those two lists don't correspond to each other so when I run the below code, it doesn't assign the right vert colors to the correct mesh.  Is there some way I can make the two lists, or two sets of information match together to fix this issue? (Note: Currently I'm just trying to create a Y gradient which is why I am just getting 2nd value for each point).


Andres Weber

unread,
May 1, 2017, 4:24:47 PM5/1/17
to Python Programming for Autodesk Maya
The problme is that you sorted the points list (and got it out of order from the original vertex list) and then used that to as the basis for building your normalized list of values.  So, ignoring optimization or anything, just put the comprehension on the for loop at line 19: for val in [p[1] for p in pointsList]
instead of reusing the pointsDirList that you used to determine the min/max.  That should get it back to working.

Aren Voorhees

unread,
May 1, 2017, 5:31:16 PM5/1/17
to Python Programming for Autodesk Maya
Ah, got it.  I didn't think about the fact that I was rearranging the order of the point list, making it not correspond to the vertex list.  To get it working, my only change was to take out the sorted() on line 14.  

Getting it working brought another issue to light though...it works really slowly.  In a test just now, it took nearly a minute to run on a 4k vert mesh, which is on the much lower end of mesh size I'd expect this to be used on.  The slowdown seems to be happening completely in the last for loop starting on line 24.  Any ideas on how I could optimize this?   

Justin Israel

unread,
May 1, 2017, 5:52:46 PM5/1/17
to python_in...@googlegroups.com
On Tue, May 2, 2017 at 9:31 AM Aren Voorhees <are...@gmail.com> wrote:
Ah, got it.  I didn't think about the fact that I was rearranging the order of the point list, making it not correspond to the vertex list.  To get it working, my only change was to take out the sorted() on line 14.  

Getting it working brought another issue to light though...it works really slowly.  In a test just now, it took nearly a minute to run on a 4k vert mesh, which is on the much lower end of mesh size I'd expect this to be used on.  The slowdown seems to be happening completely in the last for loop starting on line 24.  Any ideas on how I could optimize this?

If you look at the API docs for setVertexColor, it has this note:

NOTE: To change the colors of many vertices, it is more efficient to use the batch updating method setVertexColors() instead.

So you could build the color and vertex arrays up front, and then make one call to set the colors.

  


On Monday, May 1, 2017 at 3:24:47 PM UTC-5, Andres Weber wrote:
The problme is that you sorted the points list (and got it out of order from the original vertex list) and then used that to as the basis for building your normalized list of values.  So, ignoring optimization or anything, just put the comprehension on the for loop at line 19: for val in [p[1] for p in pointsList]
instead of reusing the pointsDirList that you used to determine the min/max.  That should get it back to working.

On Monday, May 1, 2017 at 3:10:00 PM UTC-4, Aren Voorhees wrote:
I have an old script that assigns vertex colors to a mesh, based on the vert's position (can be used to create worldspace gradients - useful for world position offset vertex animation in real-time applications).  Anyway, I'm working on re-writing this tool using the python API to hopefully make it quicker on dense meshes.  This is my first time really trying to use the api, so I'm kind of stumbling my way through it.  I've found a way to get the point positions and a list of vertex IDs on a given mesh.  However, those two lists don't correspond to each other so when I run the below code, it doesn't assign the right vert colors to the correct mesh.  Is there some way I can make the two lists, or two sets of information match together to fix this issue? (Note: Currently I'm just trying to create a Y gradient which is why I am just getting 2nd value for each point).


--
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/16ecb20f-46e5-4ab2-b467-72c34f784e9d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andres Weber

unread,
May 2, 2017, 3:50:15 PM5/2/17
to Python Programming for Autodesk Maya
Also I haven't done ANY timing to see if this actually helps, but you're casting a sublist to a set and back to a list...you could get a super minor speed up by just doing xrange(selMesh.numVertices()) to get the full vertex list.  You're also wasting time with the enumerate and index lookup on the second same size list.  You could just zip the two generators and normalize on demand by using a generator comprehension for pointsDirList and that might speed things up a little.  I'm sure these are extremely minimal if at all but just some ideas.  Justin's is going to be a huge speed increase I'm sure.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Aren Voorhees

unread,
May 3, 2017, 10:06:08 AM5/3/17
to Python Programming for Autodesk Maya
Thanks to you both for the tips - this is what I came up with:


Now it takes .3 seconds on a 75k vert mesh - pretty good speed increase I'd say!  

Justin Israel

unread,
May 3, 2017, 4:08:02 PM5/3/17
to Python Programming for Autodesk Maya


On Thu, May 4, 2017, 2:06 AM Aren Voorhees <are...@gmail.com> wrote:
Thanks to you both for the tips - this is what I came up with:


Now it takes .3 seconds on a 75k vert mesh - pretty good speed increase I'd say!  

Nice job. 
Does it apply the gradient the way you wanted, considering it is still using a sorted vert list and a color list that is in the order of the unsorted points? 



On Tuesday, May 2, 2017 at 2:50:15 PM UTC-5, Andres Weber wrote:
Also I haven't done ANY timing to see if this actually helps, but you're casting a sublist to a set and back to a list...you could get a super minor speed up by just doing xrange(selMesh.numVertices()) to get the full vertex list.  You're also wasting time with the enumerate and index lookup on the second same size list.  You could just zip the two generators and normalize on demand by using a generator comprehension for pointsDirList and that might speed things up a little.  I'm sure these are extremely minimal if at all but just some ideas.  Justin's is going to be a huge speed increase I'm sure.


On Monday, May 1, 2017 at 5:52:46 PM UTC-4, Justin Israel wrote:
On Tue, May 2, 2017 at 9:31 AM Aren Voorhees <are...@gmail.com> wrote:
Ah, got it.  I didn't think about the fact that I was rearranging the order of the point list, making it not correspond to the vertex list.  To get it working, my only change was to take out the sorted() on line 14.  

Getting it working brought another issue to light though...it works really slowly.  In a test just now, it took nearly a minute to run on a 4k vert mesh, which is on the much lower end of mesh size I'd expect this to be used on.  The slowdown seems to be happening completely in the last for loop starting on line 24.  Any ideas on how I could optimize this?

If you look at the API docs for setVertexColor, it has this note:

NOTE: To change the colors of many vertices, it is more efficient to use the batch updating method setVertexColors() instead.

So you could build the color and vertex arrays up front, and then make one call to set the colors.

  


On Monday, May 1, 2017 at 3:24:47 PM UTC-5, Andres Weber wrote:
The problme is that you sorted the points list (and got it out of order from the original vertex list) and then used that to as the basis for building your normalized list of values.  So, ignoring optimization or anything, just put the comprehension on the for loop at line 19: for val in [p[1] for p in pointsList]
instead of reusing the pointsDirList that you used to determine the min/max.  That should get it back to working.

On Monday, May 1, 2017 at 3:10:00 PM UTC-4, Aren Voorhees wrote:
I have an old script that assigns vertex colors to a mesh, based on the vert's position (can be used to create worldspace gradients - useful for world position offset vertex animation in real-time applications).  Anyway, I'm working on re-writing this tool using the python API to hopefully make it quicker on dense meshes.  This is my first time really trying to use the api, so I'm kind of stumbling my way through it.  I've found a way to get the point positions and a list of vertex IDs on a given mesh.  However, those two lists don't correspond to each other so when I run the below code, it doesn't assign the right vert colors to the correct mesh.  Is there some way I can make the two lists, or two sets of information match together to fix this issue? (Note: Currently I'm just trying to create a Y gradient which is why I am just getting 2nd value for each point).


--
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.

--
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/560048e2-02cb-4b90-a1ed-9ceca09b4a50%40googlegroups.com.

Aren Voorhees

unread,
May 8, 2017, 9:41:38 PM5/8/17
to Python Programming for Autodesk Maya
Hey Justin - it is applying the gradient correctly.  It seems like the vert id list and the point list, do correspond to each other although I'm not sure why (don't have Maya in front of me atm so I can't take a closer look).  Apparently when considering vert ids and points, Maya does it in the same order?


On Wednesday, May 3, 2017 at 3:08:02 PM UTC-5, Justin Israel wrote:


On Thu, May 4, 2017, 2:06 AM Aren Voorhees <are...@gmail.com> wrote:
Thanks to you both for the tips - this is what I came up with:


Now it takes .3 seconds on a 75k vert mesh - pretty good speed increase I'd say!  

Nice job. 
Does it apply the gradient the way you wanted, considering it is still using a sorted vert list and a color list that is in the order of the unsorted points? 



On Tuesday, May 2, 2017 at 2:50:15 PM UTC-5, Andres Weber wrote:
Also I haven't done ANY timing to see if this actually helps, but you're casting a sublist to a set and back to a list...you could get a super minor speed up by just doing xrange(selMesh.numVertices()) to get the full vertex list.  You're also wasting time with the enumerate and index lookup on the second same size list.  You could just zip the two generators and normalize on demand by using a generator comprehension for pointsDirList and that might speed things up a little.  I'm sure these are extremely minimal if at all but just some ideas.  Justin's is going to be a huge speed increase I'm sure.


On Monday, May 1, 2017 at 5:52:46 PM UTC-4, Justin Israel wrote:
On Tue, May 2, 2017 at 9:31 AM Aren Voorhees <are...@gmail.com> wrote:
Ah, got it.  I didn't think about the fact that I was rearranging the order of the point list, making it not correspond to the vertex list.  To get it working, my only change was to take out the sorted() on line 14.  

Getting it working brought another issue to light though...it works really slowly.  In a test just now, it took nearly a minute to run on a 4k vert mesh, which is on the much lower end of mesh size I'd expect this to be used on.  The slowdown seems to be happening completely in the last for loop starting on line 24.  Any ideas on how I could optimize this?

If you look at the API docs for setVertexColor, it has this note:

NOTE: To change the colors of many vertices, it is more efficient to use the batch updating method setVertexColors() instead.

So you could build the color and vertex arrays up front, and then make one call to set the colors.

  


On Monday, May 1, 2017 at 3:24:47 PM UTC-5, Andres Weber wrote:
The problme is that you sorted the points list (and got it out of order from the original vertex list) and then used that to as the basis for building your normalized list of values.  So, ignoring optimization or anything, just put the comprehension on the for loop at line 19: for val in [p[1] for p in pointsList]
instead of reusing the pointsDirList that you used to determine the min/max.  That should get it back to working.

On Monday, May 1, 2017 at 3:10:00 PM UTC-4, Aren Voorhees wrote:
I have an old script that assigns vertex colors to a mesh, based on the vert's position (can be used to create worldspace gradients - useful for world position offset vertex animation in real-time applications).  Anyway, I'm working on re-writing this tool using the python API to hopefully make it quicker on dense meshes.  This is my first time really trying to use the api, so I'm kind of stumbling my way through it.  I've found a way to get the point positions and a list of vertex IDs on a given mesh.  However, those two lists don't correspond to each other so when I run the below code, it doesn't assign the right vert colors to the correct mesh.  Is there some way I can make the two lists, or two sets of information match together to fix this issue? (Note: Currently I'm just trying to create a Y gradient which is why I am just getting 2nd value for each point).


--
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_maya+unsub...@googlegroups.com.

--
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_maya+unsub...@googlegroups.com.

Justin Israel

unread,
May 9, 2017, 1:18:11 AM5/9/17
to python_in...@googlegroups.com
On Tue, May 9, 2017 at 1:41 PM Aren Voorhees <are...@gmail.com> wrote:
Hey Justin - it is applying the gradient correctly.  It seems like the vert id list and the point list, do correspond to each other although I'm not sure why (don't have Maya in front of me atm so I can't take a closer look).

If it works as you expect, then can't really argue with that :)
 
 Apparently when considering vert ids and points, Maya does it in the same order?

Well even if that is true, it wouldn't make sense based on your code. You sort the vert list but not the point list. And somehow that works out.
 

On Wednesday, May 3, 2017 at 3:08:02 PM UTC-5, Justin Israel wrote:


On Thu, May 4, 2017, 2:06 AM Aren Voorhees <are...@gmail.com> wrote:
Thanks to you both for the tips - this is what I came up with:


Now it takes .3 seconds on a 75k vert mesh - pretty good speed increase I'd say!  

Nice job. 
Does it apply the gradient the way you wanted, considering it is still using a sorted vert list and a color list that is in the order of the unsorted points? 



On Tuesday, May 2, 2017 at 2:50:15 PM UTC-5, Andres Weber wrote:
Also I haven't done ANY timing to see if this actually helps, but you're casting a sublist to a set and back to a list...you could get a super minor speed up by just doing xrange(selMesh.numVertices()) to get the full vertex list.  You're also wasting time with the enumerate and index lookup on the second same size list.  You could just zip the two generators and normalize on demand by using a generator comprehension for pointsDirList and that might speed things up a little.  I'm sure these are extremely minimal if at all but just some ideas.  Justin's is going to be a huge speed increase I'm sure.


On Monday, May 1, 2017 at 5:52:46 PM UTC-4, Justin Israel wrote:
On Tue, May 2, 2017 at 9:31 AM Aren Voorhees <are...@gmail.com> wrote:
Ah, got it.  I didn't think about the fact that I was rearranging the order of the point list, making it not correspond to the vertex list.  To get it working, my only change was to take out the sorted() on line 14.  

Getting it working brought another issue to light though...it works really slowly.  In a test just now, it took nearly a minute to run on a 4k vert mesh, which is on the much lower end of mesh size I'd expect this to be used on.  The slowdown seems to be happening completely in the last for loop starting on line 24.  Any ideas on how I could optimize this?

If you look at the API docs for setVertexColor, it has this note:

NOTE: To change the colors of many vertices, it is more efficient to use the batch updating method setVertexColors() instead.

So you could build the color and vertex arrays up front, and then make one call to set the colors.

  


On Monday, May 1, 2017 at 3:24:47 PM UTC-5, Andres Weber wrote:
The problme is that you sorted the points list (and got it out of order from the original vertex list) and then used that to as the basis for building your normalized list of values.  So, ignoring optimization or anything, just put the comprehension on the for loop at line 19: for val in [p[1] for p in pointsList]
instead of reusing the pointsDirList that you used to determine the min/max.  That should get it back to working.

On Monday, May 1, 2017 at 3:10:00 PM UTC-4, Aren Voorhees wrote:
I have an old script that assigns vertex colors to a mesh, based on the vert's position (can be used to create worldspace gradients - useful for world position offset vertex animation in real-time applications).  Anyway, I'm working on re-writing this tool using the python API to hopefully make it quicker on dense meshes.  This is my first time really trying to use the api, so I'm kind of stumbling my way through it.  I've found a way to get the point positions and a list of vertex IDs on a given mesh.  However, those two lists don't correspond to each other so when I run the below code, it doesn't assign the right vert colors to the correct mesh.  Is there some way I can make the two lists, or two sets of information match together to fix this issue? (Note: Currently I'm just trying to create a Y gradient which is why I am just getting 2nd value for each point).


--
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.

--
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.

--
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/c1ccde65-a80e-4a77-a109-4c98db8ea650%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages