ScriptSpot home

Forum topic · General Scripting

Please, need help me optimising this script

By Saij · 2013-01-09

Description

Hi everyone,

I am new to max scripting and need help optimizing this script. Although this script works as intended I can't but help to think that there must be a better way. That is however beyond my skills to identify.

I wrote this script to check naming conventions.

1- go through everything in the scene

2- check the if the names are matching the given names i.e.(ch_*_A_*, Set_*_A_*, Prop_*_A_*, etc)

3- select everything that are not matching the giving names

4- isolate it

beforehand thank you all for helping a noob out.

/*
Version: v1.1
Purpose: Check scene for names:

CH_*_A_*
CH_*_S_*
CH_*_M_*

Set_*_A_*
Set_*_S_*
Set_*_M_*

Prop_*_A_*
Prop_*_S_*
Prop_*_M_*

Select and isolate everything that does not fall within the parameters
*/

macroScript NameChecker
category:"Custom Tools"
icon:#("enss_maintoolbar",1)
(
fn Check=
(
nameList = stringStream ""

-- putting things in arrays
local Array1 = $ch_*_a_* as array
local Array2 = $ch_*_s_* as array
local Array3 = $ch_*_m_* as array

local Array4 = $set_*_a_* as array
local Array5 = $set_*_s_* as array
local Array6 = $set_*_m_* as array

local Array7 = $prop_*_a_* as array
local Array8 = $prop_*_s_* as array
local Array9 = $prop_*_m_* as array

-- selecting arrays
select Array1
selectmore array2
selectmore array3

selectmore array4
selectmore array5
selectmore array6

selectmore array7
selectmore array8
selectmore array9

-- putting all selected into a new array
tarObj = selection as array

-- select everything in the scene, deselect everything in tarObj array and unhide selection
select $*
deselect tarObj
selection.ishidden = off

-- isolate selection
macros.run "Tools" "Isolate_Selection"

for o in selection do print (o.name as string) to:nameList
messageBox (nameList)
)

-- create GUI
rollout rollout_NameChecker "Name Checker " width:200
(
-- create button
group "Checks"
(
button squashBtn "Check Names!"
)

-- call on function check()
on squashBtn pressed do
(
check ()
)
)
createdialog rollout_NameChecker style:#(#style_titlebar,#Style_toolwindow,#style_sysmenu)
)

Comments (12)
Saij

Thank you all!

Thank you all,

i learned a lot from you guys. i have changed the code slightly to make it do what i want it to do, here is the final code:

names = #("CH_*_A_*", "CH_*_S_*", "CH_*_M_*", "Set_*_A_*", \
"Set_*_S_*", "Set_*_M_*", "Prop_*_A_*", \
"Prop_*_S_*", "Prop_*_M_*")

theObjs = #()

for obj in objects do
(
tmp = for str in names where matchPattern obj.name \
pattern:str ignoreCase:true collect obj join theObjs tmp
)

select $*
deselect theObjs
macros.run "Tools" "Isolate_Selection"

Again thank you all

Panayot Karabakalov

hm, is this better?

names = #("CH_*_A_*", "CH_*_S_*", "CH_*_M_*", \
	"Set_*_A_*", "Set_*_S_*", "Set_*_M_*", \
	"Prop_*_A_*", "Prop_*_S_*", "Prop_*_M_*")
theObjs = #()

for obj in objects do (
	tmp = for str in names where NOT \
	matchPattern obj.name pattern:str \
	ignoreCase:true collect obj
	join theObjs tmp
)

select theObjs
macros.run "Tools" "Isolate_Selection"
fajar

maybe you could post a scene here....um just clear the scene from unwanted object and reduce the the filesize.....

Reply to fajar
Saij

ill post a mock scene

ill post a mock sene later tonight

miauu

Or, using the fajar snippet, instead of selecting all objects and then deselecting each object in mySel array you can use this

[code]

(
fn Check =
(
names=#("CH_*_A_*","CH_*_S_*","CH_*_M_*","Set_*_A_*","Set_*_S_*", "Set_*_M_*","Prop_*_A_*","Prop_*_S_*","Prop_*_M_*")

mySel = undefined
for i in objects do
(
mySel = for o in names where matchPattern i.name pattern:("*"+o+"*") ignoreCase:true collect i
)
select mySel

if (maxVersion())[1]>=15000 then
(
IsolateSelection.IsolateSelectionModeActive()
)
else
(
macros.run "Tools" "Isolate_Selection"
)
)
Check()
)
[code]

Reply to miauu
Saij

hi miauu

thanks for helping me out,
this version of the script dosn't seems to work.
it returns undefined when i run it

fajar

did you already my script i wrotte for you ?

Reply to fajar
Saij

I tried your script but it didn't do what I need it to. It selected a few objects but left some of them out. I'll take a few screenshots ill post them later.

Reply to fajar
Saij

I tried your script but it didn't do what I need it to.

fajar

hmm...dont know what youre trying to accomplish...but if my mind correct you want to isolate all object in scene exclude theNames you mention....if then ....try


fn Check=
(
names=#("CH_*_A_*","CH_*_S_*","CH_*_M_*","Set_*_A_*","Set_*_S_*", "Set_*_M_*","Prop_*_A_*","Prop_*_S_*","Prop_*_M_*")

max select all

for i in selection do
 (
 	MySel=for o in names where matchPattern i.name pattern:("*"+o+"*") ignoreCase:true collect i
	deselect MySel
 )
 
if (maxVersion())[1]>=15000 then 
(
	IsolateSelection.IsolateSelectionModeActive()
)
else 
(
	macros.run "Tools" "Isolate_Selection"
)
)
Reply to fajar
Saij

hi fajar,

thanks for you response, i should have been more clear of what i needed.
this is a script that i wrote to check naming in a scene.

1- go through everything in the scene

2- check the if the names are matching the given names i.e.(ch_*_A_*)

3- select everything that are not matching the giving names

4- isolate it

the way i have done it is a very rudimentary:

-i selected the names and put then into its own array.

-then i selected the array using select(), with that selected i select the next one with selectMore() so that i have the original selection plus my second selection.

-when everything correct are selected then i pass it into a new array tarObj.

-after that i select everything in the scene and then deselect everything from tarObj. leaving me with objects that are not named correctly.

-finally isolate those objects.

that is how my script works, and it does what i want it to do but it feels very dragged out, so i am posting this to see if there is a better way to make the script do the same thing but more efficient.

but thanks for taking time to help me i appreciate it