Difference between revisions of "Snippet:TD-SDK Shift UVs"

From The Foundry MODO SDK wiki
Jump to: navigation, search
(Corrected example so that it handles continuous and discontinuous UVs)
Line 2: Line 2:
 
|summary=Example of using modo.Polygon.setUV /getUV from the TD-SDK
 
|summary=Example of using modo.Polygon.setUV /getUV from the TD-SDK
 
|code=<syntaxhighlight lang="python">
 
|code=<syntaxhighlight lang="python">
scene = modo.scene.current()
+
import modo
geo  = modo.Mesh().geometry # gets currently selected mesh
+
uvmap = geo.vmaps.uvMaps[0]
+
  
# Shift vertex UVs on all polygons
+
def shiftUVs (UVoffset, geometry, vertexIndex, UVMapName='Texture'):
def shiftUVs (vertex, UVMapName='Texture'):
+
    '''Shift vertex UVs on by a given offset'''
  
for polygon in vertex.polygons:
+
    if not geometry.vmaps[UVMapName]:
u,v = polygon.getUV(vertex, uvmap=UVMapName)
+
        raise LookupError
polygon.setUV ( (u+0.1, v), vertex, uvmap=UVMapName)
+
  
vertex = geo.vertices[0]
+
    uvmap  = geometry.vmaps[UVMapName][0]
shiftUVs (vertex)
+
    vertex = geometry.vertices[vertexIndex]
geo.setMeshEdits()
+
 
 +
    UV = uvmap[vertexIndex]
 +
    # If the UV tuple consists of two numbers, then it is a continuous (shared) UV
 +
    if all (isinstance(i, (float, int, long)) for i in UV):
 +
        u,v = UV
 +
        uvmap[vertexIndex] = (u+UVoffset[0], v+UVoffset[1])
 +
    # If it is discontinuous, iterate over all connected polygons
 +
    else:
 +
        for polygon in vertex.polygons:
 +
            u,v = polygon.getUV(vertex, uvmap=UVMapName)
 +
            polygon.setUV ( (u+UVoffset[0], v+UVoffset[1]), vertex, uvmap=UVMapName)
 +
 
 +
with modo.Mesh().geometry as geo:
 +
 
 +
    for i in xrange (len(geo.vertices)):
 +
        shiftUVs ((0.1, 0), geo, vertexIndex=i, UVMapName='Texture')
 +
 
 +
    geo.setMeshEdits()
 
</syntaxhighlight>
 
</syntaxhighlight>
 
|lang=Python,
 
|lang=Python,
 
}}
 
}}

Revision as of 03:20, 17 May 2016

Description:

Example of using modo.Polygon.setUV /getUV from the TD-SDK

Code:

import modo
 
def shiftUVs (UVoffset, geometry, vertexIndex, UVMapName='Texture'):
    '''Shift vertex UVs on by a given offset'''
 
    if not geometry.vmaps[UVMapName]:
        raise LookupError
 
    uvmap  = geometry.vmaps[UVMapName][0]
    vertex = geometry.vertices[vertexIndex]
 
    UV = uvmap[vertexIndex]
    # If the UV tuple consists of two numbers, then it is a continuous (shared) UV
    if all (isinstance(i, (float, int, long)) for i in UV):	
        u,v = UV
        uvmap[vertexIndex] = (u+UVoffset[0], v+UVoffset[1])
    # If it is discontinuous, iterate over all connected polygons
    else:
        for polygon in vertex.polygons:
            u,v = polygon.getUV(vertex, uvmap=UVMapName)
            polygon.setUV ( (u+UVoffset[0], v+UVoffset[1]), vertex, uvmap=UVMapName)
 
with modo.Mesh().geometry as geo:
 
    for i in xrange (len(geo.vertices)):
        shiftUVs ((0.1, 0), geo, vertexIndex=i, UVMapName='Texture')
 
    geo.setMeshEdits()