Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk 3ds® Max® / MaxScript / Basic script example
  RSS 2.0 ATOM  

Basic script example
Rate this thread
 
37871
 
Permlink of this thread  
avatar
  • Total Posts: 68
  • Joined: 21 January 2008 11:30 PM

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.



Replies: 0
avatar
  • IonDave
  • Posted: 17 December 2009 04:57 AM

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 [000] -- 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!



~Dave

http://www.max-realms.com - 3ds Max models, tutorials and forums

Max3-2010

Replies: 0
avatar
  • Location: West Midlands, England, UK
  • Total Posts: 14445
  • Joined: 06 August 2007 11:06 PM
  • Permlink of this post

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 4.2 through 2013.
XP-64 (SP2)
NVidia 9800GTX-512 (Driver 266.58).
Core 2 Quad Q6600 2.4GHz, 8Gb Ram, DX9.0c.

Replies: 0
avatar

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
)



Replies: 1
/img/forum/dark/default_avatar.png

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 ...

[edit] But see Anubis’s post below as well [/edit]

Author: Steve_Curley

Replied: 17 December 2009 10:11 AM  
avatar
  • IonDave
  • Posted: 17 December 2009 09:37 AM

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.



~Dave

http://www.max-realms.com - 3ds Max models, tutorials and forums

Max3-2010

Replies: 0
avatar
  • Anubis
  • Posted: 17 December 2009 10:21 AM

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
)


Max 9 through 2009, XP-Pro x64 SP2
ASUS EAH3450 Series (Driver 8.470).
Core 2 Duo E8400 3GHz, 4Gb Ram, DX9.0c.

Replies: 2
/userdata/avatar/234tptgj3_159515783649ebfe783a8f3.png

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?

Author: IonDave

Replied: 17 December 2009 10:26 AM  
/img/forum/dark/default_avatar.png

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 do
(
 
-- the base of the speed up trick is that
 
-- Max will not read the code here if selection is empty
)
Author: Anubis

Replied: 17 December 2009 10:29 PM  
avatar

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.



Replies: 0
avatar

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

;)

-Johan



Replies: 1
/img/forum/dark/default_avatar.png

The same question came to my mind at the beginning but I assumed that Charles wish to enjoy learning MaxScript :)

Author: Anubis

Replied: 18 December 2009 02:11 AM  
avatar
  • IonDave
  • Posted: 18 December 2009 04:28 AM

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



~Dave

http://www.max-realms.com - 3ds Max models, tutorials and forums

Max3-2010

Replies: 0