Forum Groups
  All forums
    Help & Feedback
      Questions
      Work in progress
      Finished Art
      Non-Max related
    Community
      Offtopic
      News
    Hardware
    Photography



Maxunderground news unavailable

How can i speed up/optimise/make my script more efficient?
show user profile  jpedleham
Hey, at the moment this script takes quite a while to execute. bearing in mind that theres about 3500 objects in my scene. Is there a cleaner way to go about something like this?

Would it be simpler to select the objects by name and apply the modifier like that rather than running every object through the entire script?

select objects
for obj in selection do
(

if matchpattern obj.name pattern:"*glazed*" do
(
(addModifier Obj (materialModifier materialID:2 name:"RevitMaterialMod"))
)



if matchpattern obj.name pattern:"*nonslip*" do
(
(addModifier Obj (materialModifier materialID:3 name:"RevitMaterialMod"))
)



if matchpattern obj.name pattern:"*surestep*" do
(
(addModifier Obj (materialModifier materialID:4 name:"RevitMaterialMod"))
)



if matchpattern obj.name pattern:"*carpet*" do
(
(addModifier Obj (materialModifier materialID:5 name:"RevitMaterialMod"))
)



if matchpattern obj.name pattern:"*railing*" do
(
(addModifier Obj (materialModifier materialID:6 name:"RevitMaterialMod"))
)



if matchpattern obj.name pattern:"*roof*" do
(
(addModifier Obj (materialModifier materialID:7 name:"RevitMaterialMod"))
)



if matchpattern obj.name pattern:"*sarnafil*" do
(
(addModifier Obj (materialModifier materialID:8 name:"RevitMaterialMod"))
)



if matchpattern obj.name pattern:"*hygenic*" do
(
(addModifier Obj (materialModifier materialID:10 name:"RevitMaterialMod"))
)



if matchpattern obj.name pattern:"*fawn*" do
(
(addModifier Obj (materialModifier materialID:9 name:"RevitMaterialMod"))
)




if matchpattern obj.name pattern:"*Terrazzo*" do
(
(addModifier Obj (materialModifier materialID:11 name:"RevitMaterialMod"))
)



if matchpattern obj.name pattern:"*Forbo*" do
(
(addModifier Obj (materialModifier materialID:12 name:"RevitMaterialMod"))
)


if matchpattern obj.name pattern:"*RAL*" do
(
if matchpattern obj.name pattern:"*9006*" do
(
(addModifier Obj (materialModifier materialID:15 name:"RevitMaterialMod"))
)

if matchpattern obj.name pattern:"*7016*" do
(
(addModifier Obj (materialModifier materialID:14 name:"RevitMaterialMod"))
)

if matchpattern obj.name pattern:"*7012*" do
(
(addModifier Obj (materialModifier materialID:13 name:"RevitMaterialMod"))
)

if matchpattern obj.name pattern:"*7035*" do
(
(addModifier Obj (materialModifier materialID:16 name:"RevitMaterialMod"))
)
)





if matchpattern obj.name pattern:"*timber*" do
(
(addModifier Obj (materialModifier materialID:17 name:"RevitMaterialMod"))
)


if matchpattern obj.name pattern:"*foundation*" do
(
(addModifier Obj (materialModifier materialID:18 name:"RevitMaterialMod"))
)


if matchpattern obj.name pattern:"*Cast-In-Place*" do
(
(addModifier Obj (materialModifier materialID:18 name:"RevitMaterialMod"))
)


if matchpattern obj.name pattern:"*CONCRETE*" do
(
(addModifier Obj (materialModifier materialID:18 name:"RevitMaterialMod"))
)


if matchpattern obj.name pattern:"*slab*" do
(
(addModifier Obj (materialModifier materialID:18 name:"RevitMaterialMod"))
)

if matchpattern obj.name pattern:"*PALISADE*" do
(
(addModifier Obj (materialModifier materialID:6 name:"RevitMaterialMod"))
)

if matchpattern obj.name pattern:"*MONOLITHIC*" do
(
(addModifier Obj (materialModifier materialID:18 name:"RevitMaterialMod"))
)


if matchpattern obj.name pattern:"*Walnut*" do
(
(addModifier Obj (materialModifier materialID:19 name:"RevitMaterialMod"))
)


if matchpattern obj.name pattern:"*Wall*" do
(
if matchpattern obj.name pattern:"*Tile*" do
(
if matchpattern obj.name pattern:"*brick*" do
(
(addModifier Obj (materialModifier materialID:20 name:"RevitMaterialMod"))
)
)
)



)
)


