omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    How do I add textures to sprites?

    Pythonista
    5
    17
    9064
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • heyguy4
      heyguy4 last edited by

      I have a sprite called spriteName. Whenever I type something like.

      self.spriteName.texture = ('plc:Gem_Blue')
      

      or import my own image into my own folder called Textures and use

      self.spriteName.texture = ('Textures/IMG_0469.JPG')
      

      They both result in the error "TypeError: Expected a Texture Object"
      I don't know what I'm doing wrong.

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        A TypeError usually gives you a hint that you are using the wrong type, and the message often gives details of what was expected instead. The error gives you a hint that texture is not a string, but instead a Texture object... in this case, a scene.Texture object. See the docs on creating such an object --

        self.spriteName.texture= scene.Texture('plc:Gem_Blue') 
        

        should do the trick.

        It might be a little confusing, because the SpriteNode() constructor's texture argument does allow a string, so you migtpht think the texture attribute is the same as what you passed in.

        1 Reply Last reply Reply Quote 1
        • heyguy4
          heyguy4 last edited by

          Now it says "global name 'scene' is not defined"

          1 Reply Last reply Reply Quote 0
          • omz
            omz last edited by

            @heyguy4 Just use Texture instead of scene.Texture. Most included examples import everything from the scene module (using from scene import *), but not the module itself. @JonB's code would be correct when you import just the module (import scene).

            1 Reply Last reply Reply Quote 0
            • heyguy4
              heyguy4 last edited by

              Okay now there's no error, but I can't see the sprite when the code compiles.
              Here's my setup method

                      def setup(self):
                              self.background_color = 'green'
              
                              self.spriteName = SpriteNode()
              
              
                              self.spriteName.anchor_point = (5,5)
                              self.spriteName.position = (50, 50)
              
                              self.spriteName.texture = Texture('plc:Gem_Blue')
              
              1 Reply Last reply Reply Quote 0
              • omz
                omz last edited by omz

                You haven't added the sprite to the scene. You have to add something like self.add_child(self.spriteName). Alternatively, you could also pass parent=self to the SpriteNode initializer.

                1 Reply Last reply Reply Quote 0
                • heyguy4
                  heyguy4 last edited by

                  That solves the issue with the blue gem not appearing, but if I use one of my own images that I imported into the folder

                  self.spriteName.texture = Texture('IMG_0469.JPG')
                  

                  returns a "Image not found" error and

                  self.spriteName.texture = Texture(ui.Image.named('IMG_0469.JPG'))
                  

                  returns a "Could not load image" error

                  1 Reply Last reply Reply Quote 0
                  • omz
                    omz last edited by omz

                    Is the imported image in the same folder as your script?

                    heyguy4 1 Reply Last reply Reply Quote 0
                    • heyguy4
                      heyguy4 last edited by

                      Yes it is

                      1 Reply Last reply Reply Quote 0
                      • heyguy4
                        heyguy4 @omz last edited by

                        @omz Does the folder have to have the same name as the program?

                        JonB 1 Reply Last reply Reply Quote 0
                        • omz
                          omz last edited by

                          No, that's not necessary, but the image shouldn't be in a sub-folder (or you'd need to include the sub-folder's name in the image name).

                          1 Reply Last reply Reply Quote 0
                          • heyguy4
                            heyguy4 last edited by

                            It's not in a sub folder. I have no idea why it's not working.

                            1 Reply Last reply Reply Quote 0
                            • ccc
                              ccc last edited by

                              Try...

                              import os
                              print(os.listdir(os.curdir))  # see if 'IMG_0469.JPG' is in that list
                              
                              1 Reply Last reply Reply Quote 0
                              • brumm
                                brumm last edited by brumm

                                from scene import *
                                
                                class MyScene (Scene):
                                    def setup(self):
                                        self.background_color = 'green'
                                
                                        self.spriteName = SpriteNode()
                                
                                        self.spriteName.position = self.size / 2      
                                 
                                        self.spriteName.texture = Texture('plc:Gem_Blue')
                                        #self.spriteName.texture = Texture('canvas.png')
                                        #self.spriteName.texture = Texture('image000.jpg')
                                        self.add_child(self.spriteName)
                                
                                run(MyScene())
                                
                                1 Reply Last reply Reply Quote 0
                                • JonB
                                  JonB @heyguy4 last edited by

                                  @heyguy4
                                  Try adding

                                  import os
                                  print(os.path.abspath(os.curdir))
                                  print(os.listdir('.'))
                                  

                                  at the start of your script. Then, copy and paste the name of the image that gets printed to the console. A common problem is that this is case sensitive, you must type the name exactly as it is printed.

                                  1 Reply Last reply Reply Quote 0
                                  • heyguy4
                                    heyguy4 last edited by

                                    I did what you said but I'm still getting the "Image Not Found" Error

                                    1 Reply Last reply Reply Quote 0
                                    • ccc
                                      ccc last edited by ccc

                                      What was printed when you added @JonB and/or my code?!?

                                      Does adding the following code print True or False?!?

                                      import os
                                      print('IMG_0469.JPG' in os.listdir(os.curdir))
                                      
                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post
                                      Powered by NodeBB Forums | Contributors