Hi Simon, thank you for responding so quickly. Funny enough, the "rambling 
bit" is what helped me most :) I had tried a similar solution before, but 
after a bit more time of thinking, I figured it out. Just for kicks though, 
I tried the "import time" solution. Following are my tests and new question!

Test solution #1:
I tried using Time to solve it. It works, but with one unfortunate 
bug/feature: If you are holding the slider and don't move it, it'll think 
that you released the slider and reset the undo Queue.
Here's the modified sample code:
import maya.cmds as mc
import time

class Jimmy(object):
    
    def __init__(self):
        ''' Make Jimmy '''
        mc.polySphere(name='Jimmy')
        self.timey = 0

    def makeJimmyUI(self,*args,**kwargs):
        ''' Make Jimmy's window '''
        
        # Remove UI if it already exists
        if mc.window('jimmyUI',query=1,exists=1):
            mc.deleteUI('jimmyUI')

        mc.window('jimmyUI')
        mc.columnLayout()

        # Create Jimmy's slider
        mc.floatSliderGrp('allInTheHips',
                          label='Move Jimmy',
                          dragCommand=self.moveJimmy, # Have Jimmy's slider 
activate moveSphere
                          sliderStep=0.01,
                          minValue=-10,
                          maxValue=10,
                          )

        # Show Jimmy's Window
        mc.showWindow('jimmyUI')
    
    def moveJimmy(self,*args, **kwargs):
        ''' Move Jimmy up and down '''
        
        thisTime = time.time() # Store the current time
        remTime = (thisTime - self.timey) # Compare it to the new time
        if remTime <.03: # If the time difference is less than 0.03 seconds,
            mc.undo() # Run the undo function
        else:
            print 'Reset Timer' # If more than 0.03 seconds pass, reset the 
undo queue

        mc.setAttr('Jimmy.translateY',
                   mc.floatSliderGrp('allInTheHips', query=True, value=True)
                   )

        self.timey = time.time() # Store the new time


James = Jimmy()
James.makeJimmyUI() # As long as "Jimmy" exists, this UI can be reopened 
later



Test solution #2 (successful with one caveat):
Solution: Store selected Item and its initial value in global variables. If 
it's the same item and NOT the same initial value, undo. If undo deselects 
the item, redo. Proceed with code.
Bug: The script outputs every single Undo that is performed. Not sure how 
to make it not display the Undo output.
Sample code time! (note: the code no longer creates a sphere. It instead 
manipulates the selected item. Did this so you can test on different items)
import maya.cmds as mc

class Jimmy(object):
    ''' The code no longer creates an item. Instead, create multiple 
objects, select one, then another,
    then re-select the previous one. The code works!! IT IS 
ALIIIIIIIIVE!!!!!
    '''
    def __init__(self):
        ''' Get Jimmy's attributes '''
        self.oldItem = None
        self.oldItemTranslate = None

    def makeJimmyUI(self,*args,**kwargs):
        ''' Make Jimmy's window '''
        
        # Remove UI if it already exists
        if mc.window('jimmyUI',query=1,exists=1):
            mc.deleteUI('jimmyUI')
        
        mc.window('jimmyUI')
        mc.columnLayout()

        # Create Jimmy's slider
        mc.floatSliderGrp('allInTheHips',
                          label='Move Jimmy',
                          dragCommand=self.moveJimmy, # Have Jimmy's slider 
activate moveSphere
                          sliderStep=0.01,
                          minValue=-10,
                          maxValue=10,
                          )

        # Show Jimmy's Window
        mc.showWindow('jimmyUI')
    
    def moveJimmy(self,*args, **kwargs):
        ''' Move selected item up and down '''
        
        # Move the first item in the selection only
        newItem = mc.ls(sl=1)[0]
        newItemTranslate = mc.getAttr(newItem+'.translateY')
        
        # If this item was just moved by the slider and if item's value is 
the same as its original value
        if self.oldItem == newItem and self.oldItemTranslate != 
