Python Scripting

The Sims 4 uses XML Tuning to store game data, and Python to interpret that data. Modders can use Python as a basis to create their own XML Tuning (for example, module tuning or test sets), to run a command from the game's console, to store settings, to avoid overriding files, and more. The vast majority of mods can be made without scripting, but scripting is a required element for more advanced mods.

Getting Started
To get started with Python scripting, visit this website (S4S Forum Post) to download and install Andrew's starter project for scripting. This tutorial will cover the installation of Python 3.7.0 and PyCharm Community Edition.

Once the starter project is set up, you should mark all folders containing Python code as a sources root, so that PyCharm can help you write code correctly. This includes the,  ,  , and   subfolders in the   folder, as well as each   folder in your projects. You can mark a directory as a sources root by right-clicking on it and using Mark Directory As > Sources Root. You will know that you've correctly marked the directory when the folder icon turns blue and you no longer see red lines under import statements.

Types of Scripts
There are many different kinds of scripts that one may need to write depending on the task at hand. This is not a comprehensive list, but it covers the most common uses for scripts.

Commands
Commands are scripts that can either be triggered by the  of interaction tuning or the game's cheat console. This includes custom cheat codes.

Injection
Injection is an incredibly powerful form of script modding that is used to edit tuning at runtime. Injection may also be used to refer to the act of appending a custom function onto an existing class method, which triggers the modder's script every time the class method is called.

Custom Tuning
Custom tuning files can be created with scripts, such as creating custom test sets that can parse original test syntax. This also includes custom module tuning, which can be used to store settings and tuning references for ease of use in other scripts and especially injections.

Settings
If a mod requires its own settings, it may do so with a JSON or CFG file which must be parsed with a script.

Scripting Tutorials/Guides
Scripting tutorials are the next step to creating a script for The Sims 4. Many of these tutorials aren't easy to find as they are written in forums or on a blog post. This list will link to the various tutorials.


 * Injector Script (Crucial for SA Injection, etc)
 * Adding Super Affordances onto Objects/Sims
 * Adding Super Affordances onto Sims
 * Injecting Super Affordances (TURBODRIVER, Recommended to view this for starters)
 * Adding Emotional Whimsets
 * Getting a Town's Population
 * Storing Sim ID into a Commodity
 * Get To Work Employees
 * In-Game Notifications
 * Adding a Trait onto Club Requirements
 * Ok/Cancel & User Input Via Python (Outdated)
 * InventoryType Overrides Via Scripting
 * Python: Getting Sim Names
 * Tips on Creating a New Interaction
 * Script Automation (Alarms)
 * Injecting Reward Traits (Scumbumbo)
 * Injecting Satisfaction Rewards (TURBODRIVER)
 * Scripts are Case Sensitive
 * Showing all Commodities/Statistics on a lot with a Cheat
 * Proximity Detection
 * Looping through every Sim that the Actor knows to check for a relationship
 * Getting Furnished Lot Value
 * Get data from .package with a script (list of dicts, dict, frozendict, turning frozendict into regular dict)
 * Playing Sound Effects Via Python
 * Adding "loot_list" onto "basic_extras" (Iced Cream)
 * How to Debug your Script (YouTube Video)
 * Script Debugging (Written Version)
 * Accessing CAS Modifiers Via Script
 * Automatically Add Traits to All Sims
 * Sims 4 Modern Python Coding: A Tutorial
 * Injecting to "test_autonomous"
 * Create a Custom Tuning Test or Medium Article
 * Running Command every Few Sim-Minutes
 * Getting a Statistic/Commodity Value
 * Injecting Interactions Via Tags: PasteBin
 * Getting Social Media Followers Count
 * Overridding Off-The-Grid Functionality
 * Getting a Tooltip to Show Up
 * Get All Objects on the Lot with a Specific Object Definition ID
 * Injecting Super Affordances through a Referenced Super Affordance
 * Adding/Removing Traits Via Script
 * Loop an effect/VFX Via a Custom Class
 * Adding/Removing Commodities to "intiial_commodities_blacklist" of a Trait
 * Get Sim's Current Mood, fit/fat slider values and Add/Remove a Buff
 * Injecting Loots onto Buffs (TwelfthDoctor1: LootBuff Injector)
 * How to make a Startup Notification
 * Adding buffs, super_affordances onto RoleState (RoleState Injector)
 * Injecting a TestSet into TestSet Reference (Triplis)
 * Injecting a TestSet into Test Globals (LMS/Scumbumbo)
 * Injecting a TestSet into Test Globals (Triplis)
 * Injecting Super Affordance from AffordanceList (Triplis)
 * Injecting Static Commodites into an Interaction (Triplis)
 * Injecting Filters into SimFilter (Frankk)
 * Creating Lots of Pie Menus Automatically (thepancake1)
 * A Command that transfers a PickedSim into another Household (Frankk)
 * Using Snippets in The Sims 4 for Modding (LeRoiDeTout)

For loose leaf Scripting examples, please look here.