A Spinning Cube

Table of Contents?

Getting Started

Note: this is for trunk, soon to be beta-3

Let's jump in by opening your favorite Python shell. If you've never done this before, just open a terminal and type "python":

arc@sobek ~ $ python
Python 2.4.2 (#1, Mar 18 2006, 00:45:59) 
[GCC 3.4.4 (Gentoo 3.4.4, ssp-3.4.4-1.0, pie-8.7.8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

This ">>>" is the prompt in Python. For future reference, if you see it as a "…" it means you're entered a multi-line statement. If you have done so by accident, simply type control-c to break out of it. In the following example code, whenever a line begins with ">>>" you should type in whatever follows it in your Python shell.

Now, let's load the PySoy module and open our 3d window:

>>> import soy
>>> scr = soy.Screen()
>>> win = soy.Window(scr,"Test Window")

If a window opened on your screen, congratulations! :-) PySoy is now initialized for 3d.

Create a Scene

Now that PySoy is initialized, we need a 3d space to put things into. We call this space our "scene". To create a scene, click back on your Python shell and type:

>>> sce = soy.scenes.Scene() # changed in Beta3 from soy.Scene()
>>> 

… and, just so our scene isn't a void of empty space, let's drop a cube into it. To make a cube, we need 3 things, a shape, a body and a material.

>>>
>>> cube = soy.shapes.Box(1,1,1) # Step 1: Shape
>>> mat = soy.materials.Material()# Step 2: Material
>>> mat.shininess = 5 # Looks nicer :D
>>> body = soy.bodies.Body(scene=sce,mesh=soy.meshes.Shape(mat),shape=cube) # Step 3: Body

Bodies also need a mesh, but we can just assign that argument to a Shape mesh, it will automatically use our shape's geometry.

Most PySoy objects take their parent-space as their first argument. We'll see other parents and how to change them later.

Light, Camera, Action!

We still have a pure black window and that's just not very interesting. Next we're going to put a light and camera into our scene:

>>> light = soy.bodies.lights.Light(sce)
>>> camera = soy.bodies.Camera(sce)
>>> pro = soy.widgets.Projector(win,camera=camera)
>>>

This last line attaches the "output" of the camera, which exists in the 3d scene, to the black window you have open. This way, what the camera "sees" is what will be drawn inside the window. Anything inside your scene now is rendered and outputted to the Window.

Now, let's think about these three things in our scene:

  • A Cube
  • A Light
  • A Camera

We simply dropped them into the scene, never changing their location or telling them to "do" anything but exist. Right now the camera and the light are in the center of the cube, so we'll need to change that in order to see it. Let's move the camera back some and position the light so that it shines down on the cube from above the camera:

>>> camera.position = (0,0,5.0)
>>> light.position = (0.5, 1.0, 5.0)
>>>

Cube Rotation

Rotation in PySoy is incredibly easy.. we just have to specify our rotation speed in our body's "rotation" attribute.

>>> body.rotation = (1,1,1) # Rotate the cube 1 unit in the X axis,1 unit in the Y axis and 1 unit in the Z axis

Hit control-c on the Python shell window after you're done enjoying the spinning cube. :-)
(In Windows: use exit() to leave the Python shell)

You can also see the source code in the examples directory. (SVN)

source:/media/tutorials/primer/cubes.png

Learn how to use Blender to create your own PySoy objects in Part 2: Modeling a Cabin