random rotation around normal

31 views
Skip to first unread message

Anno

unread,
Feb 13, 2017, 9:27:59 AM2/13/17
to Python Programming for Autodesk Maya
Hi,
I'm building a scatter tool for maya that uses nparticles and an instancer to distribute geometry in the scene.
I built my own sampler that basically returns a point array which gets fed into the particle system.
Alongside the point array i also get a normal array which I can use to calculate rotation values that match to the surface normal direction.
I achieve this my doing the following:

normal = n[i] # MVector, surfacenormal
world
= OpenMaya.MVector(0, 1, 0)
rot_quat
= OpenMaya.MQuaternion(world, n[i], normal_weight)

rotation
= OpenMaya.MVector(math.degrees(rot_quat.asEulerRotation().x), math.degrees(rot_quat.asEulerRotation().y), math.degrees(rot_quat.asEulerRotation().z))
self.rotation_pp.append(rotation)


What I want to do now is to randomly rotate each instance by a certain amount of degree around its "local" x, y and z axis.

Can somebody point me towards the right direction how I can get the desired result?

thanks a lot,
Anno



Anno

unread,
Feb 15, 2017, 8:06:05 AM2/15/17
to Python Programming for Autodesk Maya
Hi again,
I found a solution so if someone is interested.

I create a new transform matrix and fill it with random values.
Then I can post-multiply the matrix by a matrix I get from the quaternion.
From the resulting matrix I can get my new rotation values.


rand_r = (10,360,10)
normal
= n[i]

world
= OpenMaya.MVector(0, 1, 0)

quat
= OpenMaya.MQuaternion(world, n[i], normal_weight)



r_x
= math.radians(random.uniform(-rand_r[0], rand_r[0]))
r_y
= math.radians(random.uniform(-rand_r[1], rand_r[1]))
r_z
= math.radians(random.uniform(-rand_r[2], rand_r[2]))


mat
= OpenMaya.MTransformationMatrix()


util
= OpenMaya.MScriptUtil()
util
.createFromDouble(r_x,r_y,r_z)
r_i
= util.asDoublePtr()
mat
.setRotation(r_i, OpenMaya.MTransformationMatrix.kXYZ)


mat
= mat.asMatrix() * quat.asMatrix()



Cheers,
Anno
Reply all
Reply to author
Forward
0 new messages