RimWorld

RimWorld

103 ratings
AI-gen portraits made easy (v2)
By bolphen
Guide to set up the AI-gen in the avatar mod, which allows you to easily generate consistent AI portraits for your colonists.
3
2
   
Award
Favorite
Favorited
Unfavorite
Set up the backend
The latest version of avatar mod features a new AI-gen UI allowing you to easily generated portraits in-game, as soon as you've set up a backend. Here is how it works.


1. Install the mod
Install the avatar mod. Optionally, test that the pixel-art avatars correctly show up in-game.
https://gtm.steamproxy.vip/sharedfiles/filedetails/?id=3111373293

2. Set up a backend
Set up a local backend for AI-gen. This step depends on what hardware setup you have. If you have no clue what this is about, you should probably go to 2.B.

2.A
Update. If you have a local Stable Diffusion instance, @John Jacob Jingleheimer Wick has made the following configurable python script https://github.com/deadmanIsARabbit/RimworldAvatarToStableDiffusion/
You can check out the instructions there.

If you have a fancy GPU setup or a remote AI-gen service, you can set up an executable script which has the following repaint syntax (you can name it whatever you like)
repaint "path/to/image.png" "prompts"
It accepts the name of an image and some prompts, and repaint and replace the image with AI-gen. The actual script content will be depending on the software you are using (if you don't know how, google how to use the software via commandline). It's recommended to use img2img to use the input image as reference, but you can also just ignore it and do txt2img instead and overwrite the image.

For AUTOMATIC1111 here are two example scripts kindly shared by @Saiphe and @Pat https://pastebin.com/PXy0NHn9 https://pastebin.com/Tp9jXXt7 You can also check out their comments below for more detailed instructions (scroll back to comments from 14-16 Apr, 2024).

2.B
If you don't have a fancy GPU, you can use my script which makes use of the api of the site https://pixai.art. Note that for now you'll need python installed on your computer. I also do not guarantee this will always work in the future.

2.B.1 Get a token
  • Register an account on https://pixai.art (you can use fake addresses if you want)
  • Open DevTools in your browser and go to Storage -> Local Storage -> api.pixai.art:token (for Chrome it's under the Application tab)
  • Copy the value of the token, which should be a long sequence of letters and numbers

2.B.2 Set up the repaint script
  • Install python if it's not yet available on your computer. Once done, install the dependency via the following command
    python -m pip install requests pillow
  • Download the files from this repo https://github.com/bolphen/pixaiAPI
  • Open the "repaint.py" file as a text file and replace the token with the one you just got
  • Change "high_priority = False" to "high_priority = True". Save the changes you've made.
  • Make sure the python script is executable. Follow the instructions for your OS if you don't know how: Windows[stackoverflow.com] Linux[stackoverflow.com] macOS[stackoverflow.com]
    To check that the script is executable, you should be able to run directly
    repaint.py "path/to/image.png" "portrait, beautiful"
    without "python" at the beginning (also note that "path/to/image.png" is a placeholder; replace it with the path of an actual image!).

3. Link avatar to the backend
Now you can start the game and go into the settings of avatar. Put the path of the repaint executable (the full path of "repaint.py" if you are using mine) in the last text field. And now you are good to go!
Using the AI-gen UI
When in game, you can right click on the avatar and choose the "Generate portrait" option.

You'll see a new window with the reference image and some basic prompts.

You can choose whether to draw the headgear / clothes, and you can review and freely change the prompts as you find fit. Once you clicked "OK", the image and the prompts will be sent to the repaint script for AI-gen. The image will automatically show up once the generation has finished.
Enjoy!
Notes
Troubleshooting
If you are having trouble setting the mod up, make sure the script is correctly working outside the game. The instructions in the StackOverflow links should normally work. Alternatives are either set up a .bat script to run the python script and provide the mod with the .bat, or use "auto-py-to-exe" to convert the python script into an actual .exe.

If you have successfully set up the mod but it somehow stopped working, here are a few common pitfalls (if you are using pixai.art)
  • You are simply out of credits...
  • Someone noticed that the api token might change after a certain amount of time. You can try to get the token again and update it in the script.

Misc stuff
  • For each pawn, the mod tries to read the static portrait and displays it in-game. The static portraits are stored in the avatar folder, which should be
    • Windows: %USERPROFILE%\Appdata\LocalLow\Ludeon Studios\RimWorld by Ludeon Studios\avatar
    • Linux: ~/.config/unity3d/Ludeon Studios/RimWorld by Ludeon Studios/avatar/
    • macOS: ~/Library/Application Support/RimWorld/avatar/
    The portraits are named like "Tynan_Sylvester_123.png". Generating portrait in-game will create a png with this name. You can further retouch / modify the AI-gen portraits any way you like, or even replace it with any other image.
  • The default settings generate portraits with white or gray backgrounds. You can set up the repaint script to auto-remove the background (for example, using https://github.com/danielgatis/rembg ). Tho it shouldn't be too much of a hassle to remove them by hand (magic-wand!).
  • If you are using the pixai api script, the cost is 200 credits per generation, so the daily free quota allows you to generate 50 portraits.
  • In fact, the script is doing 4x batch generations, so if you want, you can go on the website and choose the best one yourself (the script currently simply picks the first one) and manually replace the static portrait with it.
  • You can also use other models, for example realistic ones: just replace the model id in "repaint.py".
  • You can add your own AIGenPromptDef: see here[github.com]

Generation specific
  • The auto-generated prompts may not be optimized. For example, if a pawn is wearing both a T-shirt and marine armor, both keywords will show up, but you should remove "T-shirt" since it's just noise and might end up making the armor look "soft-textile-like".
  • Headgear are usually tricky to draw, so they are disabled by default.
  • Xenotypes are also tricky to get right, and I don't think there's any easy all-in-one solution. Just play with the prompts until you're happy with the results. Maybe someone will train a model for RimWorld one day, who knows :)

Credits
Thanks to Tiago, Red007Master, and SirOctogiraffe for comments and feedbacks!

I've set up a discord server, feel free to join https://discord.gg/3FAVpagmK9
The idea is that people who've already set up the script may help others troubleshooting. You can also share your AI-gen portraits and your workflows.
113 Comments
Misha 16 Jul @ 4:39am 
poorly explained mess
JageriuS 30 May @ 8:53am 
@John Jacob Jingleheimer Wick
I got the script working with Your .exe, thanks a lot, is there a way to make the exe not minimize Rimworld on every generation?
hi guys, sorry my bad english, is not my first language. I had that goddamn error of the black command screen doing nothing. In my case, it was Phyton was not installed correctly. I had to uninstall it and install it again MARKING the checkbox " Add Python to PATH", VERY, VERY important, at least for me and for now, Phyton MUST be 3.10.6. Once installed you have to delete the folder "venv" inside of webui, then run "webui-user.bat" (don't forget to add the "--api" inside the commandline first) and wait. it might take between 2 to 5 minutes, after that it should work fine, at least for me. I hope it can be helpful for somebody.
Morrowind3 24 May @ 4:19am 
@WetSovietBear
I had the same issue. For me, the problem was that I misinterpreted the instructions for the --api flag.

You should edit the .bat and edit it to the COMMANDLINE_ARGS variable there. I was running the bat from the command line with webui_user.bat --api which doesn't work
Personman 20 May @ 6:37pm 
why tho?
Tripolar_fox 16 Apr @ 6:46am 
A brilliant guide. Works fine, though I have to change the token sometimes and re-compile repaint.py script into an .exe file using pyinstaller. Still, this is just excellent!
Does anybody know though how to make a transparent background instead of white?
BIGDICKOSKAR#420 8 Apr @ 2:58pm 
can I use civitai instead of pixai?
Direcub10 26 Mar @ 8:40pm 
@WetSovietBear have u tried turning it off and on again
WetSovietBear 26 Mar @ 1:11pm 
@John Jacob Jingleheimer Wick
I'm using your exe, and the game is able to run it, it asks which model I want to use, after I pick one it just closes and stable diffusion does nothing.
@ODevil
Got that. Responded on GitHub. Let's keep it there.