[Closed] splitting a mesh based on a similar mesh
Sorry if the title is a little vague… this is what I’m trying to do:
I'm working on converting a track mesh from an old racing game. There are two 3rd party apps that allow me to convert the model so I can load it in max.
I.One app exports the track in segments (like it should be), but with a lot of flipped polys and missing some mapping.
II.The other app imports the mesh perfectly but as a single object.
I need to have the track segmented for setting the visual load values and material settings for the game engine so I was trying to script splitting the single mesh(II) into the segments from (I).
This is how I attempted to do this - please don't laugh, I'm still very green with maxscript :)
1.Merge I & II
2.Explode all the faces in (II) as objects
3.Extrude (I) to make the surface into 3d object
4.Use a grouping script.. the logic being: join together all the objects from (2.) who's pivot are inside the volume of the extruded segments from (3.)
(based on [Kramsurfer's grouping box]( http://forums.cgsociety.org/showpost.php?p=5390118&postcount=4))
(
tnCount = 0 --counter for garbage collection
myTrack = $trackobj* as array --make an array of all the boxed track sections from app1
myObjects = $object* as array --make an array of all the face-objects from app2
hide myTrack --hides all boxed track sections from app1
for Obj in myTrack do
(
unhide Obj --unhides one box at a time for grouping
tnArea = Obj
local NewObject = undefined
for chkObj in myObjects do
( -- if not the box, but within the volume, attach.
tnCount +=1
if ( mod tnCount 100 == 0) do gc() --garbage collection
if chkObj != tnArea and chkObj.ishidden == false then
if chkObj.pos.x > tnArea.min.x and chkObj.pos.x < tnArea.max.x then
if chkObj.pos.y > tnArea.min.y and chkObj.pos.y < tnArea.max.y then
if chkObj.pos.z > tnArea.min.z and chkObj.pos.z < tnArea.max.z then
if NewObject == undefined then
(
NewObject = ChkObj
convertToMesh NewObject
NewObject.name = "_" + tnArea.name
)
else
attach NewObject chkObj
)
hide Obj --hide box after joining all objects in volume
)
)
I can't get this to work so I was hoping someone could suggest a different approach.
Just a wild shot pseudo code:
for i in “model I” objects)
{
create new mesh name = “segmentnr i”
for q in “model I”.mesh i.faces
{
for u in “model II faces”
{
if( raycast +/- intersect “model I”[i].face[q] “model II”.face[u] )
{
detach “model II”.face[u]
attach to “segmentnr i”
}
}
}
}
Thanks noontz.
I tried filling out your pseudo code in steps..
(1) for i in "model I" objects
create new mesh name = "segmentnr i"
(2) for q in "model I".mesh i.faces
{
(3) for u in "model II faces"
{
(4) if( raycast +/- intersect "model I"[i].face[q] "model II".face[u] )
{
(5) detach "model II".face[u]
attach to "segmentnr i"
}
}
}
}
(
myTrack = $track
myObjects = $trackobj* as array
faceCountII = myTrack.getnumfaces()
faceTestII = ((myTrack.getnumfaces()) != 0)
for Obj in myTrack do --(1)
(
local NewObject = undefined
convertToMesh NewObject
NewObject.name = "_" + Obj.name
faceCountI = Obj.getnumfaces()
for q in 1 to faceCountI do --(2)
(
for u in 1 to faceCountII while faceTestII do --(3)
--(4)
)
--(5)
)
)
I've been reading up on Intersectray, but haven't been able to figure out (4) & (5). Can anyone help me out with those?
Thanks again.
Sorry for bumping my own thread, but I took another shot at it. I think I’m getting close but it’s giving me an arror (in [color=white]red[/color]). Does anyone know what I might have done wrong?
(
myTrack = $track
mySegments = $trackobj* as array
faceCount_T = myTrack.getnumfaces()
faceTest_T = ((myTrack.getnumfaces()) != 0)
for i in mySegments do
(
convertTo i (Editable_Poly)
local NewSegment = undefined
faceCount_S = i.getnumfaces()
for q in 1 to faceCount_S do
(
r=ray q.pos [0,0,-1]
for u in 1 to faceCount_T while faceTest_T do
(
if(intersectray u r) != undefined then
(
polyOp.detachFaces MyTrack #{u} delete:true asNode:true name:"Face"
if NewSegment == undefined then
(
NewSegment = Face
convertTo NewSegment (Editable_Poly)
NewSegment.name = "_" + i.name
)
else
attach NewSegment Face
)
)
)
)
)
faceCount_S = i.getnumfaces()
for q in 1 to faceCount_S do
(
r=ray q.pos [0,0,-1]
Firstly what is the error ?
but does q actually have a pos ? It looks to be derived from faceCount_S which will only return the amount of faces in a mesh not the actual face itself…(p.s I’m not a programmer )
Try replacing your error line with…
r = ray (polyop.getFaceCenter i q) [0, 0, -1]
Untested, as I don’t have Max in front of me, but it looks like it’ll work.
Cheers,
Drea
Thanks Andreas I changed that section of code and it’s not breaking there anymore. One last thing… I can’t figure out how to reference the face for the intersectray test. I tried defining it (red code) but I’m getting a “–No “convertTo” function for undefined” error. (in green)
(
myTrack = $track
mySegments = $trackobj* as array
faceCount_T = myTrack.getnumfaces()
faceTest_T = ((myTrack.getnumfaces()) != 0)
for i in mySegments do
(
convertTo i (Editable_Poly)
local NewSegment = undefined
faceCount_S = i.getnumfaces()
for q in 1 to faceCount_S do
(
r = ray (polyOp.getFaceCenter i q) [0, 0, -1]
for u in 1 to faceCount_T while faceTest_T do
(
polyOp.detachFaces MyTrack u delete:false asNode:true name:"tempFace"
local uSel = tempFace
convertTo uSel (Editable_Poly)
if(intersectray uSel r) != undefined then
(
polyOp.detachFaces MyTrack u delete:true asNode:true name:"Face"
if NewSegment == undefined then
(
NewSegment = Face
convertTo NewSegment (Editable_Poly)
NewSegment.name = "_" + i.name
)
else
attach NewSegment Face
)
)
)
)
)
Although slow, this worked:
Undo off ()
myTrack = $track
mySegments = $trackobj* as array
faceCount_T = myTrack.getnumfaces()
faceTest_T = ((myTrack.getnumfaces()) != 0)
with redraw off
(
for i in mySegments do
(
convertTo i (Editable_Poly)
local NewSegment = undefined
for u = polyop.getNumFaces myTrack to 1 by -1 do
(
r = ray (polyOp.getFaceCenter myTrack u) [0, 0, -1]
polyOp.detachFaces MyTrack #{u} delete:false asNode:true name:"tempFace"
local uSel = $tempFace
convertTo uSel (Editable_Poly)
if(intersectray i r) != undefined then
(
if NewSegment == undefined then
(
NewSegment = uSel
convertTo NewSegment (Editable_Poly)
NewSegment.name = "_" + i.name
)
else
polyOp.attach NewSegment uSel
)
else delete uSel
)
)
)
Hope somebody finds this useful…