I am trying to play my own animation in the ursina engine but I have no idea how to. According to the documentation, I would need this code:
from ursina import *
app = Ursina()
window.color = color._20
animation = Animation('ursina_wink', fps=2, scale=1, filtering=None, autoplay=True)
EditorCamera()
app.run()
And that one does indeed work. However, I do not understand how I can replace the default animation with my own frames ('ursina_wink' is inbuilt).
The documentation says there is a frame parameter, so here is what I tried so far:
from ursina import *
app = Ursina()
window.color = color._20
coin1 = load_texture('animation/coin1')
coin2 = load_texture('animation/coin2')
coin3 = load_texture('animation/coin3')
coin4 = load_texture('animation/coin4')
coin5 = load_texture('animation/coin5')
coin6 = load_texture('animation/coin6')
animation = Animation('test',fps=2, scale=1, filtering=None, autoplay=True, frames = (coin1, coin2,coin3,coin4,coin5,coin6))
EditorCamera()
app.run()
But I cannot see anything on the screen and I cannot find an example online for it. Would really appreciate some help.
It loads an image sequence as a frame animation. You can load it like this:
Animation('coin') and it will take all the frames starting with 'coin' and load them alphabetically.
I will clarify this in the documentation.
I have come across your error and I have a simple solution, try to convert the images into a gif because in the ursina documentation it says that you can also use a gif wich is way more simple but less customizable, here's the text:
Loads an image sequence as a frame animation.
Consider using SpriteSheetAnimation instead if possible.
So if you have some frames named image_000.png, image_001.png, image_002.png and so on,
you can load it like this: Animation('image')
You can also load a .gif by including the file type: Animation('image.gif')
So, I hope this helped you.
Related
I am using pyqtgraph and trying to use a histogramLUTable inside of a viewbox to try and plot some data and I have this black box with a little A that is obstructing the graph origin/axis in the lower left hand corner and I can't seem to find what it is named and/or how to get rid of it. I attached a picture with a little red box around what I am talking about.
The image in question with little red box circling what I am referring to:
There is also a little black box on the lower right hadn side obfuscating the x axis but doesn't go into the level adjustment on the right hand side.
image showing issue with first code written :
For reference, my code so far:
import PyQt5
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pandas as pd
import pyqtgraph as pg
up = np.linspace(0,1,100)
down = np.flip(up)
nparray = np.hstack([up, down])
data = np.vstack([[nparray], [nparray],[nparray]])
data = np.transpose(data)
app = QtGui.QApplication([])
win = pg.GraphicsLayoutWidget(show=True, title="pyqtGraph attempt", size=[800,600])
view = pg.PlotItem()
win.addItem(view)
img = pg.ImageItem(data, border='w')
histogram = pg.HistogramLUTItem()
histogram.setImageItem(img)
win.addItem(histogram)
if __name__ == '__main__':
pg.mkQApp().exec_()
EDIT (again):
I took a different approach to try and mitigate the little black box by using a plotItem and Viewbox rather than the HistogramLUTable but when I go this route I get the same issue. Originally I said this gives me another black box in the lower right hand corner but if you look at my original screen shot the black box is there originally as well.
import PyQt5
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pandas as pd
import pyqtgraph as pg
up = np.linspace(0,1,100)
down = np.flip(up)
nparray = np.hstack([up, down])
data = np.vstack([[nparray], [nparray],[nparray]])
app = QtGui.QApplication([])
win = pg.GraphicsLayoutWidget(show=True, title="pyqtGraph attempt", size=[800,600])
view = pg.PlotItem()
view.hideButtons()
win.addItem(view)
img = pg.ImageItem()
view.addItem(img)
img.setImage(data)
if __name__ == '__main__':
pg.mkQApp().exec_()
image showing issue with second code written:
This element is a button that allows the plot to auto-scale, therefore it appears when you zoom. One possible solution is to hide it using hideButtons() method:
view = pg.PlotItem()
view.hideButtons()
win.addItem(view)
So after a lot of playing with the code the issue appeared to be using:
app = QtGui.QApplication([])
in conjunction with:
pg.mkQApp().exec_()
It causes issues, I assume from interfering with each other, because your trying to call two application execution methods but this is just an assumption.
Preferred solution if using pyqtgraph is to stick with removing
app = QtGui.QApplication([])
altogether and only including
if __name__ == '__main__': pg.mkQapp().exec_()
But an alternate also includes using
app = QtGui.QApplication([])
with
if __name__ == '__main__': app.exec_()
But it depends on how you are using pyqtgraph, if you are using it for it's graphing methods then you will probably need to use app.addItem(plotItem) and therefore you need to call app.exec_() however if you allow pyqtgraph to take care of generating the GUI you can let it take care of executing the application via pg.mkQapp().exec_()
I am wondering about how to make a simple cube texture in the Python Panda3D wrapper, ursina engine library.
Here's the code that I have tried.
from ursina import *
app = Ursina()
window.fps_counter.enabled = False
class Voxel(Button):
def __init__(self, position=(0,0,0), texture='red.png'):
super().__init__(
parent = scene,
position=position,
model='cube',
origin_y=0.5,
texture=texture,
color=color.color(0,0, random.uniform(0.9, 1.0)),
scale = 1.0
)
for x in range(3):
for y in range(3):
for z in range(3):
voxel = Voxel(position=(x,y,z))
EditorCamera()
app.run()
I want to know how to make uv map ( one that I made is really bad ).
I want to know convert uv map to ursina engine cube texture.
Built in textures are great for Ursina Voxel engines. They include:
'arrow_down'
'arrow_right'
'brick'
'circle'
'circle_outlined'
'cobblestone'
'cursor'
'file_icon'
'folder'
'grass'
'heightmap_1'
'horizontal_gradient'
'noise'
'radial_gradient'
'reflection_map_3'
'shore'
'sky_default'
'sky_sunset'
'ursina_logo'
'ursina_wink_0000'
'ursina_wink_0001'
'vertical_gradient'
'white_cube'
They are all great textures. But if you don't want to use built in textures, use a .jpg .psd or .png file, but without the file extension.
e.g.
My_Image.png would be imported as...
Entity(model=cube,
texture='My_Image')
If you have multiple files that have the same name but different file extensions,
e.g.
My_Image.png
and
My_Image.jpg
and both of them are compatible, it will use the first file in the directory.
I hope this helps. If so, please approve this so others can see that it works. It worked for me and I hope this improves your programs!
There is no such thing as ursina specific textures. When importing a texture, just use texture='cool_file' without the extension (.jpg, .png and .psd). Ursina currently supports png, jpeg and psd images (the psd-tools library must be installed since it isn't by default).
If you want to do uv mapping, you need to directly do it in a 3D software (e.g. Blender), and just import the model.
So I have been having problems trying to write the function to change the size of an image if to big and saving it as a thumbnail. I have how to retrieve the image just lost after that. I know about pillow but cant use for the class any help would be appreciated.
Update: So far I have gotten the code to resize the image and make it a thumbnail. The next part that I am on is having it save if resized to thumbnail2, but if it stays the same save as thumbnail1. Here is my code so far without the next step.
import urllib
url ="https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstra ion_1.png"
src = "C:\Users\laramie\Pictures\PNG_transparency_demonstration_1.png"
connect = urllib.urlretrieve(url, src)
def scalePicture(src):
newWidth = getWidth(src)/2
newHeight = getHeight(src)/2
canvas = makeEmptyPicture(newWidth, newHeight)
for x in range(newWidth):
for y in range(newHeight):
setColor(getPixel(canvas, x,y), getColor(getPixel(src, x*2, y*2)))
return canvas
def thumbNail():
srcPic = makePicture(src)
destWidth = getWidth(srcPic) / 2
destHeight = getHeight(srcPic) / 2
destPic = makeEmptyPicture(destWidth, destHeight)
destPic = scalePicture(srcPic)
show(srcPic)
show(destPic)
thumbNail()
There are a bunch of strange things going on in your code:
destPic = makeEmptyPicture(destWidth, destHeight)
destPic = scalePicture(srcPic)
the first line here is not required, because the destPic is overwritten immediately.
for x in range(newWidth):
for y in range(newHeight):
setColor(getPixel(canvas, x,y), getColor(getPixel(src, x*2, y*2)))
Ths is a very inefficient way to scale an image, that gives inferior results, unless the scale factor is an integer, and even then there are faster and better approaches.
I would recommend you to import PIL (Python Image Library) and use it to work with images. Things like loadng, saving, scaling or flipping images are easily done. However, you may need to install this library if it did not come with your python installation.
https://media.giphy.com/media/3og0IDneIpXgdMDOzm/source.gif
I am working on a code that draws a transparent rectangle on screen and saves the section as an image, yet excessive rectangles are being left behind.
import wx
import win32gui
app = wx.App(False)
s = wx.ScreenDC()
s.Pen = wx.Pen("#FF0000")
int_x = win32gui.GetCursorPos()[0]
int_y = win32gui.GetCursorPos()[1]
while 1:
s.DrawRectangle(int_x,int_y,win32gui.GetCursorPos()[0]-int_x,win32gui.GetCursorPos()[1]-int_y)
I think you would need to do some kind of clear or refresh after the draw
s.Clear()
s.Refresh()
I haven't used this so I'm not sure if it will just work in your while loop but I'd imagine it won't and you would need something to handle the start (mousedown) and end (mouseup) of the drawing and then on mouseup do the clear and refresh that i mention.
Apologies in advance if this is a simple fix (I am relatively new to programming). I am creating a python game (with pygame) that will include a lot of images. I am currently using the following code to import and scale the pictures according to the screen resolution:
pygame.init()
WINDOW= pygame.display.Info() # size of window's width in pixels
WINDOWW = WINDOW.current_w
WINDOWH = WINDOW.current_h
size = 1920/WINDOWW
CreditsL = pygame.image.load ('TEXT\Credits.png')
Creditsrect= CreditsL.get_rect()
Credits = pygame.transform.scale(CreditsL, (int(Creditsrect.w/size), int(Creditsrect.h/size)))
Since I have to import tons of images I would like to know how I can make a function that will import and scale the pictures, instead of me having to copy and paste.
Thanks in advance
def resize(imgpath):
img = pygame.image.load(imgpath)
rect = img.get_rect()
return pygame.transform.scale(img, (int(rect.w/size), int(rect.h/size)))
Credits = resize('TEXT\Credits.png')
If you want to avoid having to copy-paste this last line for each and every image, use a list of [(name, path),] tuples and build a dict from it:
SOURCES = [
("credit", 'TEXT\Credits.png'),
("something", 'TEXT\whatever.png'),
# etc
]
IMAGES = dict((name, resize(path)) for name, path in SOURCES)
Then to use the resized "credit" image :
do_something_with(IMAGES["credit"])
It is pretty easy to wrap your current code lines into a function and call it:
def importer(path):
CreditsL = pygame.image.load(path)
Creditsrect = CreditsL.get_rect()
return pygame.transform.scale(CreditsL,
(int(Creditsrect.w/size),
int(Creditsrect.h/size)))
Credits = importer('TEXT\Credits.png')
Scaling the image through pygame, from my understandings, is not the way to go. Use a simple photo editor (Paint if you gotta!) to scale your image accordingly. It is more work for the computer to do. The more work your computer does, the less efficient the game! Memory might not be an issue now, but it would probably be wise to make good practice, as well as make your code easier to look at.