Mon, Dec 2, 8:52 AM CST

Renderosity Forums / Blender



Welcome to the Blender Forum

Forum Moderators: Lobo3433 Forum Coordinators: LuxXeon

Blender F.A.Q (Last Updated: 2024 Dec 01 9:08 pm)

Welcome to the Blender Forum!


   Your place to learn about Blender, ask questions,
   exchange ideas, and interact with the other Blender users!


   Gallery | Freestuff | Tutorials

 

Visit the Renderosity MarketPlace. Your source for digital art content!

 





Subject: Blender python: Creating a cylinder given two endpoints and a radius


wealthychef ( ) posted Mon, 07 July 2014 at 10:40 PM · edited Mon, 02 December 2024 at 3:35 AM

I am stuck trying to make a cylinder given its endpoint locations and a radius.  I need to compute the rotation for the following function call: 

bpy.ops.mesh.primitive_cylinder_add(radius = radius, depth = depth, location = center, rotation = rotation)

How do I do this?  For example, given endpoints (0,0,0) and (2,2,2), what value do I use for "rotation"?  Thanks for any pointers.  


wealthychef ( ) posted Tue, 08 July 2014 at 8:12 PM

Tough one, eh?  :-)  I've googled all over the place, and I've found some code, but it does not work. that is, the resulting quaternion, when used as obj.rotate_quaternion, does not result in properly rotated cylinder with ep1 = (0,0,0) and ep1 = (0,100,0)

def FindRotation(ep1, ep2):

First, create quaternion per

http://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another

vec = numpy.array(ep2) - numpy.array(ep1)

vec = vec/numpy.linalg.norm(vec)

if not vec[0] and not vec[1]:
return (0,0,0)
print ("vec: %s"%str(vec))
Z = numpy.array((0,0,1))
kcostheta = numpy.dot(Z, vec)
k = Length(vec) # sqrt(Length(vec)^2 * Length(Z)^2) = Length(vec)
Q = Quaternion(numpy.cross(vec,Z), kcostheta+k)
Q.normalize()
print ("kcostheta: %f, k: %f, Q: %s"%(kcostheta, k, str(Q)))
return Q


wealthychef ( ) posted Tue, 08 July 2014 at 8:56 PM

Found a solution thanks to  http://mcngraphics.com/thelab/blender/connect/

def FindRotation(ep1, ep2):
ep1 = numpy.array(ep1)
ep2 = numpy.array(ep2)
center = ep1 + 0.5 * (ep2 - ep1)

Position of ep2 if an imaginary sphere encompassing

both objects was transformed to 0, 0, 0

ep2new = ep2 - center

--------------------------------------------

Spherical coordinates (radius r, inclination theta, azimuth phi) from

the Cartesian coordinates (x, y, z) of o1 on our imaginary sphere

r = numpy.linalg.norm(ep2new) # radius of sphere
theta = math.acos(ep2new[2]/r)
phi = math.atan2( ep2new[1], ep2new[0])
return (0,theta, phi)

 


Privacy Notice

This site uses cookies to deliver the best experience. Our own cookies make user accounts and other features possible. Third-party cookies are used to display relevant ads and to analyze how Renderosity is used. By using our site, you acknowledge that you have read and understood our Terms of Service, including our Cookie Policy and our Privacy Policy.