PDA

View Full Version : Python - access to history


i9089303
11-17-2010, 02:47 PM
hello

I have write a python script for maya which creates suction cups for octopus which work as I wanted it. When the script run, there is a window with few sliders which controls the length and the width of the of the cup. The tool is basically a series of 3 extrude. The slider have access to to each one of these (as long the history is not deleted) using setAttr command.

I want to be able run the script as many times as I need it, creating deferent shapes, without deleting the previous windows which is easy but when I am running the script and a new windows opens the old on does not have access to its own cup(s). Actually when I am trying to move the sliders, they pass the values of the new window. I guess this has something to do with the variables. How can I do it so that each windows (as long is open) be able to control the cups that it has create.

Maybe I am not giving the best explanation so I am gonna write down the script and you can run it in order to review the problem.

Thanks.

value = 0.5
extrFirst = cmds.polyExtrudeFacet( kft=False, ltz=value, ls=(1, 1, 0) )
extrOutRing = cmds.polyExtrudeFacet( ls=(.3, .3, 0) )
extrSecond = cmds.polyExtrudeFacet( ltz=-(value/2))

cmds.window( t='Suction Cup Generator', s=False)
cmds.columnLayout()
cmds.floatSliderGrp( 'length', l='Length', cw3=(50,1,200), f=True,
min=0.01, max=1.0, fmn=0.01, fmx=100.0, v=0.5, s=0.01, dc='changeLength()')
cmds.floatSliderGrp( 'widthOut', l='Width out', cw3=(50,1,200), f=True,
min=0, max=10, fmn=0, fmx=100.0, v=1, s=0.01, dc='changeWidthOut()')
max = cmds.floatSliderGrp( 'widthOut', q=True, v=True )
cmds.floatSliderGrp( 'widthIn', l='Width in', cw3=(50,1,200), f=True,
min=0, max=max, fmn=0, fmx=max, v=0.3, s=0.01, dc='changeWidthIn()')

cmds.showWindow()

def changeLength():
length = cmds.floatSliderGrp( 'length', q=True, v=True )
cmds.setAttr( '%s.localTranslateZ' % extrFirst[0], length )
cmds.setAttr( '%s.localTranslateZ' % extrSecond[0], -(length/2) )

def changeWidthIn():
widthIn = cmds.floatSliderGrp( 'widthIn', q=True, v=True )
cmds.setAttr( '%s.localScaleX' % extrOutRing[0], widthIn )
cmds.setAttr( '%s.localScaleY' % extrOutRing[0], widthIn )

def changeWidthOut():
widthOut = cmds.floatSliderGrp( 'widthOut', q=True, v=True )
cmds.setAttr( '%s.localScaleX' % extrFirst[0], widthOut )
cmds.setAttr( '%s.localScaleY' % extrFirst[0], widthOut )

bendingiscool
11-17-2010, 08:04 PM
Wouldn't it be a better idea to either a. create the suction cup in code and store that as a variable, then you can access its controls each time you make a new one. Or b. have the sliders effect the item you have selected, so then you can just select the desired cup, and adjust that.

cheers,
Chris

i9089303
11-17-2010, 10:30 PM
bendingiscool thanks for your reply, but I am not sure that I completely understood your advice.

What do you mean by creating a suction cup in code?

Jacobborsting
11-18-2010, 10:18 AM
Its always a good idea to build your gui as an object where all the variables are children of that object. Its much cleaner and you wont run into problems with shares variables between instances of the same gui.

Below is your code in a class structure:
class suctionCup():
def __init__(self, winName="winTheWindow"):
self.winTitle = "Suction Cup Generator"
self.winName = winName
def create(self):
if cmds.window(self.winName, exists=True):
cmds.deleteUI(self.winName)

cmds.window(self.winName, title=self.winTitle)
value = 0.5
self.extrFirst = cmds.polyExtrudeFacet( kft=False, ltz=value, ls=(1, 1, 0) )
self.extrOutRing = cmds.polyExtrudeFacet( ls=(.3, .3, 0) )
self.extrSecond = cmds.polyExtrudeFacet( ltz=-(value/2))
cmds.columnLayout()
self.lengthSlider = cmds.floatSliderGrp( 'length', l='Length', cw3=(50,1,200), f=True,
min=0.01, max=1.0, fmn=0.01, fmx=100.0, v=0.5, s=0.01, dc=self.changeLength)
self.widthOutSlider = cmds.floatSliderGrp( 'widthOut', l='Width out', cw3=(50,1,200), f=True,
min=0, max=10, fmn=0, fmx=100.0, v=1, s=0.01, dc=self.changeWidthOut)
max = cmds.floatSliderGrp( 'widthOut', q=True, v=True )
self.widthInSlider = cmds.floatSliderGrp( 'widthIn', l='Width in', cw3=(50,1,200), f=True,
min=0, max=max, fmn=0, fmx=max, v=0.3, s=0.01, dc=self.changeWidthIn)

