Rotating object by the Z axis

Hello all :wink:

First of all a little bit of code :

        self.tree = loader.loadModel("Tree/Tree")
        for i in range(4):
            self.treeTmp = render.attachNewNode("NewTree")

By the previous code I’m trying to do a simple rotation for each new tree following the z axis.
Still having some confusion to determine what are really the Yaw, Pitch and Roll. Tried to play a little bit with them, but I think that the center of the object is the root of the trunk and not the middle of the object itself!!!

Anyway… Tried many things and not getting the trick…
Can anyone help ?

The center of rotation is the (0, 0, 0) point, which is only rarely the geometric center of the object. Since trees are usually modeled with (0, 0, 0) on the ground, it probably is the root of the trunk.


Ok but how rotate each new instance of the tree according to the Z axis of the tree itself ?

The purpose of this rotation is simple: giving a different shape for each instance. (well, the shape is the same but will look different for the viewer)

What about inserting a simple randomization of H and scale inside your cycle?
Something like:

import random
for i in range(4):
    self.treeTmp = render.attachNewNode("NewTree") 
    self.treeTmp.setScale(random.uniform(0.5, 1.5)#scale will be between 0.5 and 1.5 of the original

Choose wisely the magic numbers
Remember that a cycle of this kind will always return a different tree disposition each time it’s executed.
But if you give the random module a seed, it will generate a determined disposition for your trees.

random.seed(1)#choose a number that works the way you like, experiment!

Thanks Ausir but I think that I’m badly explaining my problem.
When I’m using setH() on the instances, it’s like I’m changing the positionning of the trees on the X and Y axis and not the rotation of the tree itself!!

Let’s take a bisc exemple of two trees and rotate them according to Z :
Here is what I want to do :


This is working very good and is the result desired
But for the instances, it’s not working the same way and I can’t understand why!

        for i in range(2):
            self.treeTmp = render.attachNewNode("NewTree")

Hope I’m explaining it better this way.

I see your problem now.
My post was more of a “strategy to put things at random”…

The point is that NodePath.instanceTo() returns the new nodePath that is created.In your code, you create 4 trees as children of self.treeTmp, but you don’t access their properties.

So, try to go something like:

for i in range(10):
    self.tmp.setH(random.random(360))#and so on

This way, all trees will be children of self.treeTmp.

Ok we’re getting closer :slight_smile:
But I’m more confused now! You mean that for accessing the proprities of each instance, I need to create an other variable ?

Which will look something like this :

for i in range(2):
            self.treeTmp = render.attachNewNode("NewTree")
            self.tmp = self.tree.instanceTo(self.treeTmp)

That will not work.

Let’s take the example given in the manual :

dancer = Actor.Actor("chorus-line-dancer.egg", {"kick":"kick.egg"})
for i in range(50):
  placeholder = render.attachNewNode("Dancer-Placeholder")
  placeholder.setPos(i*5, 0, 0)

The posistion of each new instance is being accessed by placeholder and it will be the same thing for the heading (seH())

I just tried some snippets, and your original (as well as my first snippet) definitely work.
So, you should make sure your tree model isn’t wrong.
To test with a “neutral” model, replace

self.tree = loader.loadModel("Tree/Tree")



and see for yourself the diferences in behaviour.

Also, there’s no reason to use instanceTo here. Use copyTo instead, it’s much less likely to cause confusion, and just as efficient.


Finally found the problem :smiley:

It was not realted to my original files but the first setPos().

        #Load the tree
        self.tree = loader.loadModel("Tree/Tree.egg")
        #self.tree.setPos(1,1,1)   #THIS WAS THE ANNOYING LINE
        for i in range(10):
            self.treeTmp = render.attachNewNode("NewTree")

The code here is working perfectly and is want I wanted. Removing the comment on the line self.tree.setPos(1,1,1) and adding a modifiction on the heading after that was dispatching my trees in circle and not rotating them arround the z axis.

Understood that while I was playing with the smiley.egg and I didn’t know the command copyTo() yet.
Thanks to all for helping :wink: