Entities (Ursina style)¶
Entity spawns an object from a description, like the Ursina game engine. Every argument is also an attribute you can change later.
from entity import Entity, color
ground = Entity(
model='cube',
color=color.magenta,
z=-.1,
y=-3,
origin=(0, .5),
scale=(50, 1, 10),
collider='box',
)
ground.y += 1 # move it up 1 m
ground.color = '#ff8000' # repaint it
ground.destroy() # remove it
Models¶
model decides what is spawned.
model= |
Spawns |
|---|---|
'cube' 'sphere' 'cylinder' 'cone' 'plane' |
A shape. A cube with scale 1 is 1 m wide. |
'earth' |
A Cesium globe |
'camera', 'camera_pawn' |
A cinema camera, or one you can fly |
'text3d' |
3D text |
'plot', 'table', 'gizmo', 'file_explorer', 'sysmon', 'desktop' |
Starcel's tools |
'zoom_plot', 'manim', ... |
Any other spawn_X function in ue_spawn.py, by its X |
'C:/pictures/a.png', 'b.mp4', 'c.wav', 'd.zarr' |
A picture, video, sound or saved table |
'duck.glb', .fbx, .obj, .gltf, .stl, .blend ... |
A 3D model, loaded while playing; no import needed |
'/Game/...' |
A StaticMesh or Blueprint asset |
None |
An empty actor |
Relative file names are looked up in the current folder, Content, Content/Models and Content/Scripts.
Entity(model='duck.glb', y=1, scale=50)
Entity(model='C:/pictures/cat.jpg', z=5)
Entity(model='earth', preset='night')
Arguments that the model's spawn function takes are passed through:
Entity(model='camera', preset='cinematic') # spawn_camera(preset=...)
Entity(model='text3d', text='hello') # spawn_text3d(text=...)
Position, rotation, scale¶
e = Entity(model='cube')
e.position = (0, 2, 5) # x right, y up, z forward, in metres
e.x += 1
e.rotation_y = 45 # from above, positive turns right
e.rotation_x = 10 # positive looks down
e.scale = 2 # one number scales evenly
e.scale_y = 4
origin¶
origin is the point of the model that sits at position, measured in sizes of the model.
Entity(model='cube', origin=(0, -.5)) # stands on its position
Entity(model='cube', origin=(0, .5)) # hangs below its position
Using Unreal's axes instead¶
Pass Unreal values (FVector in centimetres, z up) whenever you like:
axes='ue' makes plain numbers use Unreal's axes and centimetres too, and rotation=(roll, pitch, yaw). Entity.axes = 'ue' makes that the default for every entity.
Colours¶
from entity import Entity, color
Entity(model='cube', color=color.magenta)
Entity(model='cube', color=color.rgb(1, .5, 0)) # 0..1
Entity(model='cube', color=color.rgb32(255, 128, 0)) # 0..255
Entity(model='cube', color=color.hsv(300, 1, 1)) # hue in degrees
Entity(model='cube', color=color.hex('#ff00ff80')) # rrggbb or rrggbbaa
Entity(model='cube', color='magenta') # a name or '#rrggbb'
Entity(model='sphere', color=color.azure.with_alpha(.4)) # see-through
Named colours: white black gray light_gray dark_gray red orange yellow lime green turquoise cyan azure blue violet magenta pink brown gold clear.
color also tints pictures and the materials of 3D models.
Materials¶
Entity(model='sphere', material='M_ShinyGold') # from /Game/Materials
Entity(model='sphere', material='/Game/Materials/M_Color.M_Color')
Colliders¶
collider= |
Shape |
|---|---|
'box', 'sphere', 'capsule' |
Fitted to the model |
'mesh' |
The exact shape of the model |
None |
No collision |
If you leave collider out, the model keeps what it spawns with: shapes and pictures can be hit, 3D model files can't.
The Unreal actor¶
e.actor is the Unreal actor. Its methods also work on the entity:
Other options: visible=False hides it, name='Floor' names it in the outliner.
A small scene¶
from entity import Entity, color
Entity(model='plane', scale=20, color=color.dark_gray)
for i in range(10):
Entity(model='cube', x=i - 4.5, origin=(0, -.5),
scale=(.8, 1 + i * .3, .8), color=color.hsv(i * 36, .8, 1))
Entity(model='sphere', y=5, color=color.gold, collider='sphere')
In the tests¶
Copied from the code when this site is built. x0 is where the test's band starts, in metres.
From the tests: test_entity (Content/Scripts/test_spawn.py:1910)
def test_entity(base_y=0):
"""Spawn entities the Ursina way and change them after they spawn:
shapes with colours, origin and colliders, a translucent sphere, a 3D
model file and 3D text, then move and repaint one."""
from entity import Entity, color
x0 = base_y / 100 + 1 # metres, the start of this test's band
results = {}
def spawn(name, **kwargs):
e, t0 = None, time.monotonic()
try:
e = Entity(**kwargs)
except Exception as ex:
_log_exception(f'entity_{name}', ex)
results.update(_result(f'entity_{name}', e.actor if e else None,
inputs=kwargs, elapsed=time.monotonic() - t0))
return e
spawn('cube', model='cube', color=color.magenta, x=x0, y=1,
origin=(0, -.5), collider='box')
spawn('sphere', model='sphere', color=color.azure.with_alpha(.4),
x=x0 + 2, y=1.5)
spawn('cylinder', model='cylinder', color=color.hsv(30, .8, 1),
x=x0 + 4, y=1, scale=(.5, 2, .5))
spawn('text3d', model='text3d', text='Entity', x=x0 + 6, y=2)
if os.path.isfile(_DUCK_GLB):
spawn('glb', model=_DUCK_GLB, x=x0 + 9, y=1, scale=.1)
else:
results.update(_skip('entity_glb', f'not found: {_DUCK_GLB}'))
# Every argument is also an attribute
e = spawn('changed', model='cube', color='red', x=x0 + 12, y=1)
if e is not None:
ok = None
try:
e.y += 1
e.rotation_y = 45
e.scale = .5
e.color = '#ff8000'
if abs(e.y - 2) < 1e-3 and abs(e.rotation_y - 45) < 1e-3:
ok = e.actor
else:
_log(f' entity_changed: y={e.y}, rotation_y={e.rotation_y}')
except Exception as ex:
_log_exception('entity_changed/set', ex)
results.update(_result('entity_attributes', ok,
expected='y == 2 and rotation_y == 45'))
return results