SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

Dynamic Prompts can use not only random prompts but also variables


Updated on May 24, 2025
Added a note about duplicate keys in YAML.

What is Dynamic Prompts?

It is an extension for Stable Diffusion webui A1111 and ComfyUI that randomizes prompts.

{red|white|black} bikini

👇 ランダムで1つ選ばれ、下記のいずれかが使われる

red bikini
white bikini
black bikini

Installation instructions can be found on the distribution page above or by searching online.
The official syntax is here.

https://github.com/adieyal/sd-dynamic-prompts/blob/main/docs/SYNTAX.md


How to use it in the prompt input field

Select one at random

{red|white|black} bikini, from {above|below}
👇
red bikini, from above
...
white bikini, from below

Weight specification

`number::prompt` specifies the priority.
It seems the total does not need to be "1".
In the example below, the adoption rate of `red` will be higher.

{0.5::red|0.1::white|0.2::black} bikini

Multiple selection

`number$$` inserts the specified number of selections at the beginning.

{2$$cat|dog|bird|monkey}
👇
cat,dog

Using a character other than "," as a connector for multiple selections

`number$$ character $$` allows you to specify the character between selected prompts by inserting it at the beginning.

{2$$ and $$white|black|red} bikini
👇
white and red bikini

Specifying a range for the number of selections

`min-max$$` allows you to specify the minimum and maximum number of selections by inserting it at the beginning.
You can also specify the connector character here with `min-max$$ string $$`.

{1-2$$ and $$dog|cat|bird|monkey}
👇
cat
dog and bird

You can also omit the minimum and maximum values.

{-2$$dog|cat|bird|monkey}
{2-$$dog|cat|bird|monkey}


Selecting from a file

You can read prompts from files by placing them in the following folder.
stable-diffusion-webui/extensions/sd-dynamic-prompts/wildcards

Basic usage

Create a color.txt file in the folder above and write the following.

red
blue
white

`__filename__` will select one line from the file.

__color__ bikini
👇
white bikini

You can also use formats like specifying the number of items

You can also use formats like `number$$` described above.

{2$$__color__}
👇
blue, red

{2$$__color__|__color__}
👇
blue, red

{2$$ and $$__color__}
👇
blue and red

You can also use formats within files

You can specify the number of items or connection characters even inside files.

# costume.txt
{red|white|pink} bikini
school uniform, {red|blue} bowtie


__costume__
👇
black bikini

Calling files from within files (nesting)

This is an example where two files, color.txt and costume.txt, are prepared.

# color.txt
red
white
black

# costume.txt
__color__ bikini
__color__ skirt


__costume__
👇
white bikini

Specifying files with wildcards

`__filename*__` allows you to randomly select files starting with the specified characters.

# color_a.txt、color_b.txt がある状態
__color*__
👇
両方のファイルからどれか1つ

YAML and JSON can also be used

In addition to simple text files, you can also use YAML and JSON. JSON is omitted in this article.

YAML

# example.yaml
color:
  - white
  - black
costume:
  swimsuit:
    - bikini
    - one-piece swimsuit
  school:
    - bow tie, school uniform
    - buruma, gym uniform

Please note that when using it, it is `
__key__` instead of `__filename__`.

If a file with the same name as a key in the YAML exists, the YAML seems to take precedence. In this case, even if color.txt exists, it will be selected from color within the YAML.

When selecting a lower hierarchy, separate them with `/`.

__color__ __costume/swimsuit__
👇
black one-piece swimsuit

You can still use the same formats as before. You can nest them and specify the number of items.

color:
  - white
  - black
costume:
  - __color__ bikini
  - __color__ one-piece swimsuit
__costume__
👇
white bikini

Useful aspects of YAML

It is convenient because it can include line breaks.

color:
  - red
  - black
costume:
  swimsuit:
    - __color__ bikini
    - __color__ one-piece swimsuit
  school:
    - school uniform, __color__ bowtie
    - gym uniform, __color__ buruma
situation:
  - |
    __costume/swimsuit__, beach, blue sky, sunbeam,
    jumping, from below
  - |
    __costume/school__, classroom,
    classroom chair, classroom desk,
    window, blue sky
