libraries and packages

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
Noam Ritov
Posts: 2
Joined: Tue Nov 15, 2011 2:14 pm

libraries and packages

Post by Noam Ritov » Tue Nov 15, 2011 2:23 pm

Hi

Ok I have some problems.

I not that new to blender and not new to programming, but more electronics programming.

My problem is that I want to use the serial library in blender game engine.

I know how to use it outside of blender with python. But I don't understand how you get it to work in the blander game engine? how do you install a library in the game engine?!

The pyserial has a nice installer for win and python... But way can I get it in to the game engine? why do I have a folder of Python2.7 and then in Blender different ones...? where dose the Pyserial library need to go to?

As you can see I am kind of frustrated from all this story, have been using along time on that and trying like 3 different tutorial out there... but none of them worked...

So plz help me...

Thank...

stiv
Posts: 3645
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv » Tue Nov 15, 2011 5:12 pm

My problem is that I want to use the serial library in blender game engine.
disclaimer: I have not used this in the game engine, but...

The general rules for using a Python module with Blender are:

* Your module and Blender MUST use the same version of Python. For new Blender, this means Python 3.2. New Blender comes with this version and uses its own copy.

* Your module MUST be in the module classpath (sys.path). If you have installed a module on your system, you can either add your system site-packages folder to sys.path inside Blender, or you can set the PYTHONPATH environment variable outside Blender *before* you start Blender.

* Did I mention your module must use Python 3.2 for new Blender? (old Blender uses whatever version of Python it asks for at startup)

Noam Ritov
Posts: 2
Joined: Tue Nov 15, 2011 2:14 pm

Post by Noam Ritov » Tue Nov 15, 2011 10:48 pm

Thanks.

This is a super explanation.

But maybe I am more a noob to this as I think.

I have installed Blender 2.60
Python 3.2
pyserial to python 3.2

I can also see that blender loads python 2.6 from it folder when it starts (in the system consul) and in this folder there is in lib a folder called serial....

So I really don't understand what I am doing wrong, it can be that hard to add lib files to blender game engine, if it is so easy to add them to python (out of blender)...

I have blender installed in C:\programfile\blemder...
and Python 3.2 and 2.7 at C:\python3.2 and C:\python2.7

Both python, when I run them outside blender work perfectly with pyserial lib (import serial) But when I try to do the same in blender it doesn't work...

What to do?

Plz more help :) ...

stiv
Posts: 3645
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv » Wed Nov 16, 2011 5:40 am

I can also see that blender loads python 2.6 from it folder when it starts (in the system consul) and in this folder there is in lib a folder called serial....
Python 2.6? Something strange is going one. The latest blender (2.60) is built with Python 3.2

IFF you have installed pyserial in Python 3.2, inside of Blender, do this:

Code: Select all

import sys
sys.path.append(  the path to your python 3.2 site-packages )
# if you did that right, you can
print sys.path  # and see the correct path

import serial

lalu
Posts: 3
Joined: Wed Nov 16, 2011 7:56 pm

Post by lalu » Wed Nov 16, 2011 8:13 pm

Hi,

Have you made any progress?
I'm also trying to get pySerial working with Blender 2.6. I found that there is not really tutorials about this topic for the new blender version. I have used blender about one month and before that I have done a little bit embedded programming. I hope we will get this serial port communication working. :)

SamBushman
Posts: 18
Joined: Tue Jun 07, 2011 4:58 pm

Post by SamBushman » Wed Nov 16, 2011 8:56 pm

