Link to home
Start Free TrialLog in
Avatar of Mario Zio
Mario ZioFlag for Italy

asked on

python script to extracts all the shape key related information out of blender character into JSON format

Hello to everybody. I'm interested to learn how to make a facial animation using WebGL + Blender + Python. I've found a nice WebGL animation and I've mirrored the files from
the github rep located at this address :

https://github.com/antonpantev/webgl-shape-keys

I would like to understand how it works. To do it I've tried to change the blender character that the author has used with this :

http://www.blendswap.com/blends/view/68458

To do this I need to understand how works the export_shape_keys python script that he wrote to extracts all the shape key related information out of a blender character into JSON format. When I run the script on my Ubuntu 13.04 terminal window I see that the mouse cursor changes appearance because it gets ready to accept a mouse click. When I click somewhere on the screen the script ends giving these errors :

root@mario-ThinkCentre-XXXX:/home/marietto/Scrivania/taxi_driver#
./export_shape_keys.py
./export_shape_keys.py: line 4: OUTPUT_DIRECTORY: command not found
./export_shape_keys.py: line 5: PRECISION: command not found
./export_shape_keys.py: line 7: syntax error near unexpected token "("
./export_shape_keys.py: line 7: `def getTextureCoords(texture, fIndex,
vInFIndex):'

can someone help me to fix them ? Again,on the beginning of the script there are these two lines :

import bpy
import os

I've changed the OUTPUT_DIRECTORY parameter with this :

OUTPUT_DIRECTORY = /home/marietto/Scrivania
PRECISION = 6

but the errors happen again. What's the meaning of bpy and os ? Are they libraries ? Where can I find them ? When the mouse cursor change appearance and I click on the screen,two files called bpy and os are produced. I don't know which kind of files they are and I don't know which kind of application can open them.
Avatar of Mario Zio
Mario Zio
Flag of Italy image

ASKER

I've attached the export_shape_keys python script....
export-shape-keys.py
Avatar of pepr
pepr

I do not know the framework/environment... How exactly you launch the script? Does it contain the #! info at the first line? Or do you explicitly launch it via python? Or it is launched from another Python script?

Definitely, the path is a string and must be enclosed or in single or in double quotes like this:
OUTPUT_DIRECTORY = '/home/marietto/Scrivania'

Open in new window

or
OUTPUT_DIRECTORY = "/home/marietto/Scrivania"

Open in new window

(You can choose -- both alternatives produce exactly the same result.) The PRECISION variable is OK. Integer values are used this way also in Python.

However, if the script is interpreted by a non-Python interpreter, anything could happen.
I launch the script like this :

root@mario-ThinkCentre-XXXX:/home/marietto/Scrivania/taxi_driver# ./export_shape_keys.py
./export_shape_keys.py: line 4: OUTPUT_DIRECTORY: command not found
./export_shape_keys.py: line 5: PRECISION: command not found
./export_shape_keys.py: line 7: syntax error near unexpected token "("
./export_shape_keys.py: line 7: `def getTextureCoords(texture, fIndex,
vInFIndex):'

Check the script by yourself. I've attached it on the previous message. What's the meaning of bpy and os ? Are they libraries ? Where can I find them ? When the mouse cursor change appearance and I click on the screen,two files called bpy and os are produced. I don't know which kind of files they are and I don't know which kind of application can open them.
The os is a standard module. You can find its file in your python/Lib/os.py. When importing, the .py extension is not used in the source. Importing it means that you can use its functionality in your own code.

