pyunity.core module¶
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.7.1
>>> 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.core.MeshRenderer object at 0x0A929460>]
>>> 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.
Component
(transform, is_dummy=False)[source]¶ Bases:
object
Base class for built-in components.
-
gameObject
¶ GameObject that the component belongs to.
Type: GameObject
-
AddComponent
(component)[source]¶ Calls AddComponent on the component’s GameObject.
Parameters: component (Component) – Component to add. Must inherit from Component
-
GetComponent
(component)[source]¶ Calls GetComponent on the component’s GameObject.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
-
GetComponents
(component)[source]¶ Calls GetComponents on the component’s GameObject.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
-
-
class
pyunity.core.
GameObject
(name='GameObject', parent=None)[source]¶ Bases:
object
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
-
name
¶ Name of the GameObject
Type: str
-
components
¶ List of components
Type: list
-
AddComponent
(componentClass)[source]¶ Adds a component to the GameObject. If it is a transform, set GameObject’s transform to it.
Parameters: componentClass (Component) – Component to add. Must inherit from Component
-
GetComponent
(componentClass)[source]¶ Gets a component from the GameObject. Will return first match. For all matches, use GetComponents.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
Returns: The specified component, or None if the component is not found Return type: Component or None
-
GetComponents
(componentClass)[source]¶ Gets all matching components from the GameObject.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
Returns: A list of all matching components Return type: list
-
class
pyunity.core.
Light
(transform, is_dummy=False)[source]¶ Bases:
pyunity.core.SingleComponent
Component to hold data about the light in a scene.
-
intensity
¶ Intensity of light
Type: int
-
AddComponent
(component)¶ Calls AddComponent on the component’s GameObject.
Parameters: component (Component) – Component to add. Must inherit from Component
-
GetComponent
(component)¶ Calls GetComponent on the component’s GameObject.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
-
GetComponents
(component)¶ Calls GetComponents on the component’s GameObject.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
-
-
class
pyunity.core.
MeshRenderer
(transform, is_dummy=False)[source]¶ Bases:
pyunity.core.SingleComponent
Component to render a mesh at the position of a transform.
-
AddComponent
(component)¶ Calls AddComponent on the component’s GameObject.
Parameters: component (Component) – Component to add. Must inherit from Component
-
GetComponent
(component)¶ Calls GetComponent on the component’s GameObject.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
-
GetComponents
(component)¶ Calls GetComponents on the component’s GameObject.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
-
RemoveComponent
(component)¶ Calls RemoveComponent on the component’s GameObject.
Parameters: component (Component) – Component to remove. Must inherit from Component
-
-
class
pyunity.core.
SingleComponent
(transform, is_dummy=False)[source]¶ Bases:
pyunity.core.Component
Represents a component that can be added only once.
-
AddComponent
(component)¶ Calls AddComponent on the component’s GameObject.
Parameters: component (Component) – Component to add. Must inherit from Component
-
GetComponent
(component)¶ Calls GetComponent on the component’s GameObject.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
-
GetComponents
(component)¶ Calls GetComponents on the component’s GameObject.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
-
-
class
pyunity.core.
Tag
(tagNumOrName)[source]¶ Bases:
object
Class to group GameObjects together without referencing the tags.
Parameters: tagNumOrName (str or int) – Name or index of the tag
Raises: ValueError
– If there is no tag nameIndexError
– If there is no tag at the provided indexTypeError
– If the argument is not a str or int
-
tagName
¶ Tag name
Type: str
-
tag
¶ Tag index of the list of tags
Type: int
-
classmethod
AddTag
(name)[source]¶ Add a new tag to the tag list.
Parameters: name (str) – Name of the tag Returns: The tag index Return type: int
List of current tags
-
class
pyunity.core.
Transform
(transform=None)[source]¶ Bases:
pyunity.core.SingleComponent
Class to hold data about a GameObject’s transformation.
-
gameObject
¶ GameObject that the component belongs to.
Type: GameObject
-
localPosition
¶ Position of the Transform in local space.
Type: Vector3
-
localRotation
¶ Rotation of the Transform in local space.
Type: Quaternion
-
localScale
¶ Scale of the Transform in local space.
Type: Vector3
-
parent
¶ 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
-
children
¶ List of children
Type: list
-
AddComponent
(component)¶ Calls AddComponent on the component’s GameObject.
Parameters: component (Component) – Component to add. Must inherit from Component
-
FullPath
()[source]¶ Gets the full path of the Transform.
Returns: The full path of the Transform. Return type: str
-
GetComponent
(component)¶ Calls GetComponent on the component’s GameObject.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
-
GetComponents
(component)¶ Calls GetComponents on the component’s GameObject.
Parameters: componentClass (Component) – Component to get. Must inherit from Component
-
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.
-
RemoveComponent
(component)¶ Calls RemoveComponent on the component’s GameObject.
Parameters: component (Component) – Component to remove. Must inherit from Component
-
RemoveComponents
(component)¶ Calls RemoveComponents on the component’s GameObject.
Parameters: component (Component) – Component to remove. Must inherit from Component
-
ReparentTo
(parent)[source]¶ Reparent a Transform.
Parameters: parent (Transform) – The parent to reparent to.
-
eulerAngles
¶ Rotation of the Transform in world space. It is measured in degrees around x, y, and z.
-
localEulerAngles
¶ Rotation of the Transform in local space. It is measured in degrees around x, y, and z.
-
position
¶ Position of the Transform in world space.
-
rotation
¶ Rotation of the Transform in world space.
-
scale
¶ Scale of the Transform in world space.
-