Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Attach Visible objects with same material by 300

Attach Visible objects with same material by 300

n_rangelov
Participant Participant
1,374 Views
15 Replies
Message 1 of 16

Attach Visible objects with same material by 300

n_rangelov
Participant
Participant

Hello  i  modified a little bit  the  original script ( Attach by Same Material  )

,so it will attach only the  visible objects in the scene .

 

I have the following script 

 ---------------------------------------

 

on attach pressed do
(  
 
 
if selection.count == 0 then (messageBox "Nothing selected. To use, select one object and run the script to attach all other objects with the same material.")
else if selection.count == 2 then (messageBox "Multiple objects selected. To use, select one object and run the script to attach all other objects with the same material.")
else if selection.count == 1 then
(
mainobject = $
 
(
macros.run "Modifier Stack" "Convert_to_Poly"
mat = mainobject.material
 
for o in objects where not o.isHiddenInVpt do
 
(
--print o.material
if o.material == mat then 
(
-- print "yes"
mainobject.EditablePoly.attach o mainobject
) ) ) ) )
---------------
But cannot figure it out ,how  to  make the attach to be by 300 objects in one part   - similar to the  "quick attach  ". 
 

 

0 Likes
Accepted solutions (1)
1,375 Views
15 Replies
Replies (15)
Message 2 of 16

denisT.MaxDoctor
Advisor
Advisor
fn attachAllToOneByMaterial node:selection[1] =
(
	if iskindof node Editable_Poly do
	(
		for n in geometry where not n.isHiddenInVpt and n != node and n.mat == node.mat do polyop.attach node n 
		node
	)
)

/******* just run with selected target object 

attachAllToOneByMaterial()

*/

 

We could, of course, add some "foolproof" tests, or use some smarter method for a quick attach, but everything should work as it is.

 

0 Likes
Message 3 of 16

n_rangelov
Participant
Participant

Hello and thanks for the fast answer.

It's working,  but  how can i make it to go by batches of  300 objects  and then finishing in one.

Thanks again

0 Likes
Message 4 of 16

denisT.MaxDoctor
Advisor
Advisor

@n_rangelov wrote:

It's working,  but  how can i make it to go by batches of  300 objects  and then finishing in one.

 



 

fn attachAllByCount count:100 mat: nodes: =
(
	if nodes == unsupplied do nodes = for node in geometry where not node.isHiddenInVpt collect node
	
	if iskindof mat Material do
	(
		nodes = for node in nodes where node.mat == mat collect node
	)
	
	InstanceMgr.MakeObjectsUnique nodes #individual
	
	targets = #()
	target = undefined
	for k=1 to nodes.count while not keyboard.escPressed do
	(
		if (mod k count) == 1 then
		(
			target = nodes[k]
			converttopoly target
			append targets target
			format ">> %\n" (formattedPrint targets.count format:"04d")
		)
		else
		(
			polyop.attach target nodes[k] 
		)
	)
	targets 
)

-- You have to specify the target material yourself 
-- >>> For example:

m = objects[1].material 

--  You have to specify number of group to attach, material, and nodes
-- >>> By default all visible nodes that maches a specified material are used 

attachAllByCount count:100 mat:m

 

0 Likes
Message 5 of 16

denisT.MaxDoctor
Advisor
Advisor

For very large scenes, you need a better attachment algorithm (which exists) and perhaps some user interface for "entertaining" during the process (e.g. add a progress bar).

0 Likes
Message 6 of 16

n_rangelov
Participant
Participant
I did modify it like that 
but now i have an issue when i pick a object without material
all visible objects ( with or without material ) are attached to the first object  .
 
 
 
 
fn attachAllByCount count:100 mat:m  nodes: =
(
if nodes == unsupplied do nodes = for node in geometry where not node.isHiddenInVpt collect node
 
if iskindof mat Material do
(
nodes = for node in nodes where node.mat == mat collect node
)
 
InstanceMgr.MakeObjectsUnique nodes #individual
 
targets = #()
target = undefined
for k=1 to nodes.count while not keyboard.escPressed do
(
if (mod k count) == 1 then
(
target = nodes[k]
converttopoly target
append targets target
format ">> %\n" (formattedPrint targets.count format:"04d")
)
else
(
polyop.attach target nodes[k] 
)
)
targets 
)
 
-- You have to specify the target material yourself 
-- >>> For example:
 
