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.
Examples
To create a GameObject with 2 children, one of which has its own child, and all have MeshRenderers:
>>> from pyunity import *
Loaded window providers
Loaded config
Trying FreeGLUT as a window provider
FreeGLUT doesn't work, using GLFW
GLFW doesn't work, using Pygame
Loaded PyUnity version 0.0.1
Using window provider Pygame
>>> mat = Material((1, 0, 0))
>>> root = GameObject("Root")
>>> child1 = GameObject("Child1", root)
>>> child1.transform.position = Vector3(-2, 0, 0)
>>> renderer = child1.AddComponent(MeshRenderer)
>>> renderer.mat = mat
>>> renderer.mesh = Mesh.cube(2)
>>> child2 = GameObject("Child2", root)
>>> renderer = child2.AddComponent(MeshRenderer)
>>> renderer.mat = mat
>>> renderer.mesh = Mesh.quad(1)
>>> grandchild = GameObject("Grandchild", child2)
>>> grandchild.transform.position = Vector3(0, 5, 0)
>>> renderer = grandchild.AddComponent(MeshRenderer)
>>> renderer.mat = mat
>>> renderer.mesh = Mesh.cube(3)
>>> root.transform.List()
/Root
/Root/Child1
/Root/Child2
/Root/Child2/Grandchild
>>> child1.components
[<Transform position=<Vector3 x=-2 y=0 z=0> rotation=<Vector3 x=0 y=0 z=0> scale=<Vector3 x=1 y=1 z=1> path="/Root/Child1">, <pyunity.core.MeshRenderer object at 0x0B5E1B68>]
>>> child2.transform.children
[<Transform position=<Vector3 x=0 y=5 z=0> rotation=<Vector3 x=0 y=0 z=0> scale=<Vector3 x=1 y=1 z=1> path="/Root/Child2/Grandchild">]
-
class
pyunity.core.
Behaviour
[source]¶ Bases:
pyunity.core.Component
Base class for behaviours that can be scripted.
-
gameObject
¶ GameObject that the component belongs to.
- Type
-
-
class
pyunity.core.
Camera
[source]¶ Bases:
pyunity.core.Component
Component to hold data about the camera in a scene.
-
fov
¶ Fov in degrees measured horizontally. Defaults to 90.
- Type
int
-
near
¶ Distance of the near plane in the camera frustrum. Defaults to 0.05.
- Type
float
-
far
¶ Distance of the far plane in the camera frustrum. Defaults to 100.
- Type
float
-
clearColor
¶ Tuple of 4 floats of the clear color of the camera. Defaults to (.1, .1, .1, 1). Color mode is RGBA.
- Type
tuple
-
-
class
pyunity.core.
Component
[source]¶ Bases:
object
Base class for built-in components.
-
gameObject
¶ GameObject that the component belongs to.
- Type
-
-
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
-
parent
¶ Parent GameObject, if GameObject has one
- Type
GameObject or None
-
class
pyunity.core.
Light
[source]¶ Bases:
pyunity.core.Component
Component to hold data about the light in a scene.
-
class
pyunity.core.
Material
(color)[source]¶ Bases:
object
Class to hold data on a material.
-
color
¶ A list or tuple of 4 floats that make up a RGBA color.
- Type
list or tuple
-
-
class
pyunity.core.
MeshRenderer
[source]¶ Bases:
pyunity.core.Component
Component to render a mesh at the position of a transform.
-
move
(transform)[source]¶ Move the transformation matrix according to a transform.
- Parameters
transform (Transform) – Transform to move the transformation matrix according to.
-
render
()[source]¶ Render the mesh that the MeshRenderer has.
Notes
It loops through the trianges in the mesh, then draws them. Each triangle has the same material. When render is called, the MeshRenderer will call render on its own children, moving it accordingly. The render function assumes that the transform was applied already.
-
-
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 name
IndexError – If there is no tag at the provided index
TypeError – If the argument is not a str or int
-
tagName
¶ Tag name
- Type
str
-
tag
¶ Tag index of the list of tags
- Type
int
-
class
pyunity.core.
Transform
[source]¶ Bases:
pyunity.core.Component
Class to hold data about a GameObject’s transformation.
-
gameObject
¶ GameObject that the component belongs to.
- Type
-
parent
¶ Parent of the Transform. The hierarchical tree is actually formed by the Transform, not the GameObject.
- Type
Transform or None
-
children
¶ List of children
- Type
list
-
FullPath
()[source]¶ Gets the full path of the Transform.
- Returns
The full path of the Transform.
- Return type
str
-