read 301 times
3/8/2016 12:08:49 PM (last edit: 3/8/2016 12:17:18 PM)
show user profile  Garp
First off, instead of
select objects
for obj in selection do

you could write
for obj in objects do
Why do you want to select them?

3500 objects is not that much.
Is it worth spending hours optimizing if it saves only a handful of seconds? How long is it taking right now?

Your nested ifs at the end look weird.
You're adding a material only if the object's name contains "Wall" and "Tile" and "brick".
Is that correct?




read 291 times
3/8/2016 1:02:16 PM (last edit: 3/8/2016 1:09:05 PM)
show user profile  jpedleham
Fair point on the selection
3500 is the very minimum. i suppose realistically it could be upwards of 7500 objects but its a process that we in my team have to do weekly. At the moment it freezes up and takes about 10 minutes.

The nested ifs seem to work but i dont know how to use multiple arguments any other way.
at the moment it checks if its a wall then looks if it needs a tile texture and then if its brickbonded tile. later on there will be other types of tile and wall finishes to add to that statement.
Im adding a materialID (based on an existing multisub) to the object if it includes certain stuff yeah.
Its for large scale revit buildings that we look at in realtime software and the materials are always the same so it seems a waste of time manually processing the entire building every time.
read 276 times
3/8/2016 4:34:44 PM (last edit: 3/8/2016 4:38:18 PM)
show user profile  Garp
There isn't much room for optimization, I'm afraid.
From your code I gather that the substrings are not at a fixed position in the names. If they were, you could sort things first and then go much faster.

One thing you could do that 'might' speed things a little is to group operations. Check all the patterns first, then assign all the materials. This would require measuring, as it might end up making no difference or even slowing things down.

You could store your substrings in an array and have a second array of identical size storing the corresponding material IDs. For some index i, you would have matIDs[i] corresponding to patterns[i].
Then you'd have yet another array that you'd fill up as you traverse the objects with the appropriate IDs, again with matching indices. And then a second traversal were you assign the materials.
This would separate the pattern matching part from the material additions.

If it's not faster, at least it should make your code clearer and easier to extend with more patterns and material IDs.




read 268 times
3/8/2016 5:20:10 PM (last edit: 3/8/2016 5:24:41 PM)
show user profile  gogodr
Use If Else
otherwise it will keep testing on the same object over and over again even though you already did what you wanted

Hello there

beautiful ;3


read 260 times
3/8/2016 5:56:19 PM (last edit: 3/8/2016 5:56:19 PM)
show user profile  jpedleham
^Good call.

I assume its

If obj.name pattern:"*Wall*" do

ACTION

Else

ACTION

Else

Also garp that sounds like a decent suggestion but this code might need to be edited in the future by someone else and i think having an easily understandable bit of code that can just be copied and pasted to add another value might be slightly easier to grasp.

The substrings unfortunately could be anywhere within the name as revit naming gets quite messy containing things like dimensions, material, front finish, manufacturer etc so its a long shot that they all name things even with the right words.
read 254 times
3/8/2016 8:01:33 PM (last edit: 3/8/2016 8:01:33 PM)
show user profile  jpedleham
I tried the else statement but couldnt get it working, is there something i can put at the end of each if statement that will tell max to go to the next object?

So once it has found its material it doesnt keep looking?

EDIT: Nevermind i got it running in under two minutes so thats pretty quick on a revit building with 12k objects!
read 241 times
3/9/2016 10:34:08 AM (last edit: 3/9/2016 12:59:19 PM)
show user profile  jpedleham
Thanks for the help guys!
read 236 times
3/9/2016 1:17:48 PM (last edit: 3/9/2016 1:39:37 PM)
show user profile  jpedleham
Im getting a weird problem where the modifier panel becomes completely disabled. no buttons work and when i select an object no name appears but it appears in the other panels. Any ideas?
read 228 times
3/9/2016 2:13:05 PM (last edit: 3/9/2016 2:13:05 PM)
#Maxforums IRC
Open chat window


Support Maxforums.org