m = $.material 
 
 
--  You have to specify number of group to attach, material, and nodes
-- >>> By default all visible nodes that maches a specified material are used 
 
attachAllByCount count:100 mat:m
0 Likes
Message 7 of 16

denisT.MaxDoctor
Advisor
Advisor

@n_rangelov wrote:
 
but now i have an issue when i pick a object without material
all visible objects ( with or without material ) are attached to the first object  .
 

you have a different problem... you don't read what I write to you in comments and don't try to understand how the code works.

0 Likes
Message 8 of 16

n_rangelov
Participant
Participant

I read everything  that you wrote twice. To  be honest i did my best to understand the code and i am sorry couldn't .

And before i post it here i was trying to do it by myself  but couldn't  do it.

 

 

 

0 Likes
Message 9 of 16

denisT.MaxDoctor
Advisor
Advisor

@n_rangelov wrote:

I read everything  that you wrote twice. To  be honest i did my best to understand the code and i am sorry couldn't .

And before i post it here i was trying to do it by myself  but couldn't  do it.

 

 

 


denisTMaxDoctor_0-1689101607088.png

 

0 Likes
Message 10 of 16

n_rangelov
Participant
Participant

I already changed  that line from :

m = objects[1].material

to

m = $.material

 

 

 

0 Likes
Message 11 of 16

denisT.MaxDoctor
Advisor
Advisor
Accepted solution

@n_rangelov wrote:
but now i have an issue when i pick a object without material
all visible objects ( with or without material ) are attached to the first object  .
 

denisTMaxDoctor_0-1689106712069.png

 

Comment out this line if you want to treat any mat value, including undefined, as a condition.

 

0 Likes
Message 12 of 16

n_rangelov
Participant
Participant

Thank you for your help  i got to here.

 For progress bar i have no idea .

preview.jpg

 

(
try destroyDialog same catch()


rollout nosel "" width:230 height:80
(

label typeH2U02 "

First select object and then run the script.

" 
width:230 height:50 align:#left

)
createDialog nosel


z=300





fn attachAllByCount count:z  mat:  nodes: =
(
if nodes == unsupplied do nodes = for node in geometry where not node.isHiddenInVpt collect node


nodes = for node in nodes where node.mat == mat collect node


InstanceMgr.MakeObjectsUnique nodes #individual

targets = #()
target = undefined
for k=1 to nodes.count while not keyboard.escPressed do
(
if (mod k count) == 1 then
(
target = nodes[k]
converttopoly target
append targets target
format ">> %\n" (formattedPrint targets.count format:"04d")
)
else
(
polyop.attach target nodes[k] 
)
)
targets 
)

-- You have to specify the target material yourself 
-- >>> For example:

m = $.material

rollout same "Attach by Same Material" width:260 height:330


(

label typeH2U02 "

Attach all Visible objects by the same Material.

" 
width:230 height:60 align:#left

Group "How to Use"
( 
label typeH2U04 "

After selecting a object and pressing the Attach button  
all visible objects in the viewport (sharing the same material ),
will be attached to the original object.


" 
width:230 height:100 align:#left
)





spinner 'spn1' "Number of elements" pos:[6,205] width:100 height:16 enabled:true range:[1,400,300] type:#integer align:#left
button attach "Attach" width:100 height:50 align:#center





on attach pressed do
(
z= same.spn1.value
 attachAllByCount count:z  mat:m 

)

)


if selection.count != 0 then (
try destroyDialog nosel catch() ;
createDialog same ;

) 
)

 

 

0 Likes
Message 13 of 16

denisT.MaxDoctor
Advisor
Advisor

what is the point of attaching objects by groups of a certain count?

 

I can understand the idea of collecting by the maximum number of vertices or elements, or by the bounding box, by the world region... but the number of members... why? what for?

0 Likes
Message 14 of 16

denisT.MaxDoctor
Advisor
Advisor

@n_rangelov wrote:
denisTMaxDoctor_0-1689233877640.png

 


use this button

denisTMaxDoctor_1-1689233983661.png

to insert the code

0 Likes
Message 15 of 16

n_rangelov
Participant
Participant
I was told that when attaching a big scene that way is using less memory .
0 Likes
Message 16 of 16

denisT.MaxDoctor
Advisor
Advisor

Who cares about memory these days?

But if you want to better organize a scene, there are groups, containers, xref scenes.... Attaching is a last resort. Very often it is much easier to assemble something than to disassemble it back.

0 Likes