Version 22 (modified by bhlegm, 17 months ago) (diff)

This is my current understanding. If it needs correcting, please do so I can see what actually is. Also, I don't know how to 'run' the .py files except from my terminal, and am not really familiar with the python shell, which is why this primer was rewritten like this. Any changes to make this better appreciated.

A Spinning Cube

Table of Contents?

Getting Started

Open a text editor and start a new file. In this tutorial, save it as pysoy_primer.py. Import the soy module and the sleep method from the built-in time module in Python. We will also make a Client. In this client, there is a Window. This is the window that will show up. You will see when it appears that the title you set will be the title of the window. Also, we make a scene to put our objects in. This scene is the 3-d space in which we will put things in.

import soy
from time import sleep

client = soy.Client()
client.window.title = "Spinning Cube"
scene = soy.scenes.Scene()

At the end of the file, add the following lines. This is so that everything in your script continues to exist. This should remain at the end of your file at all times.

if __name__ == '__main__' :
	while True:
		sleep(10)

Run the script using python3

python3 pysoy_primer.py

Creating our Cube

In PySoy, 3d shapes are known as Bodies. We will create an instance of a subclass of Bodies known as Box, placing it in the scene we created earlier. For now, we will place the cube in the center of the window. This is soy.atoms.Position((0, 0, 0)). The parameters of Position represent x, y, and z axes respectively. We set the size to 1 unit of length, width, and thickness.

cube = soy.bodies.Box(sce)
cube.position = soy.atoms.Position((0, 0, 0))
cube.size = soy.atoms.Size((1,1,1))
cube.material = soy.materials.Colored()
cube.material.ambient = soy.atoms.Color('red')
cube.material.diffuse = soy.atoms.Color('red')
cube.material.specular = soy.atoms.Color('pink')

Run spin_cube.py again. Don't worry if your cube does not show up in your window yet. We simply know it exists in the blackness that is your scene.

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!

To make our cube show up, we need light to see it, a camera to watch it, and a projector to project what the camera sees onto the window. We still have a pure black window and that's just not very interesting.

light = soy.bodies.Light(scene)
camera = soy.bodies.Camera(scene)
pro = soy.widgets.Projector(client.window, 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 (their default position is also (0, 0, 0), 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 = soy.atoms.Position((0, 0, 5.0))
light.position = soy.atoms.Position((0.5, 1.0, 5.0))

Ah! Now we can see the 'cube'! But wait, only one face! :(

Cube Rotation

Rotation in PySoy is done with the addTorque method.. we just have to specify our rotation speeds for each direction (x, y, z)

#Rotate the cube 1 unit in the X axis, 1 unit in the Y axis and 1 unit in the Z axis
cube.addTorque(1, 1, 1)

Enjoy the slowly spinning cube. :) It will take a few seconds for you to realize the amazingness of its three dimensions. Set these torque values higher and watch your cube spin faster. :)

You can also see the source code in the examples directory.

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

Attachments