Shop OBEX P1 Docs P2 Docs Learn Events
Blender > Part 000 - The Basics (Python code_ing) — Parallax Forums

Blender > Part 000 - The Basics (Python code_ing)

GarethGareth Posts: 278
edited 2014-11-09 12:47 in Robotics
Blendering your Python Code.
Here is a walkthrough showing how to implement Python scripts in Blender.

From a previous post >>>aka Iron_Man ;-) <<< you might have seen that I am using a Propeller to interact with Blender.


Blender can be used as a powerful Gui to not only display data but also control things over serial connections.

i.e. imagine flexing the 3D virtual model of your robot in blender and your "Actual" physical robot responds in the same way.

or even Vice_versa....... animation possibilities and bone rigging follows suit... :-


Blender_Python_004.png

I will be using Blender release 2.72a (most current as of the 31/10/14)
Blender has an internal Python version 3.4.1 built in .....
There are two ways accessing/writing Python scripts in Blender.
Blender_Python_001.png
The above way uses the Scripting overlay .
The default presents a grey box where you will eventually write your code....(locked by default)
...and also a Python command line (the blue text below) so this makes it easy to test command etc.
Blender_Python_003.png


The second way of entering Python script is using the Game Logic.
Yes Blender can also be used to design Game dynamics.
This is my preferred way of entering scripts
Blender_Python_000.png
Grey box right is where you will eventually enter code.
Blender_Python_002.png


Ok so now we have the working environment....lets set up for some code entry.
Below the large grey box there is a "New" selection... just press it and this creates and unlocks & opens up the text box..... Called "Text" by default.
You can also open .py programs here ..however that is another chapter.
Blender_Python_005.png

Attention :- Python code_ing is Syntax/structure orientated ... ie case sensitive , indentations/white spaces are very important.Python Code_ing begins................


Type into the grey box :-
[FONT=arial][SIZE=3]import bpy[/SIZE][/FONT]
(this is blenders "Care Pack" module and helps organise things)
Blender_Python_006.png


Project 001 :- Add and Display a Cube
If you are wondering where the heck do I find the Python code to do this.......
.... Its dead easy......
... just pretend to Add a "Cube" in the normal way you would in Blender :-
Blender_Python_007.png
as you hover over the selection a "Tool Tip" will pop up and display the relevant Python code.
In our case this is :-
[SIZE=3][FONT=arial]bpy.ops.mesh.primitive_cube_add()[/FONT][/SIZE]
This is what you type into the Python box ; I can see no way of copying the tooltip directly :-(
To Run the code "Right Click" over the box and select "Run Script"
This will add a cube to the location of Blenders 3D cursor(at the moment).
Blender_Python_008.png


Project 002 :- Add and Display a uv_sphere at location (2,2,2)
any parameters like size, segments, rotation can be added between the () at the end of the command :-
[SIZE=3][FONT=arial]bpy.ops.mesh.primitive_uv_sphere_add(location=(2,2,2))[/FONT][/SIZE]

This will place the sphere at blender measurment units 2,2,2
Blender_Python_009.png


Project 002 :- Add and Display multiple cones/spheres/cubeson on each of the axes

Paste this into the Python script box and run :-
[FONT=arial][SIZE=3]import bpy
 for a in range(1, 4):
  bpy.ops.mesh.primitive_uv_sphere_add(location=(a*3,0,0))
  bpy.ops.mesh.primitive_cube_add(location=(0,a*3,0))
  bpy.ops.mesh.primitive_cone_add(location=(0,0,a*3))[/SIZE][/FONT]

Blender_Python_010.png

This is a very basic introduction on how to use Python code to add objects and manipulate them in 3D space....
If you have ideas or suggestions let me know and I add to the ToDo Sandbox below, these command lines will work along with the code posted above in this article... Its just a way of sprouting awareness of the amazing flexibility Python offers to Blender.
Sand box code Ideas :-
  • Q:= How would i tweak a single vertex on that particular cube from 50 say cubes ?.
  • A:= bpy.data.objects["Cube"].data.vertices[4].co.x += 1.0
  • This will fling the 4th vertex 1 blender measurment unit on the x axis for object named "Cube" in the scene
Extra Reading :-
Inbuilt Python Commands :- Built-in Functions
Sign In or Register to comment.