Getting a curve's mid point in worldspace?

Getting a curve's mid point in worldspace?

jmalaska
Advocate Advocate
547 Views
2 Replies
Message 1 of 3

Getting a curve's mid point in worldspace?

jmalaska
Advocate
Advocate

I'm having difficulty finding a way to get a transform for the middle spot on a curve. I haven't gone as far as rebuilding the curve evenly with a ton of points evenly to get the middle, as that seems a bit overkill and I need to do this on thousands of curves. The commands curve and curvePoint don't seem to have the flags to get this midpoint.

 

Is anyone aware of a way to do this in one call?

Accepted solutions (1)
548 Views
2 Replies
Replies (2)
Message 2 of 3

zewt
Collaborator
Collaborator
Accepted solution

If you want the point halfway along the curve, you need to convert the distance along the curve to the curve param, and then get the position of that param:

 

import pymel.core as pm

def go():
    # Get the MFnCurve for the shape node (do this however you want):
    node = pm.PyNode('curve1').getShape()
    curve = node.__apimfn__()
    
    # Get the curve param halfway along the length:
    length = curve.length()
    param = curve.findParamFromLength(length/2)
    
    # Get the world space position:
    p = om.MPoint()
    curve.getPointAtParam(param, p, om.MSpace.kWorld)

    # Sample output:
    loc = pm.createNode('locator').getTransform()
    pm.xform(loc, ws=True, t=(p.x, p.y, p.z))
    
go()

 

Message 3 of 3

jmalaska
Advocate
Advocate

Thanks @zewt this is exactly what I was looking for. Cheers!

0 Likes