Attention

You are viewing PyUnity docs under the develop branch. As such, they are only applicable if you installed from source. Go to https://docs.pyunity.x10.bz/en/latest/ for the most recent release.

pyunity.files module


Module to load files and scripts. Also manages project structure.

pyunity.files.convert(type, list)[source]

Converts a Python array to a C type from ctypes.

Parameters:
  • type (_ctypes.PyCSimpleType) – Type to cast to.

  • list (list) – List to cast

Returns:

A C array

Return type:

object

class pyunity.files.Behaviour[source]

Bases: Component

Base class for behaviours that can be scripted.

Awake()[source]

Called every time a scene is loaded up, regardless whether the Behaviour is enabled or not. Cannot be an async function.

async Start()[source]

Called every time a scene is loaded up. Only called when the Behaviour is enabled. Can be either a normal function or an async function.

async Update(dt)[source]

Called every frame. Can be either a normal function or an async function.

Parameters:

dt (float) – Time since last frame, sent by the scene that the Behaviour is in.

async FixedUpdate(dt)[source]

Called every frame, in each physics step. Can be either a normal function or an async function.

Parameters:

dt (float) – Length of this physics step

async LateUpdate(dt)[source]

Called every frame, after physics processing. Can be either a normal function or an async function.

Parameters:

dt (float) – Time since last frame, sent by the scene that the Behaviour is in.

async OnPreRender()[source]

Called before rendering happens. Can be either a normal function or an async function.

async OnPostRender()[source]

Called after rendering happens. Can be either a normal function or an async function.

OnDestroy()[source]

Called at the end of each Scene. Cannot be an async function.

class pyunity.files.Scripts[source]

Bases: object

Utility class for loading scripts in a folder.

template = 'from pyunity import *\n\nclass {}(Behaviour):\n    async def Start(self):\n        pass\n\n    async def Update(self, dt):\n        pass\n'

Type:    str

var = {}

Type:    dict

static CheckScript(text)[source]

Check if text is a valid script for PyUnity.

Parameters:

text (list) – List of lines

Returns:

If script is valid or not.

Return type:

bool

Notes

This function checks each line to see if it matches at least one of these criteria:

  1. The line is an import statement

  2. The line is just whitespace or blank

  3. The line is just a comment preceded by whitespace or nothing

  4. The line is a class definition

  5. The line has an indentation at the beginning

These checks are essential to ensure no malicious code is run to break the PyUnity engine.

static GenerateModule()[source]
static LoadScript(path, force=False)[source]

Loads a PyUnity script by path.

Parameters:
  • path (Pathlike) – A path to a PyUnity script

  • force (bool) – Continue on error

Returns:

The compiled PyUnity script

Return type:

type

Notes

This function will add a module to sys.modules that is called PyUnityScripts, and can be imported like any other module. The module will also have a variable called __pyunity__ which shows that it is from PyUnity and not a real module. If an existing module named PyUnityScripts is present and does not have the __pyunity__ variable set, then a warning will be issued and it will be replaced.

static Reset()[source]
class pyunity.files.Asset[source]

Bases: SavesProjectID

GetAssetFile(gameObject)[source]
SaveAsset(ctx)[source]
class pyunity.files.Texture2D[source]

Bases: Asset

Class to represent a texture.

load()[source]

Loads the texture and sets up an OpenGL texture name.

setImg(im)[source]
use()[source]

Binds the texture for usage. The texture is reloaded if it hasn’t already been.

GetAssetFile(gameObject)[source]
SaveAsset(ctx)[source]
classmethod FromOpenGL(texture)[source]
class pyunity.files.Skybox[source]

Bases: object

Skybox model consisting of 6 images

names = ['right.jpg', 'left.jpg', 'top.jpg', 'bottom.jpg', 'front.jpg', 'back.jpg']

Type:    list

points = [-1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1]

Type:    list

compile()[source]
use()[source]
class pyunity.files.Prefab[source]

Bases: Asset

Prefab model

Contains(obj)[source]
Instantiate(scene=None, parent=None, position=Vector3(0, 0, 0), rotation=Quaternion(1, 0, 0, 0), scale=Vector3(1, 1, 1), worldSpace=False)[source]

Instantiate this prefab.

Parameters:
  • scene (Scene, optional) – The scene to instantiate in. If None, the current scene is selected.

  • parent (GameObject, optional) – The parent to instantiate the Prefab under. If None, the prefab will be instantiated at the root of the scene.

  • position (Vector3, optional) – Position of the newly created GameObject, by default Vector3.zero()

  • rotation (Quaternion, optional) – Rotation of the newly created GameObject, by default Quaternion.identity()

  • scale (Vector3, optional) – Scale of the newly created GameObject, by default Vector3.one()

  • worldSpace (bool, optional) – Whether the above 3 properties are world space or local space, by default False

Returns:

The newly created GameObject

Return type:

GameObject

Raises:

PyUnityException – If scene is None but no scene is running

GetAssetFile(gameObject)[source]
SaveAsset(ctx)[source]
class pyunity.files.ProjectSavingContext[source]

Bases: object

class pyunity.files.File[source]

Bases: object

pyunity.files.checkScene(func)[source]
class pyunity.files.Project[source]

Bases: object

property assets
Write()[source]
ImportFile(file, uuid=None, write=True)[source]
ImportAsset(asset, gameObject=None, filename=None)[source]
SetAsset(file, obj)[source]
GetUuid(obj)[source]
static FromFolder(folder)[source]