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.core module¶
Source code: pyunity/core.py
Core classes for the PyUnity library.
This module has some key classes used throughout PyUnity, and have to be in the same file due to references both ways. Usually when you create a scene, you should never create Components directly, instead add them with AddComponent.
Example
To create a GameObject with 2 children, one of which has its own child, and all have MeshRenderers:
>>> from pyunity import * # Import
Loaded config
Trying GLFW as a window provider
GLFW doesn't work, trying PySDL2
Trying PySDL2 as a window provider
Using window provider PySDL2
Loaded PyUnity version 0.9.0
>>> mat = Material(RGB(255, 0, 0)) # Create a default material
>>> root = GameObject("Root") # Create a root GameObjects
>>> child1 = GameObject("Child1", root) # Create a child
>>> child1.transform.localPosition = Vector3(-2, 0, 0) # Move the child
>>> renderer = child1.AddComponent(MeshRenderer) # Add a renderer
>>> renderer.mat = mat # Add a material
>>> renderer.mesh = Mesh.cube(2) # Add a mesh
>>> child2 = GameObject("Child2", root) # Create another child
>>> renderer = child2.AddComponent(MeshRenderer) # Add a renderer
>>> renderer.mat = mat # Add a material
>>> renderer.mesh = Mesh.quad(1) # Add a mesh
>>> grandchild = GameObject("Grandchild", child2) # Add a grandchild
>>> grandchild.transform.localPosition = Vector3(0, 5, 0) # Move the grandchild
>>> renderer = grandchild.AddComponent(MeshRenderer) # Add a renderer
>>> renderer.mat = mat # Add a material
>>> renderer.mesh = Mesh.cube(3) # Add a mesh
>>> root.transform.List() # List all GameObjects
/Root
/Root/Child1
/Root/Child2
/Root/Child2/Grandchild
>>> child1.components # List child1's components
[<Transform position=Vector3(-2, 0, 0) rotation=Quaternion(1, 0, 0, 0) scale=Vector3(1, 1, 1) path='/Root/Child1'>, <pyunity.MeshRenderer object at 0x00000170E4199CF0>]
>>> child2.transform.children # List child2's children
[<Transform position=Vector3(0, 5, 0) rotation=Quaternion(1, 0, 0, 0) scale=Vector3(1, 1, 1) path='/Root/Child2/Grandchild'>]
- class pyunity.core.Tag[source]¶
Bases:
object
Class to group GameObjects together without referencing the tags.
- Parameters:
- Raises:
ValueError – If there is no tag name
IndexError – If there is no tag at the provided index
TypeError – If the argument is not a str or int
- class pyunity.core.GameObject[source]¶
Bases:
SavesProjectID
Class to create a GameObject, which is an object with components.
- Parameters:
name (str, optional) – Name of GameObject
parent (GameObject or None) – Parent of GameObject
- classmethod BareObject(name='GameObject')[source]¶
Create a bare GameObject with no components or attributes.
- Parameters:
name (str) – Name of the GameObject
- AddComponent(componentClass)[source]¶
Adds a component to the GameObject. If it is a transform, set GameObject’s transform to it.
- GetComponent(componentClass)[source]¶
Gets a component from the GameObject. Will return first match. For all matches, use
GameObject.GetComponents()
.
- RemoveComponent(componentClass)[source]¶
Removes the first matching component from a GameObject. To remove all matching components, use
GameObject.RemoveComponents()
.- Parameters:
componentClass (type) – Component to remove
- Raises:
ComponentException – If the GameObject doesn’t have the specified component
ComponentException – If the specified component is a Transform
- RemoveComponents(componentClass)[source]¶
Removes all matching component from a GameObject.
- Parameters:
componentClass (type) – Component to remove
- Raises:
ComponentException – If the specified component is a Transform
- class pyunity.core.HideInInspector[source]¶
Bases:
object
An attribute that should be saved when saving a project, but not shown in the Inspector of the PyUnityEditor.
- default¶
Default value (will be set to the Behaviour)
- Type:
Any
- name¶
Set when
Component.__init_subclass__
is excecuted- Type:
NoneType
- class pyunity.core.ShowInInspector[source]¶
Bases:
HideInInspector
An attribute that should be saved when saving a project, and shown in the Inspector of the PyUnityEditor.
- default¶
Default value (will be set to the Behaviour)
- Type:
Any
- class pyunity.core.Component[source]¶
Bases:
SavesProjectID
Base class for built-in components.
- gameObject¶
GameObject that the component belongs to.
- Type:
- AddComponent(component)[source]¶
Calls
GameObject.AddComponent()
on the component’s GameObject.
- GetComponent(component)[source]¶
Calls
GameObject.GetComponent()
on the component’s GameObject.
- RemoveComponent(component)[source]¶
Calls
GameObject.RemoveComponent()
on the component’s GameObject.
- GetComponents(component)[source]¶
Calls
GameObject.GetComponents()
on the component’s GameObject.
- RemoveComponents(component)[source]¶
Calls
GameObject.RemoveComponents()
on the component’s GameObject.
- property scene¶
Get the scene of the GameObject.
- class pyunity.core.SingleComponent[source]¶
Bases:
Component
Represents a component that can be added only once.
- class pyunity.core.Transform[source]¶
Bases:
SingleComponent
Class to hold data about a GameObject’s transformation.
- gameObject¶
GameObject that the component belongs to.
- Type:
- localRotation = None¶
Rotation of the Transform in local space.
- Type:
- parent = None¶
Parent of the Transform. The hierarchical tree is actually formed by the Transform, not the GameObject. Do not modify this attribute.
- Type:
Transform or None
- property position¶
Position of the Transform in world space.
- property rotation¶
Rotation of the Transform in world space.
- property localEulerAngles¶
Rotation of the Transform in local space. It is measured in degrees around x, y, and z.
- property eulerAngles¶
Rotation of the Transform in world space. It is measured in degrees around x, y, and z.
- property scale¶
Scale of the Transform in world space.
- ReparentTo(parent)[source]¶
Reparent a Transform.
- Parameters:
parent (Transform) – The parent to reparent to.
- List()[source]¶
Prints the Transform’s full path from the root, then lists the children in alphabetical order. This results in a nice list of all GameObjects.
- FullPath()[source]¶
Gets the full path of the Transform.
- Returns:
The full path of the Transform.
- Return type:
- LookAtTransform(transform)[source]¶
Face towards another transform’s position.
- Parameters:
transform (Transform) – Transform to face towards
Notes
The rotation generated may not be upright, and to fix this just use
transform.rotation.eulerAngles *= Vector3(1, 1, 0)
which will remove the Z component of the Euler angles.
- LookAtGameObject(gameObject)[source]¶
Face towards another GameObject’s position. See
Transform.LookAtTransform()
for details.- Parameters:
gameObject (GameObject) – GameObject to face towards
- LookAtPoint(vec)[source]¶
Face towards a point. See
Transform.LookAtTransform()
for details.- Parameters:
vec (Vector3) – Point to face towards
- LookInDirection(vec)[source]¶
Face in a vector direction (from origin to point). See
Transform.LookAtTransform()
for details.- Parameters:
vec (Vector3) – Direction to face in