C# 按偏移/矢量移动每个UV顶点
我想创建第二个UV集,然后通过向量C# 按偏移/矢量移动每个UV顶点,c#,python,maya,mel,C#,Python,Maya,Mel,我想创建第二个UV集,然后通过向量u=0,v=1.0/顶点数移动对象中的每个UV顶点。为4个顶点平面创建的新UV顶点坐标应如下所示:对于顶点0(u=0,v=0),对于顶点1(u=0,v=0.25),对于顶点2(u=0,v=0.5),对于顶点3(u=0,v=0.75),等等 我有一个C#的源代码: Vector2[]UV2=新的Vector2[m.vertexCount]; 浮动半像素尺寸=(1f/(浮动)m.vertexCount)/2f; 对于(int i=0;i
u=0,v=1.0/顶点数
移动对象中的每个UV顶点。为4个顶点平面创建的新UV顶点坐标应如下所示:对于顶点0(u=0,v=0)
,对于顶点1(u=0,v=0.25)
,对于顶点2(u=0,v=0.5)
,对于顶点3(u=0,v=0.75)
,等等
我有一个C#的源代码:
Vector2[]UV2=新的Vector2[m.vertexCount];
浮动半像素尺寸=(1f/(浮动)m.vertexCount)/2f;
对于(int i=0;i
就我的研究而言,Python中没有向量,现在我只能使用这个解决方案。到目前为止,我得出了以下结论:
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)
将maya.cmds作为cmds导入
cmds.polyUVSet(create=True,uvSet='map2')
vertexCount=cmds.polyEvaluate(v=True)
vertexCount_float=浮动(vertexCount)
HalfTextureSize=(1.0/vertexCount/2.0)
x=1.0/vertexCount
sel=cmds.ls(sl=1,fl=1)
对于sel中的i:
i=0,i默认maya pyhon中没有向量
如果您想要一个普通的python,唯一的解决方案就是一个单文件向量库
也可以使用API直接从maya获取3矢量:
from maya.api.OpenMaya import MVector
v1 = MVector(1,2,3)
v2 = MVector(3,4,5)
print v1 + v2
这些是3d向量,但您可以忽略3d组件
对于实际创建和编辑UV集,需要考虑几个复杂问题
只需创建一个新的UV集,就会得到一个空集;该集存在,但不包含任何UV。顶点和UV之间的关系并不总是1:1,一个物理顶点可以有多个UV,并且可以有未映射的顶点。要创建逐顶点1-uv贴图,最简单的方法是只应用简单的平面或球体贴图
您需要设置“当前uv集”,以便maya知道要使用哪个集。不幸的是,当您向maya请求UV集名称时,它会提供unicode,但当您设置它们时,它需要字符串(至少在2016年-我认为这是一个bug)
如果您当前的代码是一个指示,那么您实际上不需要向量来设置它
最终的解决方案如下所示:
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)
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)