PDA

View Full Version : Execute on each object


TedChirvasiu
06-11-2011, 05:43 PM
Hello!Can someone help me with this simple problem,please?
I can't figure out how to run this script FOR Each object in my selection...if i use for obj in selection do (modPanel.addModToSelection (TurboSmooth ()) ui:on) it will add another turboSmooth modifier for each object i have in my selection...I want to add only one turbosmooth modifier to every object in my selection.Please help me :(

denisT
06-11-2011, 11:45 PM
Hello!Can someone help me with this simple problem,please?
I can't figure out how to run this script FOR Each object in my selection...if i use for obj in selection do (modPanel.addModToSelection (TurboSmooth ()) ui:on) it will add another turboSmooth modifier for each object i have in my selection...I want to add only one turbosmooth modifier to every object in my selection.Please help me :(

don't use modPanel.addModToSelection, use addModifier
see mxs help for details

lucpet
06-12-2011, 04:22 AM
for i in selection do
(
addModifier i (TurboSmooth ()) ui:on
)

TedChirvasiu
06-12-2011, 06:45 AM
Oh,Thank you very much ,guys!It worked!
But how to do it if i want to run an entire code block for each object?

subobjectlevel = 4
obj.modifiers[#Edit_Poly].SetSelection #Face #{}
obj.modifiers[#Edit_Poly].Select #Face #{1..16}
obj.modifiers[#Edit_Poly].SetOperation #SetMaterial
obj.modifiers[#Edit_Poly].materialIDToSet = 1
obj.modifiers[#Edit_Poly].Commit ()

obj.modifiers[#Edit_Poly].selectByMaterialID = 1
obj.modifiers[#Edit_Poly].ButtonOp #SelectByMaterial

obj.modifiers[#Edit_Poly].ButtonOp #DetachFace
obj.modifiers[#Edit_Poly].DetachToObject "Objectdude"
obj.modifiers[#Edit_Poly].SetSelection #Face #{}

TedChirvasiu
06-12-2011, 07:41 AM
I did it!

rollout Divide "Divide"(

button divide "Divide"


on divide pressed do(
----------
arr = selection as array


for obj in selection do( addModifier obj (TurboSmooth()) )
for obj in selection do( addModifier obj (Edit_Poly()))

for i = 1 to arr.count do(

max modify mode
select arr[i]
subobjectlevel = 4

arr[i].modifiers[#Edit_Poly].SetSelection #Face #{}
arr[i].modifiers[#Edit_Poly].Select #Face #{1..16}
arr[i].modifiers[#Edit_Poly].SetOperation #SetMaterial
arr[i].modifiers[#Edit_Poly].materialIDToSet = 1
arr[i].modifiers[#Edit_Poly].Commit ()

arr[i].modifiers[#Edit_Poly].SetSelection #Face #{}
arr[i].modifiers[#Edit_Poly].Select #Face #{17..32}
arr[i].modifiers[#Edit_Poly].SetOperation #SetMaterial
arr[i].modifiers[#Edit_Poly].materialIDToSet = 2
arr[i].modifiers[#Edit_Poly].Commit ()

arr[i].modifiers[#Edit_Poly].SetSelection #Face #{}
arr[i].modifiers[#Edit_Poly].Select #Face #{33..48}
arr[i].modifiers[#Edit_Poly].SetOperation #SetMaterial
arr[i].modifiers[#Edit_Poly].materialIDToSet = 3
arr[i].modifiers[#Edit_Poly].Commit ()

arr[i].modifiers[#Edit_Poly].SetSelection #Face #{}
arr[i].modifiers[#Edit_Poly].Select #Face #{49..64}
arr[i].modifiers[#Edit_Poly].SetOperation #SetMaterial
arr[i].modifiers[#Edit_Poly].materialIDToSet = 4
arr[i].modifiers[#Edit_Poly].Commit ()
arr[i].modifiers[#Edit_Poly].SetSelection #Face #{}

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

arr[i].modifiers[#Edit_Poly].selectByMaterialID = 1
arr[i].modifiers[#Edit_Poly].ButtonOp #SelectByMaterial

arr[i].modifiers[#Edit_Poly].ButtonOp #DetachFace
arr[i].modifiers[#Edit_Poly].DetachToObject "Objectdude"
arr[i].modifiers[#Edit_Poly].SetSelection #Face #{}

arr[i].modifiers[#Edit_Poly].selectByMaterialID = 2
arr[i].modifiers[#Edit_Poly].ButtonOp #SelectByMaterial

arr[i].modifiers[#Edit_Poly].ButtonOp #DetachFace
arr[i].modifiers[#Edit_Poly].DetachToObject "Objectdude"
arr[i].modifiers[#Edit_Poly].SetSelection #Face #{}


arr[i].modifiers[#Edit_Poly].selectByMaterialID = 3
arr[i].modifiers[#Edit_Poly].ButtonOp #SelectByMaterial

arr[i].modifiers[#Edit_Poly].ButtonOp #DetachFace
arr[i].modifiers[#Edit_Poly].DetachToObject "Objectdude"
arr[i].modifiers[#Edit_Poly].SetSelection #Face #{}


arr[i].modifiers[#Edit_Poly].selectByMaterialID = 4
arr[i].modifiers[#Edit_Poly].ButtonOp #SelectByMaterial

arr[i].modifiers[#Edit_Poly].ButtonOp #DetachFace
arr[i].modifiers[#Edit_Poly].DetachToObject "Objectdude"


--End by deleting the object
delete arr[i]

)
)
)

createdialog divide



The array fixed it!Thank you for your help,guys :)

:banghead: However,can someone help me improve it?My computer always freezes if there are more than 128 chunks :(
Obviously,this is not the "healthiest" code :S
What can i do to improve performance? :banghead:

lucpet
06-12-2011, 08:54 AM
I just tried your code on 200 boxes and it did whatever this code does without any problems and I ended up with 484 objects.

For better explanation on why it's freezing at 128 you may have to upload the file you are working on.

Making your code better I'll leave to people like DenisT I'm not anywhere near his level and don't know the answer myself :blush: You seem to be doing pretty well anyway.
Don't forget "The Area" and scriptspot.com for other places to ask these questions

Well done and good luck

Swordslayer
06-12-2011, 01:57 PM
There might be a few things:

1. use code tag when posting code
2. try to use different variables (rollout divide, button divide - that's not a good practice)
3. don't do it in three loops when you can do it in one
4. you've already saved the node collection in the arr variable, there's no reason to loop over live selection set then (for obj in selection ...)
5. use variables to store things you need to access a lot, (like the Edit_Poly modifier)
6. hand in hand with this one goes: if you only change a few things and repeat the rest, make functions out of it instead of copy-pasting it
7. do you need the mat ID there actually or you just use it to select pieces of mesh to be detached? If it's the latter, there are few much more elegant ways to do that, like (pseudocode): while numberOfFaces > 16 do detachFaces #{1..16} This will work because everytime you delete some faces, the face indices are reset. In this case I've limited the loop to 4 iterations as in you original code.
8. as you're deleting the original mesh, what's the point in using Edit_poly modifier in the first place? It would be so much faster using polyOps or meshOps as they don't need to have modifier panel open. I'll leave this one for you to play with, though ;)

try destroyDialog divide catch()
rollout divide "Divide"
(
button btnDivide "Divide"

fn setMatAndDetach mod id =
(
mod.Select #Face #{1..16}
mod.SetOperation #SetMaterial
mod.materialIDToSet = id-1
mod.Commit()
mod.DetachToObject "Objectdude"
)

on btnDivide pressed do
(
max modify mode
local arr = selection as array
addModifier arr (TurboSmooth()) -- here or in the for loop, you choose

for obj in arr do with redraw off
(
local ePolyMod = Edit_Poly()
addModifier obj ePolyMod
select obj
subObjectLevel = 4
ePolyMod.SetSelection #Face #{}

for nr = 1 to 4 do setMatAndDetach ePolyMod nr

delete obj
)
)
)
createDialog divide

CGTalk Moderation
06-12-2011, 01:57 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.