srctools.fgd
Parse FGD files, used to describe Hammer entities.
- class srctools.fgd.ValueTypes
Bases:
EnumTypes which can be applied to a KeyValue.
- VOID = 'void'
- CHOICES = 'choices'
- SPAWNFLAGS = 'flags'
- STRING = 'string'
- BOOL = 'boolean'
- INT = 'integer'
- FLOAT = 'float'
- VEC = 'vector'
- ANGLES = 'angle'
- TARG_DEST = 'target_destination'
Aliases:
ENT_HANDLE,EHANDLE
- TARG_DEST_CLASS = 'target_name_or_class'
- TARG_SOURCE = 'target_source'
- TARG_NPC_CLASS = 'npcclass'
- TARG_POINT_CLASS = 'pointentityclass'
- TARG_FILTER_NAME = 'filterclass'
- TARG_NODE_DEST = 'node_dest'
- TARG_NODE_SOURCE = 'node_id'
- STR_SCENE = 'scene'
- STR_SOUND = 'sound'
- STR_PARTICLE = 'particlesystem'
- STR_SPRITE = 'sprite'
- STR_DECAL = 'decal'
- STR_MATERIAL = 'material'
- STR_MODEL = 'studio'
- STR_VSCRIPT = 'scriptlist'
- STR_VSCRIPT_SINGLE = 'script'
- ANGLE_NEG_PITCH = 'angle_negative_pitch'
- VEC_LINE = 'vecline'
- VEC_ORIGIN = 'origin'
- VEC_AXIS = 'axis'
- COLOR_1 = 'color1'
- COLOR_255 = 'color255'
- SIDE_LIST = 'sidelist'
- INST_FILE = 'instance_file'
- INST_VAR_DEF = 'instance_parm'
- INST_VAR_REP = 'instance_variable'
- EXT_STR_TEXTURE = 'texture'
- EXT_VEC_DIRECTION = 'vec_dir'
- EXT_VEC_LOCAL = 'vec_local'
- EXT_ANGLE_PITCH = 'angle_pitch'
- EXT_ANGLES_LOCAL = 'angle_local'
- EXT_SOUNDSCAPE = 'soundscape'
- has_list
Is this a flag or choices value, and needs a [] list?
- valid_for_io
Is this type valid for I/O definitions?
- extension
Is this an extension to the format?
- is_ent_name
Several types are simply a targetname.
- class srctools.fgd.EntityTypes
Bases:
EnumThe kind of entity each definition is.
- BASE = 'baseclass'
Not an entity, others inherit from this.
- POINT = 'pointclass'
Point entity
- BRUSH = 'solidclass'
Brush entity. Can’t have a
modelkeyvalue.
- ROPES = 'keyframeclass'
Used for
move_ropeetc
- TRACK = 'moveclass'
Used for
path_tracketc
- FILTER = 'filterclass'
Used for filters.
- NPC = 'npcclass'
An NPC.
- EXTEND = 'extendclass'
Modifies an existing entity entry (Hammer++ extension)
- is_point
Return whether this is a point entity.
- class srctools.fgd.HelperTypes
Bases:
EnumTypes of functions in the entity header.
- INHERIT = 'base'
- HALF_GRID_SNAP = 'halfgridsnap'
- CUBE = 'size'
Sets bounding box of entity, exposed as the size of purple cube
- TINT = 'color'
- SPHERE = 'sphere'
- LINE = 'line'
- FRUSTUM = 'frustum'
- CYLINDER = 'cylinder'
- ORIGIN = 'origin'
Adds circle at an absolute position.
- VECLINE = 'vecline'
Draws line to an absolute position.
- AXIS = 'axis'
Draws a pair of lines to an absolute position.
- BRUSH_SIDES = 'sidelist'
Highlights brush faces.
- BOUNDING_BOX_HELPER = 'wirebox'
Displays bounding box from two keyvalues
- SWEPT_HULL = 'sweptplayerhull'
Draws the movement of a player-sized bounding box from A to B.
- ORIENTED_BBOX = 'obb'
Bounding box oriented to angles.
- SPRITE = 'iconsprite'
- MODEL = 'studio'
- MODEL_PROP = 'studioprop'
- MODEL_NEG_PITCH = 'lightprop'
Uses separate pitch keyvalue
- ENT_SPRITE = 'sprite'
- ENT_INSTANCE = 'instance'
- ENT_DECAL = 'decal'
- ENT_OVERLAY = 'overlay'
- ENT_OVERLAY_WATER = 'overlay_transition'
- ENT_LIGHT = 'light'
- ENT_LIGHT_CONE = 'lightcone'
- ENT_ROPE = 'keyframe'
- ENT_TRACK = 'animator'
- ENT_BREAKABLE_SURF = 'quadbounds'
Sets the 4 corners on save
- ENT_WORLDTEXT = 'worldtext'
Renders 3D text in-world.
- ENT_CATAPULT = 'catapult'
Renders trigger_catpault trajectors prediction
- ENT_LIGHT_CONE_BLACK_MESA = 'lightconenew'
New helper added in Black Mesa.
- EXT_APPLIES_TO = 'appliesto'
Indicates this entity is only available in the given games.
- EXT_ORDERBY = 'orderby'
Reorder keyvalues. Args = names in order.
- EXT_AUTO_VISGROUP = 'autovis'
Convenience only used in parsing, adds @AutoVisgroup parents for the current entity. ‘Auto’ is implied at the start.
- BBOX = 'bbox'
Deprecated, doesn’t exist.
- extension
Is this an extension to the format?
- class srctools.fgd.AutoVisgroup(name: str, parent: str, ents: set[str] = NOTHING)
Represents one of the autovisgroup options that can be set.
Due to how these are coded into Hammer, our representation is rather strange. We put all the groups into a single dictionary, and on each specify the name of the parent. Note they’re case-sensitive, and can include punctuation.
- exception srctools.fgd.FGDParseError( )
Raised if the FGD contains invalid syntax.
- class srctools.fgd.FGD
A FGD set for a game. May be composed of several files.
- auto_visgroups: dict[str, AutoVisgroup]
- classmethod parse( ) FGD
Parse an FGD file.
- Parameters:
file – A :py:class:filesystem.File` representing the file to read, or a file path.
filesystem – The system to lookup files in. This is needed to resolve file inclusions. If not passed, file must be a :py:class:filesystem.File` to retrieve this automatically.
- apply_bases() None
Fix base values in entities after parsing.
While parsing the classnames may be set as strings, so order in the file doesn’t matter. This fixes them to the real entity objects.
- for ... in sorted_ents() Iterator[EntityDef]
Yield all entities in sorted order.
This ensures only all bases for an entity are yielded before the entity. Otherwise, entities are ordered in alphabetical order.
- collapse_bases(ignore_aliases: bool = True) None
Collapse all bases into the entities that use them.
This operates in-place, and clears all the base attributes as a result.
- export(
- file: FileWText | None = None,
- *,
- label_spawnflags: bool = True,
- custom_syntax: bool = True,
- old_report: bool = False,
Write out the FGD file.
- Parameters:
file – The file to write to. If
None, the contents will be returned instead.label_spawnflags – If set, prepend
[X]to each spawnflag name to indicate the numeric value.old_report – If enabled, use the legacy
kv(*int)syntax for the ‘report’ flag. This is necessary for HL2’s Hammer.custom_syntax – If disabled, all custom syntax like tags and
@resourceswill be skipped. For tagged values, this can write out duplicate copies from different tags.
- parse_file(
- filesys: FileSysT,
- file: File[FileSysT],
- *,
- eval_bases: bool = True,
- eval_extensions: bool = True,
- ignore_unknown_valuetype: bool = False,
- encoding: str = 'cp1252',
Parse one file (recursively if needed).
If eval_bases is False, bases will not be computed. This makes it impossible in some cases to evaluate these later, but it can help if it is not required.
- classmethod engine_dbase() FGD
Load and return a database of entity keyvalues and I/O.
This can be used to identify the kind of keys present on an entity. If you only need specific entities, use
EntityDef.engine_def()instead to avoid needing to fetch all the entities.
- class srctools.fgd.EntityDef(
- type: EntityTypes,
- classname: str = '',
- *,
- keyvalues: dict[str, dict[TagsSet, KVDef]] = NOTHING,
- inputs: dict[str, dict[TagsSet, IODef]] = NOTHING,
- outputs: dict[str, dict[TagsSet, IODef]] = NOTHING,
- kv_order: list[str] = NOTHING,
- bases: list[EntityDef | str] = NOTHING,
- helpers: list[Helper] = NOTHING,
- desc: str = '',
- resources: Sequence[Resource] = (),
- is_alias: bool = False,
A definition for an entity.
- type: EntityTypes
The kind of entity.
- resources: Sequence[Resource]
no
@resourcesdefinition, as opposed to an empty list for an explicit empty resources definition. Useresources_defined()to distinguish between these cases.
- is_alias: bool
If set, the
aliasof()helper was used. This entity should have 1 base, which this is simply an alternate classname for.
- classmethod parse(
- fgd: FGD,
- tok: Tokenizer,
- ent_type: EntityTypes,
- eval_bases: bool = True,
- eval_extensions: bool = True,
- ignore_unknown_valuetype: bool = False,
Parse an entity definition from an FGD file.
The
@PointClassetc keyword should already have been read, and is passed asent_type.
- classmethod engine_def(classname: str) EntityDef
Return the specified entity from an internal copy of the Hammer Addons database.
This can be used to identify the kind of keys/inputs/outputs present on an entity, as well as resources the entity requires/
Precache()es.- Raises:
KeyError – If the classname is not found in the database.
- classmethod engine_classes() Set[str]
Return a set of known entity classnames, from the Hammer Addons database.
- for ... in get_resources(ctx: ~srctools.fgd.ResourceCtx, *, ent: ~srctools.vmf.Entity | None, on_error: ~collections.abc.Callable[[str], object] = <function EntityDef.<lambda>>) Iterator[tuple[FileType, str]]
Recursively fetch all the resources this entity may use, simulating
Precache().- Parameters:
ent – A specific entity to evaluate against. If not provided, functions will silently be skipped.
ctx – Common information about the current map and game configuration. This is passed along to defined entclass functions, and is seperate, so it can be reused for many calls to this function.
on_error – If provided, when functions or entities are missing this will be called with the specific error, and raised if it returns an exception type. If not set, lookups are ignored. Most exceptions can be passed directly here to cause that to be raised.
- strip_tags(tags: frozenset[str]) None
Strip all tags from this entity, blanking them.
Only values matching the given tags will be kept.
- export(
- file: FileWText,
- label_spawnflags: bool = True,
- custom_syntax: bool = True,
- old_report: bool = False,
Write the entity out to a FGD file.
See
FGD.export()for the meaning of the parameters.
- class srctools.fgd.KVDef(
- name: str,
- type: ValueTypes | str,
- disp_name: str,
- default: str = '',
- desc: str = '',
- val_list: list[tuple[int, str, bool, frozenset[str]]] | list[tuple[str, str, frozenset[str]]] | None = None,
- readonly: bool = False,
- reportable: bool = False,
- editor_only: bool = False,
Represents a keyvalue that may be set on entities
If the type is choices or spawnflags,
val_listis required: * For choices it’s a list of (value, name, tags) tuples. * For spawnflags it’s a list of (bitflag, name, default, tags) tuples.- val_list: list[tuple[int, str, bool, frozenset[str]]] | list[tuple[str, str, frozenset[str]]] | None
- readonly: bool
Causes Hammer to prevent this keyvalue from being changed. Doesn’t force the value to the default though.
- reportable: bool
If set, this value is shown in the Entity Report list. Usually applied to things like a sprite’s material or a prop’s model.
- property choices_list: list[tuple[str, str, frozenset[str]]]
Check that the keyvalues are CHOICES type, and then return val_list.
This isolates the type ambiguity of the attr.
- property flags_list: list[tuple[int, str, bool, frozenset[str]]]
Check that the keyvalues are SPAWNFLAGS type, and then return val_list.
This isolates the type ambiguity of the attr.
- class srctools.fgd.IODef(name: str, type: ValueTypes | str = ValueTypes.VOID, desc: str = '')
Represents an input or output for an entity.
- class srctools.fgd.EntAttribute
Common base class for IODef and KVDef.
- property type: ValueTypes
The type of this field.
Custom types can be set as a string, but will appear as
ValueTypes.STRING. To retrieve the custom name, accesscustom_type.
- class srctools.fgd.Helper(*, tags: frozenset[str] = frozenset({}))
Base class for representing helper() commands in the header of an entity.
These mainly add visual widgets in Hammer’s views for manipulating and previewing keyvalues.
This should not be instantiated, only subclasses in _fgd_helpers.
- TYPE: ClassVar[HelperTypes | None] = None
- classmethod parse(args: list[str], tags: frozenset[str]) Self
Parse this helper from the given arguments.
The default implementation expects no arguments.
- overrides() Collection[HelperTypes]
Specify which types can be overriden by this.
If any of these helper types are present before this type, they’re redundant and can be removed. For example size() is ignored if a studio() is present after it.
- class srctools.fgd.UnknownHelper(name: str, args: list[str], *, tags: frozenset[str] = frozenset({}))
Represents an unknown helper.
This is intended only as a method for roundtripping FGDs with unexpected helpers. If you need to interact with an unknown helper, please file an issue/PR to get it implemented properly.
- TYPE: ClassVar[HelperTypes | None] = None
- srctools.fgd.match_tags(
- search: Container[str],
- tags: Collection[str],
Check if the search constraints satisfy tags.
The search tags should be uppercased.
All !tags or -tags cannot be present, all +tags must be present, and at lest one normal tag must be present (if they are) to pass.
- srctools.fgd.validate_tags(tags: ~collections.abc.Collection[str], error: ~collections.abc.Callable[[str], BaseException] = <class 'ValueError'>) frozenset[str]
Check these tags have valid values.
The error exception is raised if invalid.
- class srctools.fgd.Resource( )
Resources used by an entity, with filetype.
If the tags mapping is present, that indicates branch features that should/should not be present. Examples: ‘episodic’ (vs HL2), ‘mapbase’.
- classmethod mdl( ) Resource
Create a resource definition for a model.
Models have an optional
filename#1,2,3suffix to specify which skins are used.
- class srctools.fgd.ResourceCtx(tags: ~collections.abc.Iterable[str] = (), fsys: ~srctools.filesys.FileSystem = VirtualFileSystem('<virtual>'), fgd: ~srctools.fgd.FGD | ~collections.abc.Mapping[str, ~srctools.fgd.EntityDef] | ~collections.abc.Callable[[str], ~srctools.fgd.EntityDef] = <bound method EntityDef.engine_def of <class 'srctools.fgd.EntityDef'>>, mapname: str = '', funcs: ~collections.abc.Mapping[str, ~collections.abc.Callable[[~srctools.fgd.ResourceCtx, ~srctools.vmf.Entity], ~collections.abc.Iterator[~srctools.fgd.Resource | ~srctools.vmf.Entity]]] = ...)
Map information passed to
FileType.ENTCLASS_FUNCfunctions.- fsys: FileSystem
- class srctools.fgd.Snippet(name: str, source_path: str, source_line: int, value: ValueT_co)
A part of some definition which has been given a name to be reused.
- classmethod lookup_single(
- error: Callable[[str], BaseException],
- kind: str,
- mapping: SnippetLookupDict[T],
- key: str,
Locate the one snippet using the specified mapping.
If not found or having multiple,
erroris used to create the exception to raise, usingkindas the snippet type.
- classmethod for ... in lookup_multi(
- error: Callable[[str], BaseException],
- kind: str,
- mapping: SnippetLookupDict[T],
- key: str,
Locate all snippets with a key using the specified mapping.
If not found,
erroris used to create the exception to raise, usingkindas the snippet type.
- classmethod parse(
- fgd: FGD,
- path: str,
- tokeniser: BaseTokenizer,
Parse snippet definitions in a FGD.
- class srctools.fgd.HelperAxis(key: str, *, tags: frozenset[str] = frozenset({}))
A helper which draws a line between two points.
- TYPE: ClassVar[HelperTypes | None] = 'axis'
- class srctools.fgd.HelperBBox(**kwargs)
Deprecated, implementation for the non-existent bbox() type. Use size() instead.
- TYPE: ClassVar[HelperTypes | None] = 'bbox'
- class srctools.fgd.HelperBoundingBox(min: str, max: str, *, tags: frozenset[str] = frozenset({}))
Displays bounding box between two keyvalues.
- TYPE: ClassVar[HelperTypes | None] = 'wirebox'
- class srctools.fgd.HelperBreakableSurf(*, tags: frozenset[str] = frozenset({}))
Specialized helper for func_breakable_surf.
- TYPE: ClassVar[HelperTypes | None] = 'quadbounds'
- class srctools.fgd.HelperBrushSides(key: str, *, tags: frozenset[str] = frozenset({}))
Highlights brush faces in a space-sepearated keyvalue.
- TYPE: ClassVar[HelperTypes | None] = 'sidelist'
- class srctools.fgd.HelperCylinder(
- r: int,
- g: int,
- b: int,
- start_key: str,
- start_value: str,
- end_key: str | None = None,
- end_value: str | None = None,
- start_radius: str | None = None,
- end_radius: str | None = None,
- *,
- tags: frozenset[str] = frozenset({}),
Helper implementation for cylinder().
Cylinder has the same sort of arguments as line(), plus radii for both positions.
- TYPE: ClassVar[HelperTypes | None] = 'cylinder'
- class srctools.fgd.HelperDecal(*, tags: frozenset[str] = frozenset({}))
Specialized helper for infodecal.
- TYPE: ClassVar[HelperTypes | None] = 'decal'
- class srctools.fgd.HelperEnvSprite(mat: str | None, *, tags: frozenset[str] = frozenset({}))
Variant of iconsprite() specifically for env_sprite.
- TYPE: ClassVar[HelperTypes | None] = 'sprite'
- class srctools.fgd.HelperFrustum(
- fov: str | float,
- near_z: str | float,
- far_z: str | float,
- color: str | tuple[int, int, int],
- pitch_scale: float = 1.0,
- *,
- tags: frozenset[str] = frozenset({}),
Helper for env_projectedtexture visuals.
As an extension, values can be literals as well as key names. This is only natively supported by Vitamin Source, but HammerAddons will generate keyvalues to make this work.
- TYPE: ClassVar[HelperTypes | None] = 'frustum'
- class srctools.fgd.HelperHalfGridSnap(*, tags: frozenset[str] = frozenset({}))
Helper implementation for halfgridsnap().
This causes the entity to snap to half a grid. This argument doesn’t use () in Valve’s files.
- TYPE: ClassVar[HelperTypes | None] = 'halfgridsnap'
- class srctools.fgd.HelperInherit(*, tags: frozenset[str] = frozenset({}))
Helper implementation for base().
These specify the base classes for an entity def. This implementation isn’t used, the EntityDef special-cases it.
- TYPE: ClassVar[HelperTypes | None] = 'base'
- class srctools.fgd.HelperInstance(*, tags: frozenset[str] = frozenset({}))
Specialized helper for func_instance.
- TYPE: ClassVar[HelperTypes | None] = 'instance'
- class srctools.fgd.HelperLight(*, tags: frozenset[str] = frozenset({}))
Specialized helper for omnidirectional lights.
- TYPE: ClassVar[HelperTypes | None] = 'light'
- class srctools.fgd.HelperLightSpot( )
Specialized helper for displaying spotlight previews.
- TYPE: ClassVar[HelperTypes | None] = 'lightcone'
- class srctools.fgd.HelperLine(
- r: int,
- g: int,
- b: int,
- start_key: str,
- start_value: str,
- end_key: str | None = None,
- end_value: str | None = None,
- *,
- tags: frozenset[str] = frozenset({}),
Helper implementation for line().
Line has the arguments line(r g b, start_key, start_value, end_key, end_value) It searches for the first entity where ent[start_key] == self[start_value]. If the second pair are present it does the same for those for the other line end.
- TYPE: ClassVar[HelperTypes | None] = 'line'
- class srctools.fgd.HelperModel(model: str | None, *, tags: frozenset[str] = frozenset({}))
Helper which displays models.
If the model is not provided, the ‘model’ key is used.
- TYPE: ClassVar[HelperTypes | None] = 'studio'
- overrides() Collection[HelperTypes]
Avoid some issues where other helpers break this one.
- class srctools.fgd.HelperModelLight(model: str | None, *, tags: frozenset[str] = frozenset({}))
Special model helper, with inverted pitch.
- TYPE: ClassVar[HelperTypes | None] = 'lightprop'
- class srctools.fgd.HelperModelProp(model: str | None, *, tags: frozenset[str] = frozenset({}))
Model helper which does not affect the bounding box.
- TYPE: ClassVar[HelperTypes | None] = 'studioprop'
- class srctools.fgd.HelperOrientedBBox(min: str, max: str, *, tags: frozenset[str] = frozenset({}))
A bounding box oriented to angles.
- TYPE: ClassVar[HelperTypes | None] = 'obb'
- class srctools.fgd.HelperOrigin(key: str, *, tags: frozenset[str] = frozenset({}))
Parse the origin() helper.
- TYPE: ClassVar[HelperTypes | None] = 'origin'
- class srctools.fgd.HelperOverlay(*, tags: frozenset[str] = frozenset({}))
Specialized helper for env_overlay.
- TYPE: ClassVar[HelperTypes | None] = 'overlay'
- class srctools.fgd.HelperOverlayTransition(*, tags: frozenset[str] = frozenset({}))
Specialized helper for env_overlay_transition.
- TYPE: ClassVar[HelperTypes | None] = 'overlay_transition'
- class srctools.fgd.HelperRenderColor(r: int, g: int, b: int, *, tags: frozenset[str] = frozenset({}))
Helper implementation for color().
- TYPE: ClassVar[HelperTypes | None] = 'color'
- overrides() Collection[HelperTypes]
Previous ones of these are overridden by this.
- class srctools.fgd.HelperRope(name_kv: str | None = None, *, tags: frozenset[str] = frozenset({}))
Specialized helper for displaying move_rope and keyframe_rope.
- TYPE: ClassVar[HelperTypes | None] = 'keyframe'
- class srctools.fgd.HelperSize( )
Helper implementation for size().
This sets the bbox for the entity.
- TYPE: ClassVar[HelperTypes | None] = 'size'
- overrides() Collection[HelperTypes]
Additional versions of this are not available.
- class srctools.fgd.HelperSphere( )
Helper implementation for sphere().
- TYPE: ClassVar[HelperTypes | None] = 'sphere'
- class srctools.fgd.HelperSprite(mat: str | None, *, tags: frozenset[str] = frozenset({}))
The sprite helper, for editor icons.
If the material is not provided, the ‘model’ key is used.
- TYPE: ClassVar[HelperTypes | None] = 'iconsprite'
- overrides() Collection[HelperTypes]
Specifies helpers which this makes irrelevant.
- class srctools.fgd.HelperSweptPlayerHull(*, tags: frozenset[str] = frozenset({}))
Draws the movement of a player-sized bounding box from A to B.
- TYPE: ClassVar[HelperTypes | None] = 'sweptplayerhull'
- class srctools.fgd.HelperTrack(*, tags: frozenset[str] = frozenset({}))
Specialized helper for path_track-style entities.
This no longer does anything.
- TYPE: ClassVar[HelperTypes | None] = 'animator'
- class srctools.fgd.HelperVecLine(key: str, *, tags: frozenset[str] = frozenset({}))
A variant of line() which draws a line to the entity.
- TYPE: ClassVar[HelperTypes | None] = 'vecline'
- class srctools.fgd.HelperWorldText(*, tags: frozenset[str] = frozenset({}))
Specialized helper for point_worldtext.
- TYPE: ClassVar[HelperTypes | None] = 'worldtext'
- class srctools.fgd.HelperExtAppliesTo(applies: list[str] = NOTHING, *, tags: frozenset[str] = frozenset({}))
Allows specifying “tags” to indicate an entity is only used in certain games.
- TYPE: ClassVar[HelperTypes | None] = 'appliesto'
- class srctools.fgd.HelperExtAutoVisgroups(path: list[str] = NOTHING, *, tags: frozenset[str] = frozenset({}))
Convenience for parsing, adds @AutoVisgroups to entities.
‘Auto’ is implied at the start.
- TYPE: ClassVar[HelperTypes | None] = 'autovis'