Using char key modifiers

On 04/08/2017 at 19:34, xxxxxxxx wrote:

Hello,

I made a simple script to help easily rotate an object by 90 degrees, the axis is determined by a predefined key ("1", "2", "3") being held prior to script execution.

This seems to work well, I tried it with the script mapped to the ` key.
I wanted to check if it is written in the best way, especially considering where I have put my undo steps:

import c4d
from c4d import gui
  
def checkPressed(k) :
    # Check any one key
    bc = c4d.BaseContainer()
    if c4d.gui.GetInputState(c4d.BFM_INPUT_KEYBOARD, k ,bc) :
        if bc[c4d.BFM_INPUT_VALUE] == 1:
            
            print "%s PRESSED" % unichr(k)
            return True
        else:
            
            print "%s NOT PRESSED" % unichr(k)
            return False
  
def rotat(obj, r) :
    doc.StartUndo()
          
    doc.AddUndo(c4d.UNDOTYPE_HIERARCHY_PSR, obj)
    if r is "x":
        newMatrix = obj.GetMl() * c4d.utils.MatrixRotX(c4d.utils.Rad(90)) 
    elif r is "y":
        newMatrix = obj.GetMl() * c4d.utils.MatrixRotY(c4d.utils.Rad(90)) 
    elif r is "z":
        newMatrix = obj.GetMl() * c4d.utils.MatrixRotZ(c4d.utils.Rad(90)) 
    else:
        print "error"
        
    obj.SetMl(newMatrix)
        
    doc.EndUndo()
    
  
  
def main() :
  obj=doc.GetActiveObject()
  if obj is None:
        pass
  
  if checkPressed(ord('1')) :
    rotat(obj, "x")
  elif checkPressed(ord('2')) :
    rotat(obj, "y")
  elif checkPressed(ord('3')) :
    rotat(obj, "z")
    
  c4d.EventAdd()
  
if __name__=='__main__':
  main()
  

On 05/08/2017 at 05:57, xxxxxxxx wrote:

Update (https://github.com/NNNenov/C4Dhandy/blob/master/scripts/functional/Turn90/Turn90.py )

import c4d
  
def checkPressed(k) :
    # Check any one key
    bc = c4d.BaseContainer()
    if c4d.gui.GetInputState(c4d.BFM_INPUT_KEYBOARD, k ,bc) :
        if bc[c4d.BFM_INPUT_VALUE] == 1:
            return True
        else:
            return False
  
def rotat(obj, r) :
    
    newMatrix = obj.GetMl()
    
    doc.StartUndo()
    doc.AddUndo(c4d.UNDOTYPE_HIERARCHY_PSR, obj)
    
    if r is "x":
        newMatrix *= c4d.utils.MatrixRotX(c4d.utils.Rad(90)) 
    elif r is "y":
        newMatrix *= c4d.utils.MatrixRotY(c4d.utils.Rad(90)) 
    elif r is "z":
        newMatrix *= c4d.utils.MatrixRotZ(c4d.utils.Rad(90)) 
        
    obj.SetMl(newMatrix)
        
    doc.EndUndo()
  
def main() :
  
  #obj = doc.GetActiveObject()
  objs = doc.GetActiveObjects(0)
  
  if len(objs) == 0:
        pass
  for obj in objs:
      if checkPressed(ord('1')) :
        rotat(obj, "x")
    	
      elif checkPressed(ord('2')) :
        rotat(obj, "y")
    	
      elif checkPressed(ord('3')) :
        rotat(obj, "z")
    
  c4d.EventAdd()
  
if __name__=='__main__':
  main()
  

On 07/08/2017 at 02:58, xxxxxxxx wrote:

I see no problem with the position of the undo step.
In regards to "best way", I leave any discussion on optimization up to the community.