Basic script example

Basic script example

cprettyman
Collaborator Collaborator
936 Views
12 Replies
Message 1 of 13

Basic script example

cprettyman
Collaborator
Collaborator
I'm very much a novice at scripting, in any language, so please forgive me if this is too basic a q.

I need to write what I suspect is an absurdly simple script. If I have a set of objects select (it's a large set, about 1500 objects), that all happen to be splines, I need to make them all renderable, spline (enable in renderer), regctangular, with a a specific set of dimensions (which we're still trying to determine).

I'm not asking anyone to write this for me, but it seems to me that performing the same properites change on a large number of objects is probably such a common scripting thing that someone probably has a sample sitting handy that they could offer as an example, and then I will work through it to make it do what I need (a good exeercise for my self education anyway)

thanks so much.
0 Likes
937 Views
12 Replies
Replies (12)
Message 2 of 13

Anonymous
Not applicable
Whenever you want to change the same thing over and over on a specific selection, you use a "for" loop. Like this:


for obj in selection do
(
obj.pos = -- Move all objects to 0,0,0
obj.wirecolor = red -- Change the wireframe color to red
--etc.
)


I think in your case creating the shapes in the script would be more efficient than modifying an existing set of shapes...if possible.

The MAXScript Help files have lots of example "How To" sections that should be enough to get you started. Good luck!
0 Likes
Message 3 of 13

Steve_Curley
Mentor
Mentor
Ok, if you want to write it yourself...

Things you'll need to know about.
For loops.
Collections.
The names of the object properties you want to change. Editable Spline objects are called "spline shapes" in Maxscript.

Iterate over the collection, each time around the loop set the rquired properties.

There is another way, but it's a little more cryptic (mapped functions).

Read up on all the above in the Maxscript help - if you get well and truly stuck, post back and I'll try and help you out.
Also, there are innumerable examples of this kind of procedure in this very forum - looking through some recent (and not so recent) threads should provide a fair overview, though not necessarily the specifics, of what you're trying to do.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 4 of 13

cprettyman
Collaborator
Collaborator
I think I got it. Im sure there are subtleties I could add - some sort of error checking, an if statement to make sure everything in the selection set is a spline. But, this seems to work.


for obj in selection do
(
obj.render_renderable = true
obj.render_rectangular = true
obj.render_length = 0.5
obj.render_width = 1.0
)
0 Likes
Message 5 of 13

Anonymous
Not applicable

for obj in selection do
(
if (classof obj == Editable_Spline) then
(
obj.render_renderable = true
obj.render_rectangular = true
obj.render_length = 0.5
obj.render_width = 1.0
)-- End If
else
(
print "Error. This object is not of the class \"Spline\""
return undefined
)-- End Else
)-- End For


I'm not at home, so you'll have to check if "Editable_Spline" is the correct class name.
0 Likes
Message 6 of 13

Steve_Curley
Mentor
Mentor
That's the basis of it, yes. You could put in a test as in Dave's post below, or you could create your own collection based on the current selection and the test. However, I think you need a double test, because classof a Line is "Line" (even if you "convert to Editable Spline") but any other shape like a rectangle (converted) is a "SplineShape".

mySplines = for obj in selection where (classof obj == Line or classof obj == SplineShape) collect obj

Then iterate over that with

for obj in mySplines do ...


But see Anubis's post below as well

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 7 of 13

Anonymous
Not applicable
2 filtering tips using "where":
for obj in selection where isShapeObject obj do (
obj.render_renderable = true
)

for obj in shapes where obj.isSelected do (
obj.render_renderable = true
)
0 Likes
Message 8 of 13

Anonymous
Not applicable
That is indeed more concise. Is there a specialized way to add error checking for this example other than try/catch? Or are we assuming that a selection error should not be encountered with this filter?
0 Likes
Message 9 of 13

cprettyman
Collaborator
Collaborator
You guys are all awesome.

I might note that the context of this is a model that came from Revit (via FBX) to Max, and what I am doing is adddressing the muntins that divide up the windows. They were represented as lines in the revit model, and end up as splines in the Max model. I can select them all inteh select by name window becuse they all have "muntin" in the name. Conveniently, the Revit model was put together by a very careful, consistent team, so I can get away with less error checking.

That said, I appreciate the thoughts, because I can see myself reworking this script for later, less well built models where error checking is badly needed.

Thanks again.
0 Likes
Message 10 of 13

Anonymous
Not applicable
That is indeed more concise. Is there a specialized way to add error checking for this example other than try/catch? Or are we assuming that a selection error should not be encountered with this filter?

I always avoid Try/Catch 'cause its slow. And my code above will not need additional error checking. Well, then I work on selection I check it first and this is not to debbug but just to make the code faster. Im talking about this:


sel = selection as array
if sel.count > 0 do
(
-- the base of the speed up trick is that
-- Max will not read the code here if selection is empty
)
0 Likes
Message 11 of 13

Anonymous
Not applicable
And the obvious question offcourse, why don't you just select all spline objects and apply a renderable spline modifier?

;)

-Johan
0 Likes
Message 12 of 13

Anonymous
Not applicable
The same question came to my mind at the beginning but I assumed that Charles wish to enjoy learning MaxScript :)
0 Likes
Message 13 of 13

Anonymous
Not applicable
Anubis has made a great blog post on the subject of optimized selection checking and processing here: http://3dmyths.blogspot.com/2009/12/includeexclude-in-for-loops-via-where.html
0 Likes