PDA

View Full Version : where to start for an automated snap to vertex script


cgbeige
12-13-2011, 12:34 AM
I'm looking at making a script that automatically constrains some meshes to the nearest vertex of a ground plane. I know this type of thing is slow to do with MEL/Maya commands but I'm not quite ready for Python API and C++ is out of the question for my newb-ass skills. I'm not looking to have this spelled out for me but I'm completely new to these type of "find nearest point" math scripts so any pointers would be appreciated.

LegoBlock
12-13-2011, 01:30 AM
I would check out the "closestPointOnMesh" node, it's simple to use.

Azrail
12-13-2011, 05:07 PM
If you need to snap exactly to the vertex of the ground plane or whatever you don't need the closestPointOnMesh or some other heavy intersection stuff. You can do it really simple like so:
Pseudo python code, but nesting/indent is important:

objVerts = list of object's vertex positions
grndVerts = list of ground's vertex positions

minDist = 1e12
for objv in objVerts:
targetPos = 0,0,0 - store the closest position here later
for grndv in grndVerts:
dist = distance between objv and grndv
if minDist > dist:
minDist = dist
targetPos = grndv - set to the current ground vertex position
move obj vertex to targetPos

cgbeige
12-13-2011, 05:10 PM
thanks - I'm pretty busy with other stuff so I won't be able to test this for a bit. Am I right in assuming that this type of thing is much faster done with API/plug-in calls than Maya commands?

Azrail
12-13-2011, 05:22 PM
Can't tell for sure, but if you are not going for a compiled (C++) command or node I guess no. I think python is equally fast (or must I say slow) when dealing with heavy geometry, and it doesn't matter if you use API or non-API calls. So after all it's a matter of style and what is more convenient for you to use. It's achievable trough all languages. I would only do it with python & API since there are much more and better vector/point/matrix operations, which are not available trough "pure" python or (God forbid) MEL. And you will need them once you reach the worldSpace/localSpace conversion.

cgbeige
12-14-2011, 03:36 AM
ok - thanks.

cgbeige
12-15-2011, 04:40 PM
If you need to snap exactly to the vertex of the ground plane or whatever you don't need the closestPointOnMesh or some other heavy intersection stuff. You can do it really simple like so:
Pseudo python code, but nesting/indent is important:

objVerts = list of object's vertex positions
grndVerts = list of ground's vertex positions

minDist = 1e12
for objv in objVerts:
targetPos = 0,0,0 - store the closest position here later
for grndv in grndVerts:
dist = distance between objv and grndv
if minDist > dist:
minDist = dist
targetPos = grndv - set to the current ground vertex position
move obj vertex to targetPos


sorry - I'm trying to turn this into an actual command but since I don't really know Python yet, I am a little confused as to what to do with minDist = 1e12

Kriz
12-15-2011, 06:35 PM
I haven't looked through the entire thread, but it seems like Azrail is just setting minDist to a very large number.

If you are using a Python version < 3, you can use sys.maxint as the largest number:

import sys
sys.maxint

Azrail
12-15-2011, 07:54 PM
It's called scientific notation. 1e10 means 1 * 10^10 or 10 000 000 000. You don't have to do nothing with it, python 2.6 and mel both accept numbers in this format. Respectively 1e-10 means 1/10^10 or 0.00000000001. It's just a shorter way of writing really big or small numbers. By the way if you have played enough with numbers in maya you may already have noticed that really often a value of 0 is actually something like 8.2564687e-12 or something like it, which is a number very very close to zero.

CGTalk Moderation
12-15-2011, 07:54 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.