__situation__
👇
black bikini, beach, blue sky, sunbeam,
jumping, from below

What happens if the same key exists in multiple files?

Assume that the same `hair` key exists in both hair_color.yaml and hair_style.yaml.
In this case, the first key recognized will be adopted.
In the example below, no matter how many times you run it, `black hair` or `silver hair` will not be adopted.

# hair_color.yaml
hair:
  - twintails
  - ponytail

# hair_style.yaml
hair:
  - black hair
  - silver hair

# 入力プロンプト
__hair__

# 結果
twintails または ponytail 👈 最初に認識した `hair` が採用される

To handle duplicate keys, use the filename as the prefix key

If you use the filename as the prefix key, there will be no issues even if subsequent keys overlap with other files.
When calling them, separate them with a `__{prefix key}/{target key}__` like this, using a `/`.

# hair_color.yaml
hair_color:
  hair:
    - twintails
    - ponytail

# hair_style.yaml
hair_style:
  hair:
    - black hair
    - silver hair

# 入力プロンプト
__hair_color/hair__, __hair_style/hair__

Variables can be used!!

`${variable_name=value}` allows you to define variables.

${cloth_color=red}
${weather=blue sky}
${cloth_color} bikini, ${weather}
👇
red bikini, blue sky

I want to select variable values randomly

You can use the existing syntax within the value portion.

${color1=__color__}
${color2={red|white|black}}

I want to select a variable value from random options "only once"

`${variable_name=!value}` Adding a "!" will immediately evaluate and fix the value. If "!" is not included, it will be randomly selected every time the variable is called.

# 全部同じ色になる
${color=!{black|white}}
${color} hair, ${color} bikini
👇
black hair, black bikini

# ランダムな色になる
${color={black|white}}
${color} hair, ${color} bikini
👇
black hair, white bikini

Specifying a default value

`${variable_name:default_value}` is used for specification.
Since it will result in an error if the variable is not defined, specifying a default value is safer.

# 変数 color が指定されていない状態で color を使う
# 初期値が無いとエラーになる
${color:white} bikini
👇
white bikini

Usage example

color:
  - red
  - black
angle:
  - from above
  - from below
costume:
  - ${color:white} bikini
  - ${color:white} one-piece swimsuit
item:
  - ${color:red} beach ball
  - ${color:red} parasol
situation:
  - |
    __costume__, beach, blue sky, sunbeam,
    jumping, ${angle:from below}, __item__
${color=!__color__}
${angle=__angle__}
__situation__
👇
red bikini, beach, blue sky, sunbeam,
jumping, from above, red beach ball

Since I want to match the colors of the swimsuit and the ball, the color variable is fixed using the "=!" notation. The angle variable is defined with "=" because it does not need to be fixed.

Passing arguments to files

This is used when you want to fix variables only within a specific file, for example.

__situation(color=!__color__,angle=__angle__)__

Comment lines

A "#" at the beginning of a line in a text file becomes a comment line.

# ここはコメントなので選択されない
red
blue

YAML originally uses # for comments, doesn't it?

situation:
# 使い方
# 変数 color: 水着と小物の色
# 変数 angle: 画角
  - |
    __costume__, beach, blue sky, sunbeam,
    jumping, ${angle:from below}, __item__

You can also enter comments within DynamicPrompt syntax.
However, it is highly incompatible with extensions that automatically format prompts, such as sd-webui-prompt-all-in-one.

__situation(
# 水着と小道具の色
color=!__color__,
# アングル指定
angle=__angle__
)__

# しかし sd-webui-prompt-all-in-one によって下記のように整形されてしまった……
__situation(  # 水着と小道具の色  color=!__color__,  # アングル指定  angle=__angle__ )__,

There are many other things too

It seems that by using jinja2 templates, you can also use conditional branching and loops. At that point, it's basically a script.

https://github.com/adieyal/sd-dynamic-prompts/blob/main/jinja2.md

Promotion

I create illustrations of little girls. I also run a support site, so please consider supporting me if you'd like.


いいなと思ったら応援しよう!