Python Scripting Notes


How to write “Text String”

  • To run the Python script directly, write the following (script to get the login user name)

    import getpass;getpass.getuser()
    
    • A semicolon (;) allows you to write a multi-line script on a single line

  • If you want to run external Python code, you can use PYTHONPATH or similar to pass it through

    • One line of code to load the module and execute the function

    import actions;actions.actions_text()
    

Return value

  • Only return types list[str] or str are accepted.

  • list[str] should contain strings, one element per line

    def get_text() -> list[str]:
        text_lines = [
           'Line 1',
           'Line 2',
           'Line 3'
        ]
        return text_line
    
  • If you want to break a string, insert a newline code \n.

    def get_text() -> str:
        text_lines = 'Line 1\nLine 2\nLine 3'
        return text_line
    
  • If the maya.cmds command returns a value in the form of list[str] or str , it can be executed directly

    import maya.cmds;maya.cmds.ls(sl=True)
    

    Warning

    • It is the developer’s responsibility to guarantee the return type.Make sure that None or any other type is not returned.

    • If there are no characters to return, return an empty string ('').