Blendering your Python.

Here is a walkthrough showing how to implement Python scripts in 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 an Inmoov finger in blender and the "Actual" Inmoov moves its finger the same.

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

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 of accessing/writing  Python scripts in Blender.

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.

 

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

Grey box right is where you will eventually enter code.

 

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.

 

Type into the grey box :-

import bpy

(this is blenders "Care Pack" module and helps organise things)

 

Now here comes the best part........

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 :-

as you hover over the selection a "Tool Tip" will pop up and display the relevant Python code.

In our case this is :-

bpy.ops.mesh.primitive_cube_add()

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).

 

Project 002 :- Add and Display a uv_sphere at location (2,2,2)

any parameters like size, colour, rotation can be added between the () at the end of the command.

bpy.ops.mesh.primitive_uv_sphere_add(location=(2,2,2))

This will place the sphere at blender measurment units   2,2,2

 

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

Paste this into the Python script box and run
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))

hairygael

10 years 1 month ago

Nice post, it is unusual to see a Blender tuto on MRL.

How would you locate the python code if instead of adding a cube you import a stl file? This could be handy to indeed create riggs for all the InMoov joints with preview movements of positions.

Another good option to create gestures.

Indeed its possible to import stl files direct via Python code......

bpy.ops.import_mesh.stl(filepath="C:/Users/James/Desktop/InMoov/Head_for_Robot_InMoov/EyeglassV2.stl",
filter_glob="*.stl",  
files=[{"name":"EyeglassV2.stl", "name":"EyeglassV2.stl"}],
directory="C:/Users/James/Desktop/InMoov/Head_for_Robot_InMoov")