Scripting Snippets

A collection of Python snippets for common functions you may need when creating script mods for The Sims 4.

Get a Random Position on Lot

import services


# returns a sims4.math.Vector3 object
def get_random_position():
    active_lot = services.active_lot()
    return active_lot.get_random_point()

Define a Cheat Command

import sims4


@sims4.commands.Command('lot51.my_cheat', command_type=sims4.commands.CommandType.Live)
def command_my_cheat(_connection=None):
    output = sims4.commands.CheatOutput(_connection)
    output('Cheat run successfully')


@sims4.commands.Command('lot51.my_cheat_with_value', command_type=sims4.commands.CommandType.Live)
def command_my_cheat(value=None, _connection=None):
    output = sims4.commands.CheatOutput(_connection)
    output('Cheat run successfully with value: {}'.format(value))

Inject Tags

from tag import Tag


def inject_tags(kvp):
    with Tag.make_mutable():
        for (k, v) in kvp.items():
            Tag._add_new_enum_value(k, v)


my_tags = {'Func_MyTag': 12345}            
            
# do injection
inject_tags(my_tags)

Get Lot Commodity Value

import services
import sims4


def get_lot_commodity_value(commodity_id=None):
    lot = services.active_lot()
    statistics_manager = services.get_instance_manager(sims4.resources.Types.STATISTIC)
    commodity = statistics_manager.get(commodity_id)
    if not commodity:
        return None
    return lot.commodity_tracker.get_value(commodity, add=True)

Get Value of a Statistic

import services 
import objects.components.types


# subject can be a sim or object
def get_statistic_value(subject=None, stat_id=None):
    if not subject or not stat_id:
        return None
        
    statistic_component = subject.get_component(objects.components.types.STATISTIC_COMPONENT)
    statistic = services.statistic_manager().get(stat_id)
    return statistic_component.get_stat_value(statistic)

Get Sim Info

import services


def get_sim_info(sim_id=None):
    if sim_id:
        current_sim = services.sim_info_manager().get(sim_id)
    else:
        current_sim = services.active_sim_info()

    return current_sim

Get Clock Speed and Current Time

import services


def get_game_speed():
    game_clock = services.game_clock_service()
    return game_clock.clock_speed
    
    
def get_current_time():
    game_clock = services.game_clock_service()
    return game_clock.now()

Check if Sim is member of Household

import services

def is_member_of_household(sim_id:int=None, household_id:int=None):
        if household_id is not None:
            household = services.household_manager().get(household_id)
        else:
            household = services.active_household()

        if household is None:
            return False

        return household.sim_in_household(sim_id)

Get IDs of Household Members

import services

 def get_household_members(household_id:int=None):
        if household_id is not None:
            household = services.household_manager().get(household_id)
        else:
            household = services.active_household()

        ids = list()
        for sim_info in household._sim_infos:
            ids.append(str(sim_info.sim_id))

        return ids

Get Active Household

import services


def get_active_household():
    return services.active_household()

Get Active Zone ID

import services

def get_active_zone_id():
    zone = services.current_zone()
    return zone.id

Get Active Sim ID

import services

def get_active_sim_id():
    sim = services.get_active_sim()
    if not sim:
        return None
    return sim.id

Get Eco Footprint for Current Street

import services


def get_street_service_info():
    street_service = services.street_service()
    if street_service is None:
        return None
    street_provider = street_service.get_provider(services.current_street())
    street_info = {
        "footprint_compatible": street_provider.is_eco_footprint_compatible,
        "footprint_value": False,
        "footprint_convergence_value": False,
        "footprint_direction": False,
        "footprint_state": False,
    }
    if street_provider.is_eco_footprint_compatible:
        street_info['footprint_value'] = street_provider.get_street_footprint_value()
        street_info['footprint_convergence_value'] = street_provider.get_street_footprint_convergence_value()
        street_info['footprint_direction'] = int(street_provider.current_eco_footprint_direction)
        street_info['footprint_state'] = int(street_provider.current_eco_footprint_state)

    return street_info

Get Available DLC/Packs

import sims4


class PackHelper:
    
    @staticmethod
    def get_available_packs():
        return sims4.common.get_available_packs()

    @staticmethod
    def is_pack_installed(pack=None):
        return sims4.common.are_packs_available(pack)
        
        
has_eco_lifestyle = PackHelper.is_pack_installed(sims4.common.Pack.EP09)

Enjoying Lot 51 CC?

Consider becoming a Patron to support new mods
and to help keep the TDESC Browser up-to-date.