newItemTranslate:
            
            # Undo the move!
            mc.undo()
            # But if undoing deselects the manipulated item, Redo :)
            if mc.ls(sl=1) == []:
                mc.redo()
            
        else:
            # Store the new item with the old Item
            self.oldItem = newItem
            self.oldItemTranslate = newItemTranslate
        
        # Perform the calculation!
        newItemTranslate = mc.floatSliderGrp('allInTheHips',
                                             query=True,
                                             value=True) + 
self.oldItemTranslate
        mc.setAttr(newItem + '.translateY', newItemTranslate)


James = Jimmy()
James.makeJimmyUI() # As long as "Jimmy" exists, this UI can be reopened 
later


As I mentioned, it works!! Go ahead and Undo to your heart's content :)

Only problem is that the script editor spits out code every time the Undo 
function is run. So my new question is:

Is there a way to turn off the script editor echo for a single line of 
code!? I want the animator to be able to see output code as if it was 
native.

Also, if anyone can come up with a better solution to this overall puzzle, 
I'd love feedback!

Thank you again, Simon!!

Isai



> Sounds like a tricky situation, as when would the code know the user is 
> done "tweaking" the slider and happy with the result, as you are 
> continuesly apply the command. if you store the original state before a 
> tweak, you will end up with a ton of undos, which is the situation you are 
> in now.
>
  
>
You could store the original position of the selected object when the ui is 
> loaded, that way if the user performs an undo, it goes back to the start 
> position. That is half way there, as you wil have problems if users select 
> another object while the ui is open. So you would have to put a check, to 
> see if you are already storing that objects position in an undo dictionary 
> (im rambling a bit)
>  
>
You could try implement a timer, that is the user doesn't slide the bar for 
> a few seconds then it stores overwrites the undo transform that is being 
> stored
>
> as you are using commands, each command store an undo in Mayas stack, I 
> would suggest you start using OM1 or OM2, preferably OM2 to perform these 
> moves, as that way you get to control the undo stack that Maya registers.
>
> How do you see users interacting with the UI?
>
> On Thursday, 19 October 2017 12:46:12 UTC+11, Isai Calderon wrote:
>>
>> I have a window with an interactive slider moving keys around (keying is 
>> unnecessary). Unfortunately the Undo queue loads up with every tiny change 
>> in value.
>>
>> The idea is to move the slider many times until it's just right, 
>> sometimes even closing the UI and opening it up again, all while not 
>> loading the Undo queue.
>>
>> Any ideas on how to group all the instances of the "moveJimmy" command 
>> below that the slider activates into a single Undo function?
>>
>> The script is for moving keys, but the following code is a good example 
>> of what's going on. Makes sphere, makes UI with slider, slider moves sphere 
>> up and down:
>>
>> import maya.cmds as mc
>>
>> class Jimmy(object):
>>     
>>     def __init__(self):
>>         ''' Make Jimmy '''
>>         mc.polySphere(name='Jimmy')
>>
>>     def makeJimmyUI(self,*args,**kwargs):
>>         ''' Make Jimmy's window '''
>>         
>>         # Remove UI if it already exists
>>         if mc.window('jimmyUI',query=1,exists=1):
>>             mc.deleteUI('jimmyUI')
>>         
>>         mc.window('jimmyUI')
>>         mc.columnLayout()
>>
>>         # Create Jimmy's slider
>>         mc.floatSliderGrp('allInTheHips',
>>                           label='Move Jimmy',
>>                           dragCommand=self.moveJimmy, # Have Jimmy's 
>> slider activate moveSphere
>>                           sliderStep=0.1,
>>                           minValue=-10,
>>                           maxValue=10,
>>                           )
>>
>>         # Show Jimmy's Window
>>         mc.showWindow('jimmyUI')
>>     
>>     def moveJimmy(self,*args, **kwargs):
>>         ''' Move Jimmy up and down '''
>>         print 'yup'
>>         # value = value
>>         mc.setAttr('Jimmy.translateY',
>>                    mc.floatSliderGrp('allInTheHips',
>>                                      query=True,
>>                                      value=True)
>>                    )
>>
>>
>> James = Jimmy()
>> James.makeJimmyUI() # As long as "Jimmy" exists, this UI can be reopened 
>> later
>>
>>
>> Thank you!!
>>
>> All the best,
>>
>> Isai
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/a955e6bc-9742-4807-bc65-cbd323b5cf27%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to