2

I want to create second UV set, and then move each UV vertex in an object by vector u=0, v=1.0/number of vertices. The new UV vertex coordinates created for 4 vertices plane should go like this: for vertex 0 (u=0,v=0), for vertex 1 (u=0,v=0.25), for vertex 2 (u=0,v=0.5), for vertex 3 (u=0,v=0.75), etc.

I have a source code in C# :

Vector2[] UV2 = new Vector2[m.vertexCount];

    float HalfTexelSize = (1f/ (float)m.vertexCount)/2f;

    for (int i = 0; i < m.vertexCount; i++) {

        UV2[i] = new Vector2(0f, (float)i / (float)m.vertexCount) + new Vector2(0f, HalfTexelSize);
    }

    m.uv2 = UV2;
    meshFilter.mesh = m;

As far as my research goes there is no vectors in Python, and now I'm stuck with the solution. So far I came up this:

import maya.cmds as cmds

cmds.polyUVSet(create=True, uvSet='map2')
vertexCount = cmds.polyEvaluate(v=True)
vertexCount_float = float(vertexCount)
HalfTextureSize = (1.0/vertexCount/2.0)
x = 1.0/vertexCount
sel = cmds.ls(sl=1, fl=1)

for i in sel:
    i=0, i<sel
    cmds.polyEditUV(uValue=0.0, vValue=x)

But the output I get is the second UV set with every vertex in (0,0) UV coordinates. Can anyone help me? Any MEL/Python solution would be appreciated.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Sup
  • 29
  • 1

1 Answers1

0

There's no vectors in default maya pyhon.

If you want a vanilla python-only solution this project is a single-file vector library.

You can also get 3-vectors from maya directly using the API:

from maya.api.OpenMaya import MVector

v1 = MVector(1,2,3)
v2 = MVector(3,4,5)
print v1 + v2

These are 3d vectors but you can just ignore the 3d component

For actually creating and editing the UV set, there are a couple of complexities to consider

  1. Just creating a new UV set gives you an empty set; the set exists but it won't contain any UVs. The relationship between verts and UVs is not always 1:1, you can have more than one UVs for a physical vert and you can have un-mapped vertices. To create a 1-uv-per-vertex mapping it's easiest to just apply a simple planar or sphere map.
  2. You'll need to set the 'current uv set' so maya knows which set you want to work on. Unfortunately when you ask maya for UV set names, it gives you unicode, but when you set them it expects strings (at least in 2016 - I think this is a bug)
  3. You don't really need a vector to set this if your current code is an indication.

The final solution will look something like this:

import maya.cmds as cmds

# work on only one selected object at a time 
selected_object = cmds.ls(sl=1, fl=1)[0]  
vertexCount = cmds.polyEvaluate(selected_object, v=True)

# add a projection so you have 1-1 vert:uv mapping
cmds.polyPlanarProjection(selected_object, cm=True)
# get the name of the current uv set
latest_uvs = cmds.polyUVSet(selected_object, q=True, auv=True)
# it's a unioode by default, has to be a string
new_uv = str(latest_uvs[-1]) 
# set the current uv set to the newly created one
cmds.polyUVSet(selected_object, e=True, cuv = True, uvs=new_uv)
vert_interval = 1.0 / vertexCount

for vert in range (vertexCount):
    uv_vert = "{}.map[{}]".format (selected_object, vert)
    cmds.polyEditUV( uv_vert, u= 0, v = vert * vert_interval, relative=False,  uvs=new_uv)
theodox
  • 12,028
  • 3
  • 23
  • 36