How to change the scale and rotation of uv coordinates using python in game engine?

I’m using BGE and I’m creating a racing game on this engine. And I want to make a system of vinyl (stickers) in the car, similar to the system of “need for speed world”, so I have already changed the position of the UV coordinates, now I need to change the rotation and scale.Can someone help me? it is possible?

I have this code, which messes with the position of the UV coordinates, but I need the scale and rotation too …

from bge import logic # get the controller that executes the script
cont = logic.getCurrentController() # get the object that uses the controller
own = cont.owner

speed_x = 0.005 # speed by which the coordinates are moved
speed_y = 0.00 # speed by which the coordinates are moved
mesh = own.meshes[0] # access the mesh of the object in question
v_array = mesh.getVertexArrayLength(0) # how many verts are there?
for v in range(0,v_array): # go through the uv’s of every vert
vert = mesh.getVertex(0,v)
uv = vert.getUV()
uv[0] += speed_x # change the uv coordinates. uv[0] = x, uv[1] = y
uv[1] += speed_y
vert.setUV(uv) # get the game engine to notice the change!!

(I do not know how to speak English right because I’m Brazilian)

Hi, we just did it in upbge using GPU but this is the same principle here: http://pasteall.org/blend/index.php?id=44529 This is an example for rotation

check the blender python api docs. lots of neat stuff in there.

https://www.blender.org/api/blender_python_api_2_78a_release/bge.types.KX_MeshProxy.html#bge.types.KX_MeshProxy.transformUV


OWNER = bge.logic.getCurrentController

matid = 0 #first material, -1 for all#
uv_index = 0 #first uv slot, 1 for the second, -1 for all#
uv_index_from = -1

## matrix layout ##
#(rot_XX, rot_XY, rot_XZ, trans_X), # rot values are used to scale
#(rot_YX, rot_YY, rot_YZ, trans_Y), # trans_Z doesnt do much
#(rot_ZX, rot_ZY, rot_ZZ, trans_Z),
#(NA,NA,NA,NA)

toggle = False
uvX = 0
uvY = 0

if [move up]:
    uvY = 0.01
    toggle = True

matrix = ((1,0,0,uvX),(0,1,0,uvY),(0,0,1,0),(0,0,0,0))

if toggle == True: #optional efficiency check#
    OWNER.meshes[0].transformUV(matid, matrix, uv_index, uv_index_from)


Ah yes I forgot transformUV. Better to use it because it does the loop through all vertex in c++ instead of python

Thanks!!! It helped a lot! But has a problem, is changing the UV coordinates of all object textures. Can you change only the UV coordinates of just one texture?

Thanks!!! It helped a lot! But has a problem, is changing the rotation of all object textures. Can you change only the rotation of just one texture?

This does not change textures. It changes the UV-components of a mesh. Obvious when you share a mesh you share all modifications to it. When you do not want that, ensure to use a copy of the mesh.

You can also have 1 UV layer per texture but I don’t know how many layers are supported in bge code (in upbge 1.3 we’ll have the possibility to change UV in the texture shader, so it will be per texture, and iirc I did a patch to have 8 uv layers supported with transformUV https://github.com/UPBGE/blender/commit/387a731d74232e9ef278a95cf6497f1d9e5e309f)

How will this work ??? This is not python.

That’s a patch, so to use it you’d need to recompile blender

its generally not good practice to present a solution which involves recompiling, unless its clear the person is willing to engage in such perils.

btw, vanilla bge can have 2 different uv slots.

Yeah, this was just to say that upbge supported 8 uv layers with transformUV. (You don’t need to compile if you use upbge that can be found here: https://upbge.org/ (if you want to test of course))
You see, I consider the others can be interested in the way the engine works.