Change Object U V position by python

Change Object U V position by python

gsingh51_mca19
Contributor Contributor
1,348 Views
2 Replies
Message 1 of 3

Change Object U V position by python

gsingh51_mca19
Contributor
Contributor

Like I want to create GUI in which if I click on the button object comes to U = 0.5, V = 0.5, is there any script for that my object will come between 0 and 1 tile and works using absolute position.

0 Likes
Accepted solutions (1)
1,349 Views
2 Replies
Replies (2)
Message 2 of 3

Kahylan
Advisor
Advisor
Accepted solution

I wrote a Python version of what you want, if I understood you correctly. It's not perfect because it only works with a single object, but maybe you can build on it.

 

import maya.cmds as mc

def moveUVtoPosition(u = None, v = None):
    """Moves selected objects UV's to specified coordinates in the UV grid,
    takes attributes u (float) for u coordinate, v (float) for v coordinate"""
    
    #getting selection
    obj = mc.ls(selection = True)[0]
    if '.' in obj:      
        obj = obj.split('.')[0]
    
    shape = mc.listRelatives(obj, s = True)
    #get total number of UV's
    numUV = mc.polyEvaluate(obj, uv = True)
    #selecting UV's to assure UVPivot position
    mc.select('{0}.map[0:{1}]'.format(obj, numUV))
    #getting current uvPivot position
    uvp = mc.getAttr('{0}.uvPivot'.format(shape[0]))
    #calculating distance that needs to be moved
    uNew = uvp[0][0]-u
    vNew = uvp[0][1]-v
    #moving UV's and uvPivot
    mc.polyEditUV('{0}.map[0:{1}]'.format(obj, numUV),relative = True, u = uNew*(-1), v = vNew*(-1))
    mc.setAttr('{0}.uvPivot'.format(shape[0]), u, v)
    
    mc.select(obj)

moveUVtoPosition(u = 0.5, v = 0.5)

 

I hope this helps!

0 Likes
Message 3 of 3

gsingh51_mca19
Contributor
Contributor

Thanks, sir for your guidance @Kahylan 

0 Likes