Python script doesn't work with Unfold command

Python script doesn't work with Unfold command

gsingh51_mca19
Contributor Contributor
984 Views
4 Replies
Message 1 of 5

Python script doesn't work with Unfold command

gsingh51_mca19
Contributor
Contributor

with the below code there is no error but it doesn't work, in this, I am creating GUI in which two inputs density and regulation when I click on apply button it doesn't work and it gives me no error if anyone has an idea to solve please guide or help me, Thanks.

 

Code-:

import maya.cmds as cmds

def applyScale(*args):
sel = cmds.ls(sl=True, flatten=True)
txDensity = cmds.floatField("density",q=True,value=0)
txSize = cmds.floatField("resolution",q=True,value=0)
scalar = txDensity/txSize

for i in sel:
cmds.unfold(i,i=0,ss=0.001,gb=0,gmb=1,pub=0,ps=0,oa=0,us=1,s=scalar)


''' UNIQUE WINDOW NAME '''
winId="layoutDesign"

''' CONDITION WHICH CHECK EXISTENCE OF WINDOW'''
if cmds.window(winId,exists=True):
cmds.deleteUI(winId)

''' CREATES NEW WINDOW WITH SPECIFIED WIDTH AND HEIGHT'''
window_demo = cmds.window(winId,t="Layout Design",w=500,h=500,menuBar=True)

''' INITIALIZE FRAME LAYOUT AND FORM LAYOUT'''
cmds.frameLayout("FBX_Exporter_UI_animationFrameLayout", collapsable=False,label="",borderVisible=False)
cmds.formLayout("FBX_Exporter_UI_animationFormLayout",numberOfDivisions=100,parent="FBX_Exporter_UI_animationFrameLayout")

cmds.text("texelDensity",l="Texel \n Density",parent="FBX_Exporter_UI_animationFormLayout")

cmds.floatField("density",minValue=0, precision=1, step=1,w=100,h=30)

cmds.text("textureResolution",l="Texture \n Resolution",parent="FBX_Exporter_UI_animationFormLayout")
cmds.floatField("resolution",minValue=0, precision=1, step=1,w=100,h=30)

cmds.button("apply",l="Apply",h=30,w=70,c=applyScale)
cmds.formLayout("FBX_Exporter_UI_animationFormLayout",edit=True,attachForm=[("texelDensity",'top',30),("density",'left',60),("density",'top',30),("resolution",'top',50),("textureResolution",'top',30),("textureResolution",'left',190),("resolution",'top',30),("resolution",'left',270),("apply",'top',30),("apply",'left',390)])

''' AT LAST COMMAND FOR SHOW WINDOW'''
cmds.showWindow(winId)

 

 

0 Likes
985 Views
4 Replies
Replies (4)
Message 2 of 5

Kahylan
Advisor
Advisor

Hi!

 

First off, please when you post code, use a codeblock (</> button) otherwise the code loses its formatting, which isn't optimal, especially for python code because indentation matters. So here is the code with correct indetation for anybody else who wants to give it a go:

import maya.cmds as cmds

def applyScale(*args):
    sel = cmds.ls(sl=True, flatten=True)
    txDensity = cmds.floatField("density",q=True,value=0)
    txSize = cmds.floatField("resolution",q=True,value=0)
    scalar = txDensity/txSize
    
    for obj in sel:
        cmds.unfold(obj,i=0,ss=0.001,gb=0,gmb=1,pub=0,ps=0,oa=0,us=1,s=scalar)


''' UNIQUE WINDOW NAME '''
winId="layoutDesign"

''' CONDITION WHICH CHECK EXISTENCE OF WINDOW'''
if cmds.window(winId,exists=True):
    cmds.deleteUI(winId)

''' CREATES NEW WINDOW WITH SPECIFIED WIDTH AND HEIGHT'''
window_demo = cmds.window(winId,t="Layout Design",w=500,h=500,menuBar=True)

''' INITIALIZE FRAME LAYOUT AND FORM LAYOUT'''
cmds.frameLayout("FBX_Exporter_UI_animationFrameLayout", collapsable=False,label="",borderVisible=False)
cmds.formLayout("FBX_Exporter_UI_animationFormLayout",numberOfDivisions=100,parent="FBX_Exporter_UI_animationFrameLayout")

cmds.text("texelDensity",l="Texel \n Density",parent="FBX_Exporter_UI_animationFormLayout")

cmds.floatField("density",minValue=0, precision=1, step=1,w=100,h=30)

cmds.text("textureResolution",l="Texture \n Resolution",parent="FBX_Exporter_UI_animationFormLayout")
cmds.floatField("resolution",minValue=0, precision=1, step=1,w=100,h=30)

cmds.button("apply",l="Apply",h=30,w=70,c=applyScale)
cmds.formLayout("FBX_Exporter_UI_animationFormLayout",edit=True,attachForm=[("texelDensity",'top',30),("density",'left',60),("density",'top',30),("resolution",'top',50),("textureResolution",'top',30),("textureResolution",'left',190),("resolution",'top',30),("resolution",'left',270),("apply",'top',30),("apply",'left',390)])

''' AT LAST COMMAND FOR SHOW WINDOW'''
cmds.showWindow(winId)
    

 I also changed the variable created by the for loop so it doesn't clash with the "i" flag (eventhough that didn't seem to cause problems.)

Ok, now to the problem at hand... There isn't really a problem here, your script works...

Well, depending on what it should do, which I don't know because you didn't tell me.
What your script currently does, is take an objects UVs and scale them to your ratio "Texel Density"/"Texture Resulotion".

If that is what you want, great you're done!

All you need to make sure of is:

1) that the object you're using the command on has existing UV's

2) that those UV's aren't all at the same position

3) that None of your values are 0

 

But I'm assuming, that you want your UV's to unfold, because if you wanted to simply scale the UV's you could have used the scale command on the UV ID's.
What you need to change for the UV to unfold, is the "iterations" (i) flag in your unfold command, currently it is set to 0. The legacy unfold command normally takes numbers between 5000-10'000 iterations to get a good unfold.


Also, I should probably mention, that your unfold needs to be flawed for the unfold flag to do anything, if you test it on the default UV's of a cube for example, the UV's are already in their optimal positions so unfolding doesn't change them.

 

I hope this helps!

0 Likes
Message 3 of 5

gsingh51_mca19
Contributor
Contributor

Sir, it works but on all UV's and I want to work only on selected UV's is there any possibility with this code. Thanks

Message 4 of 5

gsingh51_mca19
Contributor
Contributor

s

0 Likes
Message 5 of 5

gsingh51_mca19
Contributor
Contributor
Sir, I am creating a GUI with two inputs texel density and texel resolution. After that, if I select a particular object, I want to change the texel density and texel resolution of a specific face or side. I hope you will understand. Thanks
0 Likes