[Closed] Deleting overlaping meshes
Hey guys,
I thought it would be better to transfer my thread to the sdk/maxscript section.
Here is the link
http://forums.cgsociety.org/showthread.php?f=6&t=798717
Any help regarding a maxscript solution are greatly appreciated.
I have also searched for a scipt which fits my problem but didnt find
anything useable.
regards
NAIK
OK. I gave it a go and got something that kinda works…it’s not elegant and if Bobo reads it he might laugh at me (and he is allowed to do so ;))
for o in $Sphere_Small_* do --loop through the small spheres
(
Root = o.pos --gets the center of the current small sphere
Rad_Ray = Ray Root [0,0,1] --shoots one Ray in Z+
Radius = length ((intersectRay o Rad_Ray).pos - Root) --calculates the radius of the current small sphere
Test_Arr = #() --empty Array
for v in 1 to o.mesh.numverts do --loop through the current sphere's Vertices
(
Vert_Pos = getvert o.mesh v --gets the position of the current Vertex
Test_Dir = normalize (Vert_Pos - Root) --calculates the direction for a Ray
Test_Ray = Ray Root Test_Dir --shoots one Ray from the center of the small sphere to the current Vertex
Test_Rad = try (length ((intersectRay $Spheres_Big Test_Ray).pos - Root)) catch() --calculates the distance to the surface, if the Ray hits, else undefined
if Test_Rad != undefined do append Test_Arr Test_Rad --stores the distance into the Array, if the Ray hits
)
if amin Test_Arr < Radius do delete o --compares the minimum detected distance to the sphere's radius and, if smaller, deletes the sphere
)
Few Notes:
[ul]
[li]As I said in the other thread, you’ll have to detach every small sphere into a separate object and center their pivots. The big spheres can be a single object.
[/li][li]I havent found a way to get IntersectRay to recognise backfaces. Because of that you need to apply a shell modifier to all spheres (0 strength…you just want to create two-sided geometry), else it will be unable to calculate the radius (I cicumvented this with a Normal Modifier) and will throw an error and interrupt when a small sphere is completely inside a big sphere. When the script finished, you can delete the shel modifiers (apply them all at once, with all object selected. That way you can also delete them at once). If anyone knows a way around that, shoot ;).
[/li][li]The small spheres must be named ‘Sphere_Small_01’ etc, the big sphere’s Mesh ‘Spheres_Big’ (or you’ll have to replace the names in the script with yours).
[/li][li]It only tests against the big spheres. If two small spheres intersect, none of them will be deleted (unless of course, if they also intersect a big sphere).
[/li][li]The script shoots one Ray from the center to each of the spheres Vertices, so, if the polycount of the small spheres is too low, it might not hit one of the big spheres, but then again the sphere probably won’t be near enough to any big sphere to intersect anyhow…
[/li][li]Depending on the number of spheres and Vertices, it will take a while to run…
[/li][/ul]
Hope it works for you…it did in a simple Test Scene…
Hey mate,
thanks a bunch for the script – i will give it a go immediately
and give you feedback!
best regards
NAIK
Something else you could try. All untested, so will have bugs
-- Break the mesh into separate spheres and assign to big and small arrays
bigSpheres = #()
smallSpheres = #()
bigSphereDiameter = 100 -- Change for the size of your spheres
while $bigMesh.numFaces > 0 do
(
temp = editable_mesh()
temp.mesh = meshop.detachFaces $bigMesh (meshop.getElementsUsingFace $bigMesh 1) asMesh:true
temp.pivot = temp.center
if temp.max.x - temp.min.x > bigSphereDiameter then
(
append bigSpheres temp
)
else
(
append smallSpheres temp
)
)
-- Find intersections based on the assumption that the spheres are spherical
for s in bigSpheres do
(
bigRadius = 0.5 * (s.max.x - s.min.x)
for i in smallSpheres.count to 1 by -1 do
(
smallRadius = 0.5 * (smallSpheres[i].max.x - smallSpheres[i].min.x)
dist = distance s smallSpheres[i]
if dist < (bigRadius + smallRadius) then -- If the distance is more than the sum of the radii then there's no intersection
(
delete smallSpheres[i]
)
)
)
Sorry about the indenting, I can never get it to work properly.
Cheers,
Drea
Thanks guys for dropping in.
I got an error message from Pifliks script.
–> – Unknown property : “pos” in undefined
DreaTawn – this error message is from your script
–Unknown property: “numfaces” in undfined
Thanks again guys for your help…
NAIK
you can try this out, this is quick but very inaccurate as it checks by the bounding box.
small = $Sphere_Small_* as array
big = $Sphere_Big_* as array
for b in big do
(
for s in small where not isdeleted s do
(
if (intersects s b) then
delete s
)
)
Note:
- This script requires every element to be separated using the detach to element script mention above. Please detach the big spheres to separate elements.
- Before running make sure that the pivots are centered and resetXform has been done.
In my script, add this line at the top, with xxx replaced with the name of your mesh of spheres.
bigMesh = $xxx
Then find the three places in the code that have “$bigMesh” and remove the “$”. That’s once in the line “while $bigMesh.numFaces > 0 do” and twice in “temp.mesh = meshop.detachFaces $bigMesh (meshop.getElementsUsingFace $bigMesh 1) asMesh:true”
Piflik’s script is expecting you to have separated the spheres and called them “Sphere_Small_” plus some numbers. “Sphere_Small_01” etc.
Cheers,
Drea
I just dont get it…
None of the scripts seems to work properly…
I build a new testscene and will check all three scripts again guys…
Give you feedback immediately…
NAIK
Hmm…the only place where that error can come up is in the first part, where the radius is calculated. If the Ray doesn’t hit anything, it will return undefined and that has no pos property ;).
(If the Objects weren’t named accordingly, the script wouldn’t do anything…if the small spheres had a wrong name, the loop wouldn’t run, if the Big Mesh had the wrong name, the Try/Catch would prevent an error, so naming is not the problem here)
Did you apply the Shell Modifier to all the spheres? I must admit that I didn’t test it with an Shell Modifier, but a Normal Modifier, but I figured it should work with a shell, too. (You can try using a Normal Modifier on the small spheres instead. You’ll definately need two-sided geometry for the big spheres, though. It won’t throw an error if you have only single sided geometry, but it won’t work properly on small spheres which pivots are inside a big sphere.)