Changes between Version 21 and Version 22 of PySoy Primer/Part1


Ignore:
Timestamp:
11/16/12 23:10:43 (22 months ago)
Author:
bhlegm
Comment:

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.

Legend:

Unmodified
Added
Removed
Modified
  • PySoy Primer/Part1

    v21 v22  
    22[[html(<div align="right" style="margin-top: -3em;">)]][wiki:PySoy%20Primer Table of Contents][[html(</div>)]] 
    33=== Getting Started === 
    4 '''Note: this is for trunk, soon to be beta-3''' 
     4Open 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. 
     5We 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. 
     6{{{ 
     7import soy 
     8from time import sleep 
    59 
    6 Let's jump in by opening your favorite Python shell.  If you've never done this before, just open a terminal and type "python": 
    7 {{{ 
    8 arc@sobek ~ $ python 
    9 Python 2.4.2 (#1, Mar 18 2006, 00:45:59)  
    10 [GCC 3.4.4 (Gentoo 3.4.4, ssp-3.4.4-1.0, pie-8.7.8)] on linux2 
    11 Type "help", "copyright", "credits" or "license" for more information. 
    12 >>>  
     10client = soy.Client() 
     11client.window.title = "Spinning Cube" 
     12scene = soy.scenes.Scene() 
    1313}}} 
    1414 
    15 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. 
    16  
    17 Now, let's load the PySoy module and open our 3d window: 
     15At 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. 
    1816{{{ 
    19 >>> import soy 
    20 >>> from time import * 
    21 >>> scr = soy.Screen() 
    22 >>> win = soy.Window(scr,"Test Window") 
    23  
     17if __name__ == '__main__' : 
     18        while True: 
     19                sleep(10) 
    2420}}} 
    2521 
    26 If a window opened on your screen, congratulations!  :-)  PySoy is now initialized for 3d. 
     22 
     23Run the script using python3 
     24{{{ 
     25python3 pysoy_primer.py 
     26}}} 
    2727 
    2828 
    29 === Create a Scene === 
    30 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: 
     29=== Creating our Cube === 
     30In 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. 
     31For 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. 
    3132{{{ 
    32 >>> sce = soy.scenes.Scene() # changed in Beta3 from soy.Scene() 
    33 >>>  
     33cube = soy.bodies.Box(sce) 
     34cube.position = soy.atoms.Position((0, 0, 0)) 
     35cube.size = soy.atoms.Size((1,1,1)) 
     36cube.material = soy.materials.Colored() 
     37cube.material.ambient = soy.atoms.Color('red') 
     38cube.material.diffuse = soy.atoms.Color('red') 
     39cube.material.specular = soy.atoms.Color('pink') 
    3440}}} 
    3541 
    36 ... 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. 
    37 {{{ 
    38 >>> 
    39 >>> cube = soy.shapes.Box(1, 1, 1) # Step 1: Shape 
    40 >>> mat = soy.materials.Material() # Step 2: Material 
    41 >>> mat.shininess = 5 # (Optional) Looks nicer (modifies the applied specular lighting; play with this setting to see how it affects appearance) 
    42 >>> body = soy.bodies.Body(scene=sce, model=soy.models.Shape(material=mat), shape=cube) # Step 3: Body 
    43 }}} 
     42Run 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. 
    4443 
    4544Bodies also need a mesh, but we can just assign that argument to a Shape mesh, it will automatically use our shape's geometry. 
     
    4847 
    4948=== Light, Camera, Action! === 
    50 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: 
     49To 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. 
     50We still have a pure black window and that's just not very interesting.  
    5151{{{ 
    52 >>> light = soy.bodies.lights.Light(sce) 
    53 >>> camera = soy.bodies.Camera(sce) 
    54 >>> pro = soy.widgets.Projector(win,camera=camera) 
    55 >>> 
     52light = soy.bodies.Light(scene) 
     53camera = soy.bodies.Camera(scene) 
     54pro = soy.widgets.Projector(client.window, camera) 
    5655}}} 
    5756 
     
    6362 * A Camera 
    6463 
    65 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: 
     64We 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: 
    6665{{{ 
    67 >>> camera.position = (0,0,5.0) 
    68 >>> light.position = (0.5, 1.0, 5.0) 
    69 >>> 
     66camera.position = soy.atoms.Position((0, 0, 5.0)) 
     67light.position = soy.atoms.Position((0.5, 1.0, 5.0)) 
    7068}}} 
     69Ah!  Now we can see the 'cube'! But wait, only one face!  :( 
    7170=== Cube Rotation === 
    7271 
    73 Rotation in PySoy is incredibly easy.. we just have to specify our rotation speed in our body's "rotation" attribute. 
     72Rotation in PySoy is done with the addTorque method.. we just have to specify our rotation speeds for each direction (x, y, z) 
    7473{{{ 
    75 >>> 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 
     74#Rotate the cube 1 unit in the X axis, 1 unit in the Y axis and 1 unit in the Z axis 
     75cube.addTorque(1, 1, 1) 
    7676}}} 
    77 Hit control-c on the Python shell window after you're done enjoying the spinning cube.  :-)[[BR]] 
    78 (In Windows: use exit() to leave the Python shell) 
     77Enjoy 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.  :) 
    7978 
    80 You can also see the source code in the examples directory. (SVN) 
    81  
    82 [[Image(source:/media/tutorials/primer/cubes.png)]] 
     79You can also see the source code in the examples directory.  
    8380 
    8481'''Learn how to use Blender to create your own PySoy objects in [wiki:"PySoy Primer/Part2" Part 2: Modeling a Cabin]'''