[Closed] Object Oriented programming in maxScript
I was surprised to figure out that several object oriented programming techniques are possible in maxScript.
For example.
- inheritance is possible between different classes/structs.
- polymorphism is supported as well.
Perhaps I’m the only one that missed this, but they are sure powerful techniques, and the documentation doesn’t mentionem them really.
If someone else has missed this I can post some examples showing what I’m talking about.
It would be nice if the maxScript help had more about object oriented programming IN max, since it doesn’t work like one is used to.
/Andreas
please post an example.
** edit:
is there something like static classes in maxScript without the use of the SDK?
I once tried to implement a Singelton but it failed …
** /edit
Georg
Nope. He, I somewhat want to release Polyspeed SOOON. After showing PolySpeed to Autodesk in Leipzig I got some extra preasure from them to finish it soon for their max8 roadshow so no mega rewrite no :P.
Ok, I will post some short examples, I can post bigger ones later. These are from my memory.
Inheritance.
struct baseClass
(
fn function1=
(
print “function1”
)
)
struct mediumClass
(
base=baseClass(), –this an instance of the base object
function1=base.function1, –this is the actual inheritance, so we take over this function from baseclass.
fn function2=fn
(
print “function”
)
)
In this example we have 2 objects. The mediumclass inherites the function1 from the baseclass. Even better is if you need to expand the functionality of the function1 so it adds some things in the new class. Like this:
struct mediumClass
(
base=baseClass(), –this an instance of the base object
oldfunction1=base.function1, –Notice the name is now different
fn function1=
(
oldfunction1()–call the old function
print “function1 new things too” –here we add the new things for the new class
)
fn function2=fn
(
print “function”
)
)
Usage:
small=baseClass()
medium=mediumClass()
we can now call both classes with the function1
small.function1()
medium.function1()
PolyMorphism:
Just change the function that a function is pointing at:
We have this,
oldfunction1=base.function1
but if we had another function say functionA, we could change oldfunction1 later
so instead of oldfunction1=base.function1 it would say
oldfunction1=base.functionB
this is good if we would use completely different functions depending on the object type for example. So we have special functions for poly objects and special functions for mesh objects.
I will try to add better examples later, but these techniques are extremely useful. I’m usnig it for my new and quite powerful interface.
/Andreas
f97ao,
maybe you can tell me:
what would be the MAXscript equivalent for a constructor in a struct?
Or is the struct its “own” constructor ?
struct TheCounter (
numberStore=0,
fn rise = (
numberStore+=1
return numberStore
),
fn fall = (
numberStore-=1
return numberStore
)
)
test=TheCounter()
test.rise()
test.rise()
test2=TheCounter()
test2.rise()
what would be the MAXscript equivalent for a constructor in a struct?
Or is the struct its “own” constructor ?
Constructor exist in maxScript. just call the function in the struct.
struct objectClass
(
fn Init
(
–do constructor things
),
init=Init() –here call the constructor
)
Also another thing that took me a while to figure out. If you need to call the other member functions in a struct from each other you should do this
struct objectStruct
(
–Function Definitions
Function1=fn Function1(), –define functions
Function2=fn Function2(),
–locals
–Functions
fn Functions1=
(
Function2()–this would not work if they were not defined as empty above
),
fn Function2=
(
–function2 stuff.
)
)
Been sittning the last days trying to figure this stuff out. There is little help in the maxScript manuals. Perhaps I should write a short tutorial explaining how do to most traditional object oriented programming. I haven’t used structs that much since I didn’t know they supported inheritance etc. But now I plan to use them almost exclusively.
Another good use for Structs in max is that it’s easier to error check variables. since if you write the struct name it prints all variables and their values.
Also I strongly doubt that a Destructor exist in maxScript, but that isn’t a super big problem imho.
If there is interest I may write a tutorial later.
thanks alot!
that cleared some fog.
Maybe now I can implement the Singelton
Georg
I used the stuff learned today to design this “class”:
-- -
-- ObjectVisibilityManager (simple)
-- -
struct ObjVisManager (
-- -
-- private properties
-- -
objVisStore,
-- -
-- private functions
-- -
fn storeVis = (
-- -
-- strores the visibility of all objects
-- and references to the object in a array
--
for i in objects do (
currentObjData=#()
if (isGroupHead i)==false then(
append currentObjData i
append currentObjData i.visibility
append objVisStore currentObjData
)
)
return true
),
fn theReset = (
-- -
-- reset the array
-- -
objVisStore=#()
return true
),
fn init= (
-- -
-- constructor function
-- -
global ObjVisManagerInstanced
if ObjVisManagerInstanced == undefined then (
theReset()
storeVis()
ObjVisManagerInstanced=true
return true
)else(
print "is instanced ..."
return false
)
),
-- public functions
fn toggleAll = (
-- -
-- toggles the visibility of all objects
-- -
for i in objVisStore do (
i[1].visibility=not i[1].visibility
)
),
fn restoreAll = (
-- -
-- restores the visibility of all objects
-- -
for i in objVisStore do (
i[1].visibility=i[2] )
),
fn setAllVis flag = (
-- -
--set the visibility of all objects to flag
-- -
for i in objVisStore do (
i[1].visibility=flag
)
),
-- call the constructor
start = init()
)
-- singleton
if ObjVisManagerInstanced == undefined then (
theMngr=ObjVisManager()
)
theMngr.toggleAll()
-- theMngr.restoreAll()
-- theMngr.setAllVis false
does
“– Runtime error: Struct member access requires instance: …”
mean that there are no static classes in MAXscript ?
regards,
Georg
took me some time, but now I think I got a working Singleton for maxScript.
used references:
GOF: Design Patterns
Larry Minton: a set of timer functions [MaxScript library (moderated)]
(
-- -
local theInstance
-- -
fn SingletonRDG = (
struct SingletonGenerator (
myDummy,
fn init = (
myDummy=dummy()
),
fn setPos newPos:[0,0,0] = (
myDummy.pos=newPos
myDummy.pos
),
fn getPos = (
myDummy.pos
),
start=init()
)
if theInstance == undefined then (
format "Instance is undefined: init SingletonRDG()
"
theInstance=SingletonGenerator()
)else(
format "Singleton allready exists: returning %
" theInstance
theInstance
)
)
--
format "rudi: %
%
" (SingletonRDG()) (rudi=(SingletonRDG()).setPos newPos:[random 0 100, 0, 0])
format "dagmar: %
%
" (SingletonRDG()) (dagmar=(SingletonRDG()).setPos newPos:[random 0 100, 0, 0])
-- format "-> rudi
%
-> dagmar
%
" rudi dagmar
)
Georg