Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware 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

    Howto to pass arguments to a python script

    Cinema 4D SDK
    python windows
    2
    4
    537
    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.
    • G
      Gaal Dornik
      last edited by

      Hello, i'd like to pass arguments for a python script via shortcuts, is this possible?

      Thanks.

      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @Gaal Dornik
        last edited by ferdinand

        Hello @Gaal-Dornik,

        Welcome to the Plugin Café forum and the Cinema 4D development community, it is great to have you with us!

        Getting Started

        Before creating your next postings, we would recommend making yourself accustomed with our Forum and Support Guidelines, as they line out details about the Maxon SDK Group support procedures. Of special importance are:

        • Support Procedures: Scope of Support: Lines out the things we will do and what we will not do.
        • Support Procedures: Confidential Data: Most questions should be accompanied by code but code cannot always be shared publicly. This section explains how to share code confidentially with Maxon.
        • Forum Structure and Features: Lines out how the forum works.
        • Structure of a Question: Lines out how to ask a good technical question. It is not mandatory to follow this exactly, but you should follow the idea of keeping things short and mentioning your primary question in a clear manner.

        About your First Question

        Just as note for future support requests, your question is very ambiguous, please try to be more precise in the future, as we have to guess and cover multiple cases otherwise.

        The answer to your question depends on what you would consider to be a 'Python script' and 'arguments'.

        When you run your script against an instance of c4dpy, arguments are handled just as in a vanilla interpreter, i.e., command line arguments will end up in sys.argv. When a script is for you a Script Manager script, the question is hard to answer, as there is no way to pass arguments here, as such scripts are invoked by clicking a button.

        In a Script Manager script, you can open a dialog to let the user make inputs, or as a naiver approach, put global constants to be modified by the user at the top of the file. To have the pattern of a cogwheel icon next to a command icon as shown below, to allow users to define settings before running the command, you will have to implement a CommandData plugin. But opening a dialog in a script is not that much different functionality wise.

        50d095da-55d4-4d63-a203-f643d0eb7e40-image.png

        Cheers,
        Ferdinand

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 0
        • G
          Gaal Dornik
          last edited by Gaal Dornik

          To eliminate ambiguity - i want to bind multiple keyboard shortcuts to a single python script, but with different arguments obviously.
          It's supporsed to be done inside of Cinema 4D (not c4dpy). In Modo for example you can bind a shortcut to a script with arguments pretty easily - you just add args separated with a space after name of the script: cmds.mapKey F5 @{script.py} arg1 arg2 ... etc. I was hoping for a similar solution...

          I guess i'll take dialog route in this case.

          Thanks.

          ferdinandF 1 Reply Last reply Reply Quote 0
          • ferdinandF
            ferdinand @Gaal Dornik
            last edited by

            Hey @Gaal-Dornik,

            In Cinema 4D you can bind a command to multiple shortcuts, but adding arguments is not possible. You would have to poll the keyboard yourself to distinguish such events.

            Cheers,
            Ferdinand

            Result:

            49a4bee1-8135-412f-b238-43d21184e071-image.png

            Code:

            """Runs different code based on which keys are pressed.
            
            Must be run as a Script Manager script. Pressing Shift+ALT+A while the script is invoked will
            print "Foo", while pressing Ctrl+Alt+A will print "Bar".
            
            See:
                https://developers.maxon.net/docs/py/2024_0_0a/misc/inputevents.html
            """
            import c4d
            
            def CheckKeyboardState(*args: tuple[str]) -> bool:
                """Returns #True when the given key sequence is pressed, #False otherwise.
                """
                result = []
                for char in (n.upper() for n in args if isinstance(n, str)):
                    if char == "SHIFT":
                        c = c4d.KEY_SHIFT
                    elif char == "CTRL":
                        c = c4d.KEY_CONTROL
                    elif char == "ALT":
                        c = c4d.KEY_ALT
                    else:
                        c = ord(char)
                        
                    bc = c4d.BaseContainer()
                    if not c4d.gui.GetInputState(c4d.BFM_INPUT_KEYBOARD, c, bc):
                        raise RuntimeError("Failed to poll the keyboard.")
            
                    result += [True if bc[c4d.BFM_INPUT_VALUE] == 1 else False]
            
                return all(result)
            
            def main() -> None:
                """
                """
                if CheckKeyboardState("Shift", "Alt", "A"):
                    print ("Foo")
                elif CheckKeyboardState("Ctrl", "Alt", "A"):
                    print ("Bar")
                else:
                    print("None")
            
            
            
            if __name__ == '__main__':
                main()
            

            MAXON SDK Specialist
            developers.maxon.net

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