Notifications
Clear all

[Closed] Detach by Element

Im having a weird issue with a detach by elements script.

In some files it deletes an object of a group that im trying to split up in separate elements.

This is the snippet of the script:

	(
		--detach all elements to objects
		o = $
		pName = o.name
		while polyOp.getNumFaces $ > 1 do
			(
			p = PolyOp.getElementsUsingFace $ 1
			polyOp.detachFaces $ p asNode:true name:"detachedObject01"
			)
			(
			select $detached*	
			for p in selection do
				(
				p.pivot = [((p.max.x + p.min.x)/2),((p.max.y + p.min.y)/2),p.min.z]
				p.name = uniquename (pName)
				)
			)
		delete o
	)	

With the part [delete o] disabled and running the script again the same objects get divided but there is another extra thing left that doesnt contain any objects or polys.

I have attached the scene where it happens. One of the objects suddenly gets deleted.

Also if I just create a new scene with new objects the script works fine, so it there something wrong with the file or the script?

thanks in advance

4 Replies

i see the only one real problem in your code: the face number condition in while loop has to be > 0 (not > 1).
i cleaned the code a bit to make it faster and more memory friendly:


fn detachSeparate node name: delit:off = if iskindof node Editable_Poly do
(
	local act = on
	local element = polyop.getElementsUsingFace
	local detach = polyop.detachfaces
	while node.numfaces > 0 and act do
	(
		elem = element node 1
		act = detach node elem delete:on asnode:on name:name
		if act do 
		(
		    obj = objects[objects.count]
			centerPivot obj -- or whatever you need
			if not delit do obj.parent = node
		)
	)
	if delit and (node.numfaces == 0) do delete node
)

  1. You should make sure the selection count is one, or only collect the first object in the selection array.
  2. You should name the detachedfaces as name:(uniquename “detachedobject01”) to ensure it is a unique name to begin with.
  3. You should also probably delete the detachedobject01 object as well.
  4. Once you delete o, then set o = undefined to remove any reference of the now deleted object.

Just a few thoughts,
-Eric

I see enough useful replies here but you can check my script as well:
http://www.scriptspot.com/3ds-max/scripts/detach-elements

I’m sure it can be optimized more but still hold some useful things.
For ex. enumerated naming is more faster than automatic or UniqueName().

Thank you for all your responses, script works perfectly now.

@DenisT: Thank you for providing a faster code Denis, the code im using now is indeed a tad slow when it involves a lot of objects. Even if its only 10000 polys

@PixelMonkey: Thanks for your thoughts, will keep them in mind.

@Anubis: Hi Anubis, love your scripts. Using some of your scripts right now, didnt know you also made an element splitter and a attachmeshbymat, very handy, keep up the great work