cmds.showWindow( self.winName )

def changeLength(self, arg=None):
length = cmds.floatSliderGrp( self.lengthSlider, q=True, v=True )
cmds.setAttr( '%s.localTranslateZ' % self.extrFirst[0], length )
cmds.setAttr( '%s.localTranslateZ' % self.extrSecond[0], -(length/2) )

def changeWidthIn(self, arg=None):
widthIn = cmds.floatSliderGrp( self.widthInSlider, q=True, v=True )
cmds.setAttr( '%s.localScaleX' % self.extrOutRing[0], widthIn )
cmds.setAttr( '%s.localScaleY' % self.extrOutRing[0], widthIn )

def changeWidthOut(self, arg=None):
widthOut = cmds.floatSliderGrp( self.widthOutSlider, q=True, v=True )
cmds.setAttr( '%s.localScaleX' % self.extrFirst[0], widthOut )
cmds.setAttr( '%s.localScaleY' % self.extrFirst[0], widthOut )

inst = suctionCup()
inst.create()

i9089303
11-18-2010, 11:43 AM
Jacobborsting thank you so much.
I really appreciate your help.

i9089303
11-18-2010, 02:00 PM
Jacobborsting, I tired to run the script but i cant.

a) I am running everything and then just the name of the class suctionCup() and all it gives me is that:
# Result: <__main__.suctionCup instance at 0x2E3FBE40> #

UPDATE: Ignore the above problem, I made it work but the problem b) exist.


b) Also looking this part of the code:
def create(self):
if cmds.window(self.winName, exists=True):
cmds.deleteUI(self.winName)

I am use this when I dont want to have twice the same window open, this is going to kill the window which exist with the same name. But I want to be able run this script many times and keep all the windows open in order to make changes, easy and fast. (I know that always can I have access to the extrude attributes from the attribute editor, but this can cost times when will be 100 suction cups open).

Jacobborsting
11-18-2010, 02:21 PM
Then just remove the Ui checker:

class suctionCup():
def __init__(self):
pass
def create(self):
value = 0.5
self.extrFirst = cmds.polyExtrudeFacet( kft=False, ltz=value, ls=(1, 1, 0) )
self.extrOutRing = cmds.polyExtrudeFacet( ls=(.3, .3, 0) )
self.extrSecond = cmds.polyExtrudeFacet( ltz=-(value/2))
cmds.window( t='Suction Cup Generator', s=False)
cmds.columnLayout()
self.lengthSlider = cmds.floatSliderGrp( 'length', l='Length', cw3=(50,1,200), f=True,
min=0.01, max=1.0, fmn=0.01, fmx=100.0, v=0.5, s=0.01, dc=self.changeLength)
self.widthOutSlider = cmds.floatSliderGrp( 'widthOut', l='Width out', cw3=(50,1,200), f=True,
min=0, max=10, fmn=0, fmx=100.0, v=1, s=0.01, dc=self.changeWidthOut)
max = cmds.floatSliderGrp( 'widthOut', q=True, v=True )
self.widthInSlider = cmds.floatSliderGrp( 'widthIn', l='Width in', cw3=(50,1,200), f=True,
min=0, max=max, fmn=0, fmx=max, v=0.3, s=0.01, dc=self.changeWidthIn)

cmds.showWindow()

def changeLength(self, arg=None):
length = cmds.floatSliderGrp( self.lengthSlider, q=True, v=True )
cmds.setAttr( '%s.localTranslateZ' % self.extrFirst[0], length )
cmds.setAttr( '%s.localTranslateZ' % self.extrSecond[0], -(length/2) )

def changeWidthIn(self, arg=None):
widthIn = cmds.floatSliderGrp( self.widthInSlider, q=True, v=True )
cmds.setAttr( '%s.localScaleX' % self.extrOutRing[0], widthIn )
cmds.setAttr( '%s.localScaleY' % self.extrOutRing[0], widthIn )

def changeWidthOut(self, arg=None):
widthOut = cmds.floatSliderGrp( self.widthOutSlider, q=True, v=True )
cmds.setAttr( '%s.localScaleX' % self.extrFirst[0], widthOut )
cmds.setAttr( '%s.localScaleY' % self.extrFirst[0], widthOut )

inst = suctionCup()
inst.create()

i9089303
11-18-2010, 03:04 PM
Then just remove the Ui checker

that works, great thanks

CGTalk Moderation
11-18-2010, 03:04 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.