Changes between Version 21 and Version 22 of PySoy Primer/Part1
- Timestamp:
- 11/16/12 23:10:43 (6 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
PySoy Primer/Part1
v21 v22 2 2 [[html(<div align="right" style="margin-top: -3em;">)]][wiki:PySoy%20Primer Table of Contents][[html(</div>)]] 3 3 === Getting Started === 4 '''Note: this is for trunk, soon to be beta-3''' 4 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. 5 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. 6 {{{ 7 import soy 8 from time import sleep 5 9 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 >>> 10 client = soy.Client() 11 client.window.title = "Spinning Cube" 12 scene = soy.scenes.Scene() 13 13 }}} 14 14 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: 15 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. 18 16 {{{ 19 >>> import soy 20 >>> from time import * 21 >>> scr = soy.Screen() 22 >>> win = soy.Window(scr,"Test Window") 23 17 if __name__ == '__main__' : 18 while True: 19 sleep(10) 24 20 }}} 25 21 26 If a window opened on your screen, congratulations! :-) PySoy is now initialized for 3d. 22 23 Run the script using python3 24 {{{ 25 python3 pysoy_primer.py 26 }}} 27 27 28 28 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 === 30 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. 31 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. 31 32 {{{ 32 >>> sce = soy.scenes.Scene() # changed in Beta3 from soy.Scene() 33 >>> 33 cube = soy.bodies.Box(sce) 34 cube.position = soy.atoms.Position((0, 0, 0)) 35 cube.size = soy.atoms.Size((1,1,1)) 36 cube.material = soy.materials.Colored() 37 cube.material.ambient = soy.atoms.Color('red') 38 cube.material.diffuse = soy.atoms.Color('red') 39 cube.material.specular = soy.atoms.Color('pink') 34 40 }}} 35 41 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 }}} 42 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. 44 43 45 44 Bodies also need a mesh, but we can just assign that argument to a Shape mesh, it will automatically use our shape's geometry. … … 48 47 49 48 === 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: 49 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. 50 We still have a pure black window and that's just not very interesting. 51 51 {{{ 52 >>> light = soy.bodies.lights.Light(sce) 53 >>> camera = soy.bodies.Camera(sce) 54 >>> pro = soy.widgets.Projector(win,camera=camera) 55 >>> 52 light = soy.bodies.Light(scene) 53 camera = soy.bodies.Camera(scene) 54 pro = soy.widgets.Projector(client.window, camera) 56 55 }}} 57 56 … … 63 62 * A Camera 64 63 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:64 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: 66 65 {{{ 67 >>> camera.position = (0,0,5.0) 68 >>> light.position = (0.5, 1.0, 5.0) 69 >>> 66 camera.position = soy.atoms.Position((0, 0, 5.0)) 67 light.position = soy.atoms.Position((0.5, 1.0, 5.0)) 70 68 }}} 69 Ah! Now we can see the 'cube'! But wait, only one face! :( 71 70 === Cube Rotation === 72 71 73 Rotation in PySoy is incredibly easy.. we just have to specify our rotation speed in our body's "rotation" attribute.72 Rotation in PySoy is done with the addTorque method.. we just have to specify our rotation speeds for each direction (x, y, z) 74 73 {{{ 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 75 cube.addTorque(1, 1, 1) 76 76 }}} 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) 77 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. :) 79 78 80 You can also see the source code in the examples directory. (SVN) 81 82 [[Image(source:/media/tutorials/primer/cubes.png)]] 79 You can also see the source code in the examples directory. 83 80 84 81 '''Learn how to use Blender to create your own PySoy objects in [wiki:"PySoy Primer/Part2" Part 2: Modeling a Cabin]'''
