using nearest point on curve node


#1

Hi there,

i am trying to get my head around using the ‘nearestPointOnCurve’ node. All i have in my scene is a locator which represents a fixed point in space, and an ep curve nearby. All i want to do is to find the u parameter in the curve that is the closest to the locator in space.

can anyone shed light how to implement the node for this please?

thanks alot guys,
Sam


#2

There are just a couple of connections. Here’s a pymel code example, which assumes you have a locator1 and curve1…

import pymel.core as pm

crv = pm.PyNode('curve1')
loc = pm.PyNode('locator1')
cpoc = pm.createNode('closestPointOnCurve')

crv.worldSpace[0] >> cpoc.inCurve
loc.worldPosition[0] >> cpoc.inPosition;

print cpoc.paramU.get()

You can run that, then have a look in the node editor to see the connections.
David

edit: Just noticed you asked about the nearestPointOnCurve. The one I showed above is in the bonusTools and has some extra data, but essentially the nodes work the same way. Slightly different attribute names.

import pymel.core as pm

crv = pm.PyNode('curve1')
loc = pm.PyNode('locator1')
cpoc = pm.createNode('nearestPointOnCurve')

crv.worldSpace[0] >> cpoc.inputCurve
loc.worldPosition[0] >> cpoc.inPosition;

print cpoc.parameter.get()

#3

thanks alot man. this will come in very handy.

cheers,
Sam


#4

sorry there was one final thing i forgot to mention. If instead of a locator that defines the point in space, is actually an ep curve point. eg ‘curve6.ep[1]’. is it posible to make a pyNode out of that so it can be treated in the same way as a locator?

thanks alot,

Sam


#5

You can do it, but since editPoints store their position local to the curve transform you probably need a way to get it in worldSpace. The simplest way I could think of to do that was using a locator which can be parented under the curve transform. Locators have a localPosition input and a worldPosition output so they are perfect for this kind of transform matrix math.

In this example I assume you already have curve1 and curve2, then I build the rest…

import pymel.core as pm

crv1 = pm.PyNode('curve1')
crv2 = pm.PyNode('curve2')
loc = pm.createNode('locator')
locTransform = loc.getParent()
loc.setParent(crv2, s=True, r=True)
pm.delete(locTransform)
cpoc = pm.createNode('nearestPointOnCurve')

# connect ep[1] to locator local position
pm.connectAttr(crv2.editPoints[1], loc.localPosition, f=True)
crv1.worldSpace[0] >> cpoc.inputCurve
loc.worldPosition[0] >> cpoc.inPosition;

print cpoc.parameter.get()

David


#6

thanks dude, this is great.

i was thinking couldnt you just find out the location of the curve edit point with ‘pointPosition’ and just feed that info directly into cpoc.inPosition. but i guess it has to come from a pyNode or something.

but anyway thanks for this will work great

Sam