Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Using char key modifiers

    Scheduled Pinned Locked Moved PYTHON Development
    3 Posts 0 Posters 384 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      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()
        
      
      
      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        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()
          
        
        
        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post