The way I got PySerial running on Blender 2.5x and 2.6 (but not 64-Bit :( ) was to perform the following steps (Assuming you already have a working version of the pySerial library for Python 3k installed):

1. Go into your python directory and find the site-packages folder (mine is located at "C:\Python32\Lib\site-packages".
2. In that folder you should see a folder called "serial" and a *.egg-info file (mine was called "pyserial_py3k-2.5-py3.1.egg-info"). Copy that folder and file.
3. Now go to Blender's python interpreter site-packages folder. As an example of where it could be mine was located at "C:\Program Files\Blender Foundation\Blender\2.60\python\lib\site-packages". Paste the copied file and folder into this directory.
4. If you are running serial commands from inside Blender (using the built-in terminal) you should then be able to "import serial" and be on your way. If you are writing a script to be executed in Blender, then you must import the "site" module in order to find the serial module. You would then "import site" before "import serial". From there you should be golden.

I'm sure there is are cleaner ways of performing this installation, but I monkeyed around to get this method working, automated the installation, and have not had issues since. If it isn't broken, don't fix it :)

At this time I am not able to get a 64-bit build of pySerial to run successfully in blender 2.5x - 2.6x. This is due to an odd bug in pySerial 2.5 64-bit build (see this site for more info: http://sourceforge.net/tracker/?func=de ... %5D2921959 ). Since no earlier version of pySerial was ported to Python 3k, I cannot at this time get 64-bit blender to play nice with pySerial :(

I don't recall if you need the egg-info file or not, so if your inclined for experimentation go ahead and remove the file from blender's site-packages directory once you get everything working and see if anything broke.

Also optionally, if you do not have pySerial installed in a python 3k interpreter currently you can always build the module from source (which will produce the site-packages folder) and perform the same copy-paste method detailed above.

I hope this helps. Cheers and good luck,
Sam Bushman

lalu
Posts: 3
Joined: Wed Nov 16, 2011 7:56 pm

Post by lalu » Sat Nov 19, 2011 8:45 am

Hi,

Thank you for precise answer. That was exactly what I needed :D
Now I managed to install PySerial in the Blender 2.6 with your advises. At the moment I'm able to control my device through serial port using built-in-terminal. By the way, I have to "import site" also in terminal before "importing serial".
Now I still need to find a way to open the serial port in script globally, so I don't have to open and close the port every time the script is run.

SamBushman
Posts: 18
Joined: Tue Jun 07, 2011 4:58 pm

Post by SamBushman » Sat Nov 19, 2011 9:14 am

Could you make an "addon" for Blender that builds the needed serial port and registers a reference to the object in Blender's global variables? If you did this then you could "enable" the addon, thus make the serial port, then use your various in-application scripts to reference that same serial port.

lalu
Posts: 3
Joined: Wed Nov 16, 2011 7:56 pm

Post by lalu » Sun Nov 20, 2011 4:46 pm

Hi,
Thanks for the reply again. Probably your method works well also.
Anyway, I ended up into this kind of solution.

I run a initialization script once. In that script I define the serialport object as global variable using GameLogic. Then I have an other script where I'm sending data.


###############################
Initialize.py

import site
import serial
import GameLogic
from time import sleep

#Open com port 5 at baudrate 38400
GameLogic.ser = serial.Serial(4,38400)
sleep(2) #sleeps for 2 seconds. Wait until the connection is
#established with microcontroller

#############################

Send.py

import site
import serial
import GameLogic

GameLogic.ser.write((chr(x).encode('utf-8'))
#x is some integer

############################

I hope this helps someone :)

SamBushman
Posts: 18
Joined: Tue Jun 07, 2011 4:58 pm

Just an addendum to my earlier statements regarding 64-bit

Post by SamBushman » Mon Dec 05, 2011 10:14 pm

Since my last post I have found that a 2.6 version of Pyserial has been released (http://pypi.python.org/pypi/pyserial). This link go to a download page for the source files for 2.6. I have tried and confirmed that Pyserial 2.6 does work with 64-Bit Python and have used it in Blender 2.6 64-Bit.

So if anyone out there really wants to do serial communication in 64 Bit blender, have at it!

Cheers,
Sam Bushman

LukasK
Posts: 1
Joined: Thu Apr 11, 2013 12:53 am

Post by LukasK » Thu Apr 11, 2013 1:08 am

Hello,

i can`t import the pyserial module inside of Blender. I have installed Python 3.2 on my Mac OS X 10.5.8 and pySerial 2.6. Both work perfectly from the Terminal. I placed the serial folder inside the python module path of Blender but when i try to run >>>import serial from the blender python console i get this error.
Image
Any Idea of what I am doing wrong?
Thank you very much!
Lukas

stiv
Posts: 3645
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv » Thu Apr 11, 2013 4:25 pm

Any Idea of what I am doing wrong?
* using unreadable red text

* doing 'import *'

* just a guess, but I suspect you have some junk in your pyserial folder, either an extra file or something corrupt. Check that exact file and line mentioned in the exception msg.

nestanqueiro
Posts: 4
Joined: Thu Dec 12, 2002 12:12 pm

Post by nestanqueiro » Tue Apr 16, 2013 6:56 pm

try 3 different things:
first, open blender's internal console and import it from there, then check to see if you still get errors. In linux i keep getting "ImportError: No module named serial" until I import it in blenders console.
Then try launching blender as superuser... in linux "com port" is blocked for regular users, and OSX is linux based... if this is the problem you have to add yourself to a group that has access to the com port...
Lastly, there are unofficial packages for that particular lib, I had that problem with win64 and only solved it with the unofficial pyserial.
google it, it's quite easy to find.

hope any of this helps you

Nuno Estanqueiro

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest