SFML community forums

Bindings - other languages => Python => Topic started by: Sonkun on June 01, 2012, 12:10:10 am

Title: python-sfml
Post by: Sonkun on June 01, 2012, 12:10:10 am
Hello, I'm opening this topic to talk about the project. If you have any questions or want to report issues, you can do it here. :)
Title: Re: python-sfml2
Post by: shackra on December 16, 2012, 05:25:08 am
I really love your bindings!

However, you still have to change the license to LGPL (I don't have any problem with this since I use GPL license for my [on development] commercial projects) and add doc strings!!
Title: Re: python-sfml2
Post by: Sonkun on January 07, 2013, 05:26:44 pm
The license is updated now, thanks.

I'm glad to hear you love them :) and by the way check out the new release 1.2 which should be announced soon.
Title: Re: python-sfml2
Post by: shackra on January 09, 2013, 01:21:58 am
btw, Do you have any project that try to provides a sort of UI for SFML?
I was thinking to build one relaying on HTML [processed with BeautifulSoup] and CSS [serialized with cssutils], using SFML as a render (like librocket)
Title: Re: python-sfml2
Post by: Sonkun on January 11, 2013, 01:30:37 pm
I do but I haven't published anything yet. If you want, we could discuss the theory in private as I've been working on them for more than two years now, you might be interested :)
Title: Re: python-sfml2
Post by: halflings on January 28, 2013, 02:50:39 pm
I can't install python-sfml2 from the ubunto repository :-/. This could be an error on my side, but could someone check if it works ?

Here's the error message I get :
"E: Impossible de trouver le paquet python-sfml2"
(can't find the package python-sfml2)

Title: Re: python-sfml2
Post by: Sonkun on January 28, 2013, 07:40:53 pm
I recently renamed all sfml packages and dropped the "2" so just remove the "2" from all package name. :)

libsfml2-dev -> libsfml-dev
python-sfml2 -> python-sfml
sfml2-examples -> sfml-example

The package repository name have also been renamed: use "sonkun/sfml-stable" for stable SFML releases (sfml2-RC included) and "sonkun/sfml-development" for the latest versions (in development).

These repositories contain works based on SFML as well such the Python bindings, sfeMovie, its Python binding and TGUI (v4.2). Soon, they will contain Aurora, Thor and SFGUI libraries.

By the way, what Ubuntu version do you use?

I wanted my Fedora packages ready before announcing this recent change, so I'm sorry to be late on this. :s

Let me know how it goes :)
Title: Re: python-sfml2
Post by: halflings on January 29, 2013, 09:34:02 am
Works great :) Noticed you did that when Ubuntu told me there was an update available for python-sfml

I'm using Ubuntu 12.10, and it seems to work great. (installed with no bugs, but I still got to check with a hello world :))
Title: Re: python-sfml2
Post by: Sonkun on January 31, 2013, 08:44:48 pm
The website is temporarily unavailable. The hard drive failed but hopefully I have backups. I hope it won't take too long...

I'm using Ubuntu 12.10, and it seems to work great. (installed with no bugs, but I still got to check with a hello world :))

Why not installing pysfml-examples package, then typing pysfml-pong ? :) Some codes are available on the website, section Examples: http://python-sfml.org/examples.html
Title: Re: python-sfml2
Post by: halflings on February 13, 2013, 09:27:35 pm
When trying to launch pysfml-pong, I get this error message:

Traceback (most recent call last):
  File "pong.py", line 16, in <module>
    import sfml as sf
  File "/usr/lib/python3.2/dist-packages/sfml/__init__.py", line 13, in <module>
    from sfml.graphics import *
ImportError: /usr/lib/python3.2/dist-packages/sfml/graphics.cpython-32mu.so: undefined symbol: _ZN2sf4Font14getDefaultFontEv


I get the same kind of error when using pysfml with Python 2.7 :

ImportError: /usr/lib/python2.7/dist-packages/sfml/graphics.so: undefined symbol: _ZN2sf4Font14getDefaultFontEv
Title: Re: python-sfml2
Post by: Sonkun on February 20, 2013, 01:27:20 am
The version of pySFML contained in this repository (sfm-stable) is 1.2 and depends on the release candidate of SFML2. You simply installed the latest version of SFML2 on top and thus overwrote the right SFML version.

To fix that, reinstall the libsfml package:
sudo apt-get install --reinstall libsfml

Be careful, you're having the wrong headers too, so you may want to reinstall libsfml-dev as well ;)
Title: Re: python-sfml
Post by: netrick on April 29, 2013, 08:19:08 pm
Are you going to update the binding to SFML 2.0 stable soon? Because afaik it's based on RC which is quite outdated right now and the function names have changed in few places as well.
Title: Re: python-sfml
Post by: Sonkun on April 29, 2013, 09:16:58 pm
I was maintaining two branches, the first based on the RC while the second one followed the latest changes of SFML2.

I'll release the next version (1.3), entirely based on the final version of SFML2 during this night (as announced on the website now). So, wait until tomorrow for installers/packages :)
Title: Re: python-sfml
Post by: Sonkun on April 30, 2013, 11:24:59 am
Sorry, for those who expects the 1.3 release I announced for today, I can't make it on time. I'm facing issues with some packages and installers, nothing important but I'm running out of time... I'll give it another try tomorrow morning.
Title: Re: python-sfml
Post by: halflings on April 30, 2013, 02:21:22 pm
Yay ! Happy to know that the Python binding is finally approaching a stable version :-) !

Good luck with the final changes. Could you please publish python-sfml on PyPi ? (so we can install it with "pip" which would be *way* easier).
Title: Re: python-sfml
Post by: netrick on May 01, 2013, 09:28:52 am
Any news on 1.3?

Also, could you provide packages for both ubuntu 12.04 (lts) and 13.04 (newest)? That would be amazing as for example I use 13.04 on desktop but 12.04 on my server and I want to write a little server in python using sfml sockets.
Title: Re: python-sfml
Post by: Sonkun on May 01, 2013, 02:35:34 pm
Yay ! Happy to know that the Python binding is finally approaching a stable version :-) !
The python bindings are relatively stable since v1.1.  The installation from source wasn't always straight forward although I did always encourage using packages and installers I provided instead.

Good luck with the final changes. Could you please publish python-sfml on PyPi ? (so we can install it with "pip" which would be *way* easier).
Thanks! The newest version bundles a C/Cython API so it has dependencies but I will look at how I can add it to pypi :)

Any news on 1.3?

Also, could you provide packages for both ubuntu 12.04 (lts) and 13.04 (newest)? That would be amazing as for example I use 13.04 on desktop but 12.04 on my server and I want to write a little server in python using sfml sockets.
As you can see here (https://launchpad.net/~sonkun/+archive/sfml-experimental/+packages) this is my 8th attempts in compiling the source package on their server. It works locally... but I have to figure out why it doesn't pass on launchpad...

I just woke up, I'm working on releasing 1.3.. so by the end of the day! :)
Title: Re: python-sfml
Post by: halflings on May 02, 2013, 04:52:46 pm
Are the packages stable now ? (and which ppa should be used: ppa:sonkun/sfml-development ? )
Title: Re: python-sfml
Post by: gaulois94 on May 02, 2013, 10:37:46 pm
Hi, please, why can't we create a sprite without a texture ? (with a default constructor like sfml) ? I want to create by default a sprite without a texture... We can't also create a default Texture without source...
Title: Re: python-sfml
Post by: Sonkun on May 03, 2013, 01:48:00 pm
Are the packages stable now ? (and which ppa should be used: ppa:sonkun/sfml-development ? )
I put a note here regarding the packages repository: http://en.sfml-dev.org/forums/index.php?topic=11338.0

Quote
Warning2: For Python users, I'm experiencing issues with packaging the latest pySFML version, so you'll be better off using the previous stable version (1.2) which is based on SFML2-RC (in sonkun/sfml-stable) But don't worry, first the API differences are small (so it won't take a long time to update your code), and second, as soon as it's done, you can easily update to SFML2 by installing sonkun/sfml-development).

It should be done soon. I've got free time this afternoon so I'll rewrite the setup.py.

Hi, please, why can't we create a sprite without a texture ? (with a default constructor like sfml) ? I want to create by default a sprite without a texture... We can't also create a default Texture without source...
Creating an empty texture would result an invalid and unusable texture, in your class constructor, do:

self.texture = None

But you're right for the sprite default constructor unable to take no argument. It'll be fixed in the next release, meantime, here's a workaround:

sprite = sf.Sprite(sf.Texture.create(1.1)) # create a sprite from a dummy texture
sprite.texture = None # remove the internal texture
Title: Re: python-sfml
Post by: gaulois94 on May 03, 2013, 04:19:37 pm
Hi again,

I have an error when I want to iterate on sf.RenderWindow.events :
AttributeError: 'code' object has no attribute 'on_resize'
I use the last source update...

Thank you.

édit : Sorry again, but in the source, why sf.RenderWindow isn't inheritate from sf.RenderTarget ?  I need this... Thank you again :)
Title: Re: python-sfml
Post by: Sonkun on May 03, 2013, 04:45:49 pm
Hi again,

I have an error when I want to iterate on sf.RenderWindow.events :
AttributeError: 'code' object has no attribute 'on_resize'
I use the last source update...

Thank you.

for event in window.events: print(event) works for me. What branch did you compile ?
Title: Re: python-sfml
Post by: gaulois94 on May 03, 2013, 04:48:23 pm
I'm on archlinux and then I use the package python-sfml-git .
Please, see my last edit :)
And for the error, with a sf.RenderWindow basic (without inheritance) I don't have this error.

My code :
mport sfml as sf
from Render import Render
from EventManager import EventManager
from Image import Image

class Window(Render, sf.RenderWindow):
        """This class create a window
        Cette classe créé une fenêtre"""


        def __init__(self, videoMode, title, parent=0, framerateLimit=60,\
                        backgroundColor = sf.Color.BLACK, backgroundImage = Image()):

                sf.RenderWindow.__init__(self, videoMode, title)
                Render.__init__(self, parent, sf.Rectangle(sf.Vector2(),\
                                videoMode.size), backgroundColor, backgroundImage)

                self._isStaticToView = False
                self.position = (0,0)
                self.framerate_limit = framerateLimit
                self._event = EventManager(self)
                #self.resetView()
                #self.clear(backgroundColor)

        def updateFocus(self):
                return;

        def update(self):
                """Update the Window. It Update all event, and update framerate.
                It Draw and display also all widgets child"""

                if self.isDrawing:
                        self._event.update()
                        self._framerate = 1/(self.event.elapsedTime*10**-6)

                        if self.event.isResize:
                                Widget.resizeWidget(self.event.defaultWindowSize,\
                                                self.event.newWindowSize)
       
                        Widget.widgetFocus =  None
                        Updatable._focusIsChecked = False

                        self.clear(self.backgroundColor)

                        if self.event.isMouseInRect(self.rectOnScreen):
                                Updatable.updateFocus(self)
                        Widget.update(self, self)
                        self.display()

       #It's not the most importante

        def getEventFromRootParent(self):
                return self._event

        def _setSize(self, size):
                Widget.size = size
                sf.RenderWindow.size = size
                self._imageBackground.scale = sf.Vector2f(\
                                size.x/self._imageBackground.local_bounds,\
                                size.y/self._imageBackground.local_bounds)

        def getPosOnScreen(self, *args):
                return sf.Vector2f(0,0)

        def setPos(self, position, *args):
                Widget.setPos(position, False)
                self.position = position

        def _setView(self,view):
                sf.RenderWindow.view.__set__(self, view)
                Render._setView(self,view)
       
        def _resizeWidget(self, pos, size):
                self._size = size
       
        size = Widget.size
        event = property(lambda self:self._event)
        framerate = property(lambda self:1/self._event.elapsedTime*0.001)
        setPosOnScreen = setPos

and Render (abstrate class)
import sfml as sf
from Image import Image
from Widget import Widget
from copy import copy
import functions

class Render(Widget):
        """Basic virtual class for all Render's class"""

        def __init__(self, parent, rect, backgroundColor=sf.Color.BLACK,\
                        title=str(), backgroundImage=Image()):
                Widget.__init__(self,parent, rect)
                self.canFocus = False
                self.backgroundColor = backgroundColor

        def show(self, render):
                raise NotImplementedError

        def moveView(self, move):
                newView = self.view
                newView.move(move.x, move.y)
                self.view = newView

        def zoomView(self,zoom):
                newView = self.view
                newView.zoom(zoom)
                self.view = newView

        def resizeView(self, size):
                self.view.size(size)
                self.view = self.view

        def resetView(self):
                self.view = self.default_view

        def setViewSize(self, size):
                newView = self.view
                newView.size = size
                self.view = newView

        def setViewPosition(self, pos):
                viewCopy = self.view
                viewCopy.center = sf.Vector2(pos - self.view.size / 2)
                self.view = viewCopy

        def setViewport(self, viewport):
                viewCopy = self.view
                viewCopy.viewport = viewport
                self.view = viewCopy

        def getViewPosition(self):
                return sf.Vector2(self.view.center[0],self.view.center[1]) + \
                                sf.Vector2(self.view.size[0],self.view.size[1]) / 2

        def getSommeViewPosition(self):
                render = Widget.getRender()
                rect = self.getViewRectWithZoom()
                if isinstance(render,Render):
                        return sf.Vector2(render.getSommeViewPosition() +\
                                        sf.Vector2(rect.left, rect.top))
                else:
                        return sf.Vector2(rect.left, rect.top)

        def getViewRect(self):
                return sf.Rectangle(self.getViewPosition(), self.view.size)

        def getViewRectWithZoom(self):
                viewRect = self.getViewRect()
                scale = self.getViewScale()
                return sf.Rectangle(\
                                sf.Vector2(viewRect.left*scale.left, viewRect.top*scale.top),\
                                sf.Vector2(viewRect.width * scale.width,\
                                        viewRect.height * scale.height))

        def getRender(self):
                return self

        def isInView(self,rect):
                return functions.rectCollision(rect,self.view.viewport)

        def getViewScale(self):
                return sf.Rectangle(sf.Vector2(self.viewport.left*self.view.size.x+1,\
                                self.viewport.top * self.view.size.y + 1),\
                                sf.Vector2(self.viewport.width / \
                                        (self.view.size.x / self.size.x),\
                                        self.viewport.height / (self.view.size.y / self.size.y)))

        def _setBackgroundImage(self, backgroundImage):
                self._backgroundImage = backgroundImage
                self._backgroundImage.pos = sf.Vector2(0,0)
                self._backgroundImage.size = self.size
                self._backgroundImage.setParent(self.parent, 0)
       
        def _setView(self, view):
                back = self.getViewRectWithZoom()
                back = sf.Vector2(back.left, back.top)
                sf.RenderTarget.view.__set__(self,view)

                for child in self._child:
                        if isinstance(child,Widget) and child.isStaticToView:
                                child.setPos(child.pos - back)

        def _setViewport(self, rect):
                newView = copy(self.view)
                newView.viewport = rect
                self.view = newView

        def _setTitle(self, title):
                self._title  = title

        size = Widget.size
        backgroundImage = property(lambda self:self._backgroundImage,\
                        lambda self,image: self._setBackgroundImage(image))
        viewport = property(\
                        lambda self:sf.RenderTarget.view.__get__(self).viewport,\
                        lambda self,rect : self._setViewport(rect))
        title = property(lambda self:self._title,\
                        lambda self,title : self._setTitle(title))
       
        view = property(lambda self:sf.RenderTarget.view.__get__(self),\
                        lambda self,view : self._setView(view))
 

The most importante here are : constructors, updates functions and properties.
Title: Re: python-sfml
Post by: Sonkun on May 03, 2013, 06:14:38 pm
Ohh, an ArchLinux user :p I'm afraid these scripts aren't updated...(I need to contact their author). Officially, version 1.3 isn't released yet, I just made the Windows installers and uploaded a snapshot of the website as the main audience is Windows users.

RenderWindow doens't inherit from RenderTarget because of a Python limitation. A pure Python class can't inherit from two extension Python classes at the same time. Here both, sf.Window and sf.RenderTarget are extension classes.

Are you having issues with isinstance or issubclass functions ? I recently read the abc module documentation, I think it could solve this issue.

And sorry, I don't get it, what's wrong with your code ? (then I'll read it^^)
Title: Re: python-sfml
Post by: gaulois94 on May 03, 2013, 06:49:06 pm
But I nead this inheritance... Indeed, in the "Render" class, I write on the sf.RenderTarget.view, and if the Windows doesn't have the same view of sf.RenderTarget....

But the Leonard binding have this inheritance, why ?

And sorry for this question but what are the differences between pur and extension python classes

edit : Ah.... Leonard choose the sf.RenderTarget as root class for sf.RenderWindow. I think is better  (because  sf.Window is only useful for sf.RenderWindow (when sf.RenderTarget is useful for sf.RenderTexture and Window). And the script for archlinux is good : it take the last source, compile it and install.

edit2 : you can let this way for inheritance : I find a way with super :) .

Please, can you teste this code for me ? The script is "test.py" writing with python3 . Thank you :)



[attachment deleted by admin]
Title: Re: python-sfml
Post by: Sonkun on May 04, 2013, 01:20:19 am
I can't test this code since you import stuff you don't provide. Avoid posting long code and don't hesitate to mail me to send me code you want me to test.

Also, prefer opening a new topic when you have an issue instead of putting everything here because first, it's meant for giving your opinions on the bindings, second, it help others search for similar issues (the subject summarizing your issue and being referenced by search engines).

Quote
And sorry for this question but what are the differences between pur and extension python classes .
A extension class is a Python class written in C unlike pure Python classes which are written in Python.

Quote
edit : Ah.... Leonard choose the sf.RenderTarget as root class for sf.RenderWindow. I think is better  (because  sf.Window is only useful for sf.RenderWindow (when sf.RenderTarget is useful for sf.RenderTexture and Window).
How is sf.RenderTarget a better choice ?  Actually the reason is because he doesn't implement sf.Window.

Quote
And the script for archlinux is good : it take the last source, compile it and install.
Oh... :) Then I'm glad!

I speak French too so you can carry on posting on the French forum (if you want of course :])
Title: Re: python-sfml
Post by: halflings on May 04, 2013, 02:01:39 am
Is it safe to switch to sfml-development now ? :-p
Title: Re: python-sfml
Post by: Sonkun on May 04, 2013, 02:53:43 am
Is it safe to switch to sfml-development now ? :-p

Very very soon! :p I just found out package "cython3" is available for Raring Ringtail so I'll take its source package, and will distribute for older versions which should remove my issue. When targeting Python 3, I relied on Cython installed in Python 2 and the setup.py became hard to understand and maintain because of other stuff I added to make it flexible (platform-specific behavior, cython needed or not, cython available, cython available in other Python version).

Now, I know where I'm going: Cython IS mandatory and must be installed in the targeted Python version. :)
Title: Re: python-sfml
Post by: gaulois94 on May 04, 2013, 11:02:38 am
The sources were in attachments x) (src.zip...).
Well sorry for the desagrements and for my bad english (I post here because this forum is more active than the french forum ;) ).
I will open a new topic :)
Title: Re: python-sfml
Post by: Sonkun on May 07, 2013, 09:59:53 am
Sorry, still no Debian/Ubuntu packages available... I rewrote the setup.py and I already tried a thousand time to package but it still gives weird errors. I'm following both Debian and Ubuntu python packaging policy and their helper scripts don't install things at the right place...