The bpy is the Blender module for Python. It is not the part of the standard Python distribution. You have to download the neccessary files and install them (probably from here http://www.blender.org/documentation/blender_python_api_2_59_0/info_overview.html).

On launching your export_shape_keys.py, it seems that the script was made executable via chmod. However, it seems it has no #! first line inside. I am not sure but I guess it tries to interpret the conten by your bash or another Unix shell. Or you have to add the first line like #! /usr/bin/env python, or you can launch it via explicitly passing the python script to the Python interpreter on command line:
python ./export_shape_keys.py

Open in new window

What should be the goal of this script ? Maybe I miss something. I can tell you what I would like that it did,please tell me if it does. I would like that it was able to generate a file containing all the coordinates of a blender character part,like the one that I have attached here. It contains the coordinates of this blender character's eye :

http://www.blendswap.com/blends/view/41157

Anton Pantev has used it to build the WebGL facial animation that you can see here :

http://www.antonpantev.com/shapekeys/
Eye.001-0.js
.
I have no experience with the Blender tool. I can help you with Python. Anyway, the Eye.001-0.js is apparently generated javascript that is used for storing the data. The format is called JSON (JavaScript Object Notation), and it is used for data interchange. You can think about it as about a text file that is capable to capture identifiers, data, and the data types. (Another alternative is a XML file.)

In Python, there is the json standard module in Python. Similarly, there are the like libraries for other programming languages. This way, the format that looks like JavaScript-only is actually the form to transfer the data reliably not only over the network, but also from a programming language to another programming language.

Otherwise, JSON is not directly related to Blender, and I do not know the details of Blender. I do not know how the data for the Eye.001-0.js was generated by the Blender. Definitely, Blender generated the data somehow, and then it used a JSON library to write the data in the Eye.001-0.js form to the file.
Yes. Here you can find another version of the same kind of script :

https://gist.github.com/paulkaplan/5209228

I tried to run it,but it didn't work :

root@mario-ThinkCentre-XXXX:/home/marietto/Scrivania# ./shapeKeysToJSON.py
from: can't read /var/mail/json
./shapeKeysToJSON.py: riga 9: syntax error near unxeptected token "("
./shapeKeysToJSON.py: riga 9: `encoder.FLOAT_REPR = lambda o: format(o, '.15g')'

Again,as before,two files are produced when I click on the screen : bpy and json. But I don't see any file containing the coordinates expressed with numbers that I need to use with WebGL...
root@mario-ThinkCentre-XXXX:/media/marietto/09274c80-4a49-4f4f-9e2e-83c4a5578a04/Animazione/Tools/Python# python ./shapeKeysToJSON.py
Traceback (most recent call last):
  File "./shapeKeysToJSON.py", line 3, in <module>
    import bpy
ImportError: No module named bpy
The last two lines say the reason. You have to install the Python support for the Blender or possibly also the Blender itself. I cannot help you with that, as I have no experience with Blender. Study the documentation http://www.blender.org/documentation/blender_python_api_2_59_0/info_overview.html

One more thing, your last comment also proved that you launched Python scripts the wrong way earlier. Or you must launch it via explicit python command, or you must add the above mentioned #! line into the script.
What about this script ?

http://code.google.com/p/blender-webgl-exporter/downloads/list

I ran it and it didn't work,but it seems to me that the errors aren't caused by the same reasons as before.
Schermata.png
Schermata-1.png
WebGLExport.py
Well, the last script suggests that there is the executable named BPY that is used for interpretation of the script. It suggests that Blender has a Python interpreter embedded inside of its own code. At least you know how the scripts should be launched. You should probably launch it explicitly as BPY the_script.py instead of python the_script.py.
No. This is not the solution. I think that some kind of PYTHON PATH environment variable is missing....

root@mario-ThinkCentre-XXXX:/media/marietto/09274c80-4a49-4f4f-9e2e-83c4a5578a04/Animazione/Esempi/blender-webgl-exporter# python ./WebGLExport.py
Traceback (most recent call last):
  File "./WebGLExport.py", line 41, in <module>
    import Blender
ImportError: No module named Blender

root@mario-ThinkCentre-XXXX:/media/marietto/09274c80-4a49-4f4f-9e2e-83c4a5578a04/Animazione/Esempi/blender-webgl-exporter# BPY ./WebGLExport.py
BPY: comando non trovato
root@mario-ThinkCentre-XXXX:/media/marietto/09274c80-4a49-4f4f-9e2e-83c4a5578a04/Animazione/Esempi/blender-webgl-exporter#
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have no BPY file. But I have a folder called bpy here :

/media/marietto/09274c80-4a49-4f4f-9e2e-83c4a5578a04/Animazione/Tools/Blender/blender-2.66a/2.66/scripts/modules/bpy

this is the content of the bpy folder :

__init__.py  ops.py  path.py  __pycache__  utils.py
I tried to run the script directly inside the blender modules folder :

root@mario-ThinkCentre-XXXX:/media/marietto/09274c80-4a49-4f4f-9e2e-83c4a5578a04/Animazione/Tools/Blender/blender-2.66a/2.66/scripts/modules# python WebGLExport.py
Traceback (most recent call last):
  File "WebGLExport.py", line 41, in <module>
    import Blender
ImportError: No module named Blender


read here :

http://www.blender.org/forum/viewtopic.php?t=18931&sid=2d977cee0f1a085f7390fa87fc37899c

he says : "The Blender Python API has changed, there is indeed no Blender module anymore, nor any of the modules you try to import from it. See the new Blender Python API instead..."  

Posted: Fri Jan 14, 2011 6:37 pm
Maybe this is the updated version ?

https://github.com/lubosz/blender-glge-exporter