Object.LocalOrientation don't work(or am I dumb...)

Hello! I am using a list to get all objects in the scene that have “Grass” in their properties, to make a grass (not procedural, because I don’t know: (…). But when I apply “localOrientation” to each object on the list, they change their orientations in the same direction, and their orientations are different.The objects have an “arrow” on top because I was wondering if they changed for each other in different ways (and it worked …).
(Note: UPBGE Project)

video: https://youtu.be/04BhMXbViXA

Edit: The bad script…

import bge
import math
import mathutils
import random
from collections import OrderedDict

class grass(bge.types.KX_PythonComponent):
    args = OrderedDict([
        ("velocity", 0.0),
    def start(self, args):
        self.velocity = args["velocity"]
        self.x_z = 0.0
        self.y_z = 0.0
        self.timer = 0.0
        self.grass = [obj for obj in bge.logic.getCurrentScene().objects if "Grass" in obj.getPropertyNames()]
        self.tree = [obj for obj in bge.logic.getCurrentScene().objects if "Tree" in obj.getPropertyNames()]
        for gr in self.grass:
            gr['Ori'] = random.uniform(0.0, 360.0)
            xyz = gr.localOrientation.to_euler()
            xyz[2] = math.radians(gr['Ori'])
            gr.localOrientation = xyz.to_matrix()
    def grass_logic(self):
        def ori():
            self.x_z = math.sin((self.object['Time']*self.velocity)/1.2)/5
            self.y_z = math.cos((self.object['Time']*self.velocity)/2.3)/5
            return(self.y_z, self.x_z)
        for gr in self.grass:
            gr.worldOrientation[0][2] = ori()[0]
            gr.worldOrientation[1][2] = ori()[1]
        for tr in self.tree:
    def update(self):

Hi @Markuebinha

So let me get this strait :thinking:
You want all the objects with grass property to face in the same direction :question:
Like at the beginning of the video in the Blender View-Port :question:

[ EDIT ]
After looking at your file I still don’t understand what you want to happen :question:

Random Rotations that are locally set :thinking: :question:

I want objects to have their local orientations shifted in different directions, since everyone today always shifts to the same side.

The script modifies the orientation of all objects in the list at random so that it does not become monotone. But when I used the time math.sin() to modify the local orientation of each object, they deform in the same direction, and that’s not what I want, I want them to change individually.

You can rotate clockwise or counterclockwise, that’s it.

dirn = random.uniform(0,1)
dirn = 1 if dirn < 0.5 else -1
xyz[2] = math.radians(gr['Ori'])*dirn

whats wrong with obj.applyRotation((x,y,z), True)?

WARNING: python is very slow, running code on each blade of grass will take a long time.

use a shader to warp the uv coords or otherwise displace the normals.

I don’t know how to make this, this is very hard for me…

I made this code, trying to change the position of the object mesh vertices, but nothing happens. If I put the position of the vertex on the console, it shows that its position changes, but visually, it doesn’t.

import bge
import math
import mathutils

cont = bge.logic.getCurrentController()
own = cont.owner

grass = [obj for obj in bge.logic.getCurrentScene().objects if "Grass" in obj.getPropertyNames()]
velocity = 5

def ori():
    x_z = math.sin((own['Time']*velocity)/1.2)/5
    y_z = math.cos((own['Time']*velocity)/2.3)/5
    return(y_z*10, x_z*10)

for gr in grass:
    vert_pos = [ori()[0],ori()[1],1.0]
    mesh = gr.meshes[0]
    for mat in range(len(mesh.materials)):
        for vet in range(mesh.getVertexArrayLength(mat)):
            vertex = mesh.getVertex(mat, vet)
            vertex_get_xyz = vertex.getXYZ()
            vertex_get_xyz[0] = vertex_get_xyz[0]+ori()[0]
            vertex_get_xyz[1] = vertex_get_xyz[1]+ori()[0]
            pos = mathutils.Vector((vertex_get_xyz[0],vertex_get_xyz[1],vertex_get_xyz[2]))

Er so this is what I get on my end when opening @Markuebinha’s blend file :thinking:

I see grass with rotation set to different rotations.
However it’s the same rotations every time.
@Markuebinha I think you are wanting ABSOLUTE_RANDOM* rotations per-init :question:

PS. I think I am the dumb one here . . . :slight_smile:

Yes, i want individuall random object rotations!

this is all you need to get them random oriented

import random

def test(cont):
    own = cont.owner
    grass = [obj for obj in own.scene.objects if "Grass" in obj]

    for obj in grass:
        obj.worldOrientation = [0,0,random.randint(1,360)]


Yeah! Mas eu preciso que o “vento” nas folhas também se mova em direções diferentes.

write in English please, most people wont understand what you write otherwise.

i put what you have written into google translate and got this.
“” Yeah! But I need the “wind” on the leaves to move in different directions as well. “”

1 Like

So Sorry…
Can anyone help me?

Uh. Did @Cotaks’s script not work :question:

it’s simple, first set the rotation, then use the script to make wind.

Yes, but it’s something I had used before.

I want something like this: https://youtu.be/245Zv4AWXVM?t=56

I finally got a satisfactory result, got what I wanted using mesh.transformUV() and mesh.transform(). This results not bad, for me :smiley: Vertex_BGE_Test.blend (541.9 KB)

1 Like