OOP (Pseudo Object Oriented Programming) in MaxScript

Hi all:

There is some time i have not written in the blog, but i expect that is gonna change currently. I have a bunch of projects at the moment and i guess that i am gonna have some interesting stuff to show you all early. One of them is going to be developed under 3DS Max using MaxScript. Maybe in future versions of 3DS Max i will develop it using Python ( let’s see how the API evolves 😉 )

So, this article tries to explain a way of using MaxScript Struct as an Object ( Object Oriented Programing ). The main reason to do that is to organize our code and our information structures inside MaxScript, cause when a script start to grow and become really large, our organization is one of the most important things to understand what we are doing, write less mistakes, find mistakes faster, and let other people to read and understand our code.

An Struct allow us to store a couple of variables together, as they have some relationship between them. Inside MaxScript Help, we could read some code like this one:

struct Person(name, age, gender)
me = Person name:"Gorka" age:31 gender:#male
format "% - % - %n" me.name me.age me.gender
you = ()
format "% - % - %n" you.name you.age you.gender

We have just define an Struct named Person that contains three variables: name, age and gender (public by default). Next line creates an instance of this Struct, and next one prints the information contained within Person Structme‘. Last two lines instances another Person Struct with no init values (should use (), if not we would access the Person definition). We should realize that ‘you ‘ attributes values are all undefined. While sometimes we want undefined values in order to check no existence of something, we are going to see how we could initialize our Person Struct with some by-default values.

struct Person (
  public
  name, 
  age,  
  gender, 
  fn OnInit = (
    if (name==undefined) then name = ""
    if (age==undefined) then age = 18
    if (gender==undefined) then gender=#male
  ),
  on create do (
    OnInit()
    --format "Person Created %n" this
  ),
  on clone do (
    --format "Struct Person Cloned %n" this
  )
)

From 3DS Max 2010, two event handlers are available. These are on create and on clone. on create is going to be executed when instancing our Struct, so we will use it in order to initialize our Struct in case we have not provide any value while instancing.  on clone is gonna be executed when using copy statement.

At the moment it’s time to add another functionality to our Person Struct. Let’s add a OnBirthday function that add 1 to Person Struct age and return modified age.

  ), -- END on clone
  fn OnBirthday = (
    this.age += 1
    format "% is % years oldn" this.name this.age
    return this.age
  )
) -- END Struct Person
you = Person name:
you.OnBirthday()

We have just defined a new function within our Person Struct ( don’t forget to add a , after on clone ‘)’ ). Currently we could call OnBirthday method on any of our Person Structs. Noticed that i have use this statement inside OnBirthday method. It’s used to access instance variables. Of course any of Struct methods could contain parameters as we usually do in MaxScript function definition

Currently is time for you to practice.


In next articles we would speak about developing some Singletons inside MaxScripts and also about Event – Delegate system between Structs ^^

Leave a Reply

Your email address will not be published. Required fields are marked *