Python  scripting  in  Scripthea - sMacros

 

If you know some Python, Scripthea offers Python scripting with sMacros (Scripthea macros) from within the application. You can automate some tasks that haven't been automated by Scripthea. For example, you can scan over a SD parameter or modify the prompts in particular way or even connect an AI agent to help you with your dream images.

Setting up python scripting

Usually you don't need to set up anything different from the default configuration Scripthea came with. 

The default Enable Python macros is on but if you don't care about scripting you can disable Python macros along with its tab.

The default location type is Integrated which point to the embedded python installed along with Scripthea. In case you prefer to run your own Python installation you can opt to Custom and Browse to the python3XX.dll of your choice. The reason for that could be that you need more python packages than the minimalistic integrated installation.
In order to extend your python functionaly with packages you have two options: either you instal python and install all the packages you need (if you haven't this already) or extend functionality of integrated installation. For the second option see this clip for instructions how to install pip etc.

After setting custom Python installation it would be a good idea to validate this new location - click on the button Validate python location

 

The python scripting is using three classes via which you can access Scripthea functionality. You don't need to create instances of the classes they are already created.

The Scripthea sMacro classes/instances are:

 1. st (stands for ScripThea) with following methods:

  • print(dynamic text, string color) - similar of python print to output into sMacro print panel. Mostly aimed for intermediate info and debuging. color is string of the name or hexadecimal of the color. Python print(... and st.print(... are switchable from the respective checkbox at the top of the print panel.
  • log(dynamic text) - output into the main log panel (left). Mostly aimed for the user.
  • Input(string info, string defaultText)- similar to standart input function in python. It will open dialog box for the user to enter some text.
  • IsCancellationRequested - When you start your macro that value is False but if you click on Cancel button it becomes True. If you check that value regularly in your macro and it is true call sys.exit(<code>) to interupt the execution of your code. the number <code> is printed out.

2. qry (short for QueRY) with following methods:

  • Text2Image(string prompt) - Generate an image with given prompt. If prompt is missing, it takes selected prompt from the composer which must be in Single mode. Returns the image filename
  • SelectCues(int percentage, int idx) - Select percentage of cues in selected pool; if idx = 0 in selected tab; if idx = -1 in all the tabs in the pool; if idx > 0 in the idx tab in the pool. Returns a list of selected cues.
  • mSetApply(string mSet, bool append) - Apply mSet to modifiers, in addition (append) or not to the checked already modifiers. Return True if successful.
  • GetPreview(bool append) - Generate prompts combining selected cues with checked modifiers. The composer must be in Scan mode and some cues must be selected. append is to add to or replace current selection of modifiers. If mSet = Reset all modifiers are deselected. Return a list of the generated prompts.
  • SetPreview(List<string> prompts, bool append) - Generate prompts combining selected cues with selected modifiers. The composer must be in Scan mode and some cues must be selected. If mSet is empty the current set of modifiers is used. append is to add to or replace current selection of modifiers. If mSet = 'Reset' all modifiers are deselected. Returns the total nunber of preview prompts.
  • ScanImages(bool fromPreview) - Generate series of images from prompts from the composer. fromPreview - if True a preview list is used, if  False- the same as pressing the Scan button on the composer. The composer must be in Scan mode and some cues must be checked. Return list of tuples (prompt,filepath).
  • PromptList2Image(List<string> prompts) - Generate image with given prompts in a file. file format is the same as in save file in preview panel. Return list of tuples (prompt,filepath)
  • ImageDepot(string command, string folder) - Command ->get: get working image depot folder; create: create <folder> directory; switch: switch working image depot to <folder>; setNext: create and set working image-depot-folder next to current working IDF. If folder is empty it creates a time-stamp folder. Return the absolute path to the new ImageDepot. e.g. ImageDepot('setnext','') ImageDepot('get','')
  • getStatus - Get the current status of the composer. [Idle, SingeQuery, Scanning, Request2Cancel]
     

3. prm (for PaRaMeter) with following methods:

  • get(string prmName) - Get SD parameter.Possible parameter names are: negative_prompt: string, width: integer, height: integer, sampler_name: string (Euler a, Euler, LMS, Heun, DPM2, DPM2 a,DPM++ 2Sa,DPM++ 2M, DPM++ SDE, DPM fast, DPM adaptive, LMS Karras, DPM2 Karas, DPM2 a Karas, DPM++ 2Sa Karas, DPM++ 2M Karas, DPM++ SDE Karas, DDIM, PLMS, UniPC), restore_faces: boolean, seed: integer, cfg_scale: double, steps: integer.Returns the parameter value.
  • set(string prmName, dynamic prmValue) - Set SD parameter, returns True/False.Possible parameter names are: negative_prompt: string, width: integer, height: integer, sampler_name: string (Euler a, Euler, LMS, Heun, DPM2, DPM2 a,DPM++ 2Sa,DPM++ 2M, DPM++ SDE, DPM fast, DPM adaptive, LMS Karras, DPM2 Karas, DPM2 a Karas, DPM++ 2Sa Karas, DPM++ 2M Karas, DPM++ SDE Karas, DDIM, PLMS, UniPC), restore_faces: boolean, seed: integer, cfg_scale: double, steps: integer.
python sMacro tab

 python sMacro tab offers three panels inside: left one is for the code, middle one is for print output and right one - sMacro help

The code editor offers the usual buttons/features and Run button for sMacro execution. If you need to have the option to cancel the sMacro execution mid-flight you can do that by checking regularly st.IsCancellationRequested in your code, if it becomes True that means that Cancel button has been pressed and your code is supposed to  call sys.exit(<code>) , the <code> number will be printed out in the middle panel (see ImagesTest.py )