PDA

View Full Version : Integrating maxSDK or C++ applications in maxscript?


scorpion007
03-03-2006, 11:50 PM
Hi,
Lets say i have a scripted application running in max all nicely. Certain parts of this application are quite intensive, involving running long loops and reading/manipulating lots of data.
After optimizing maxscript and making it run as fast as possible, I'm considering rewriting certain portions in c++ perhaps using the maxSDK to speed them up - rather than rewriting the whole application is c++.
I'm just wondering how i could say re-write 1 key function in c++ and then call that function in maxscript, for an equivalent result, just perhaps faster execution, since it will be machine code, not interpreted.
Do i make a dll or something? I'm quite new in this area. Any pointers are welcome.

f97ao
03-04-2006, 01:24 AM
Hi,
Lets say i have a scripted application running in max all nicely. Certain parts of this application are quite intensive, involving running long loops and reading/manipulating lots of data.
After optimizing maxscript and making it run as fast as possible, I'm considering rewriting certain portions in c++ perhaps using the maxSDK to speed them up - rather than rewriting the whole application is c++.
I'm just wondering how i could say re-write 1 key function in c++ and then call that function in maxscript, for an equivalent result, just perhaps faster execution, since it will be machine code, not interpreted.
Do i make a dll or something? I'm quite new in this area. Any pointers are welcome.

What you need to do is a maxScript Extension.
It works very good to write the time critical functions in c++ and then you call them from MaxScript. So in maxScript you write for example:
"CreateBigObjects 1000" --this will call your own c++ function that generates lots of objects.

You have to install the 3dsMax SDK (it's normally not installed, you have to select it at the installation). Make sure to look in the manual for MaxScript extensions. There are several examples. They are hard to understand, but I figured them out in a few days so it's possible. Another tip is to check in the source code that will be available when you install the max SDK. Most plugins for max are extremely big, but especially the smaller ones are possible to understand.

You can also join the Sparks forum on Autodesk homepage, they have discussions about c++ and the SDK.

Hope this points you in the right direction.

/Andreas

scorpion007
03-04-2006, 02:18 AM
Thanks, I do have the SDK installed, though when reading through the reference I didn't find anything appropriate for me. Now I will look into these MaxScript extensions, seems like what I'm looking for. Thanks.

scorpion007
03-04-2006, 03:54 AM
BTW, does Visual C++ 2005 Express version work with the SDK? I'm thinking the compiler is different and doesnt like some code that's part of the standard includes in maxSDK. Or I could be doing something wrong..

For example, when compiling a sample, gscript, I get the following error:
include\polyobj.h(109) : error C2065: 'i' : undeclared identifier
And I checked the source of that file, and it's probably incorrectly using the scope of the 'i' variable. Maybe its fine with Visual C++ 7.0, as stated in the software requirements section.

AlpineMan
03-06-2006, 06:12 PM
The help file that shipped with the 3ds Max 8 SDK is very good. It really helps new beginners who are newer to Maxscript and newer to C++ coding in the SDK. It is light years ahead of the old help files.

Chris J.

arketip
03-09-2006, 06:34 PM
Which saving of time can I hope by using routines C ++ ?

Here is a routine which calculates rotations 3D with maxscript:
fn slowTask theVertex theRotMatrix = (
for i=1 to 12345678 do (
theVertex*=theRotMatrix
)
)

(
local theRotMatrix=(quat 2.0 [0,0,1])as matrix3
local theVertex=[11.1,22.2,33.3]
local startTime=timestamp()

slowTask theVertex theRotMatrix

local endTime=timestamp()
format "Processing took % ms\n" (endTime-startTime)
)

Can anybody test the speed difference between maxscript and C ++ please ?

EDIT: I don't have an compiler installed on my pc. I wish at first to have an idea.

f97ao
03-14-2006, 11:08 AM
[QUOTE=arketip]Which saving of time can I hope by using routines C ++ ?

Can anybody test the speed difference between maxscript and C ++ please ?
[/QUOTE=arketip]

This really depends on what you are doing. Some things are not faster in c++ at all, while some things are enormously much faster.

In general the Autodesk people said that the code is 10-100x faster in C++, but it really depends on what you are doing.
What you want to be careful is if you have to do MANY things in MaxScript. This will always be far slower. Big loops checking for example the position about a vertex will not be fast.

Another thing you need to be careful with is modifying geometry in complex ways. If you mix modifying geometry with big loops you have a BIG problem if it needs to be fast.

Example

A loop that moves a vertex in an Editable_Poly 10000 times. This will in fact be almost 10000 slower in MaxScript than it would be in c++.

However if you move the 10000 vertex points with ONE command in MaxScript then there will be no speed difference. Basically you want to use as few commands as possible in MaxScript, then it can actually be very fast.

/Andreas

joconnell
03-14-2006, 11:37 AM
Just curious as to how difficult you think it is to use the sdk if you've only got a scripting background? I'm still only a beginner in maxscript compared to you guys but a few of the bits I've written in maxscript are really intensive (find and replace on geometry objects for example) so c++ may sort that out. A few people have said that c++ is very like maxscript - is this really the case that it's similar in terms of its structure and language? I find maxscript really easy to work with (aside from trying to figure out how certain badly document commands may work) so is c++ a big jump again?

Cheers,

John

f97ao
03-14-2006, 01:59 PM
Just curious as to how difficult you think it is to use the sdk if you've only got a scripting background? I'm still only a beginner in maxscript compared to you guys but a few of the bits I've written in maxscript are really intensive (find and replace on geometry objects for example) so c++ may sort that out. A few people have said that c++ is very like maxscript - is this really the case that it's similar in terms of its structure and language? I find maxscript really easy to work with (aside from trying to figure out how certain badly document commands may work) so is c++ a big jump again?

Cheers,

John

It depends on what you want to do and also if you have any previous experience with other programming languages.

But if you think that maxScript is "real easy" then it should be possible.

First I would recommend you to quickly readup on the basics on c++. Check here for example
www.cplusplus.com (http://www.cplusplus.com)

If you read most of it you are in a far better position to begin working with the sdk.

If you want to work with interfaces you want to check out these (they show you how to do windows, buttons, getting keyboards presses etc)
http://www.winprog.net/

In the future I hope to write a few tutorials on writing plugins for max, right now I really need to dig deeper into them myself.

So in short. C++ is a big jump for most things, but not impossible.

/Andreas

joconnell
03-14-2006, 03:28 PM
To be honest I've only ever used scripting languages like actionscript (pre 2.0), php, lingo and maxscript so maxscript would be the nearest to a proper language I'd have used - I'll take a look at those links though. Also a few people were asking in a cg academy thread about doing a bobo like dvd on the max sdk - might be worth a look?

f97ao
03-14-2006, 03:35 PM
To be honest I've only ever used scripting languages like actionscript (pre 2.0), php, lingo and maxscript so maxscript would be the nearest to a proper language I'd have used - I'll take a look at those links though. Also a few people were asking in a cg academy thread about doing a bobo like dvd on the max sdk - might be worth a look?

Well, give it a shot if you really need the extra power. A dvd for the max SDK would be great. Even if you know c++ quite well it's not so easy to use the max SDK. I expect to become drastically better in this area pretty soon, since I begin a new job as a MaxScript/Max SDK/C++ developer :)

/Andreas

stuh505
03-14-2006, 07:30 PM
I just started wading through the sdk getting started help files last night...having a dvd for max sdk would be pretty cool. I havent finished reading the manual but there doesnt seem to be much explaining how to get started by writing a simple plugin. All the plugins examples I expect to be simple like "sphere" or whatnot are thousands of lines of code...it would be nice to see some hello world type examples!

arketip
03-14-2006, 09:18 PM
Hi thanks for the replies

andreas : lets say i have to calculate many 3D operations (vector, matrices) but in memory only.


And nobody can translate my little script in cpp ?

stuh505
03-15-2006, 12:49 AM
Hi thanks for the replies

andreas : lets say i have to calculate many 3D operations (vector, matrices) but in memory only.


And nobody can translate my little script in cpp ?

Well it's not just a matter of translating it into cpp, it's a matter of translating it into the specific classes of the SDK.

I'm just starting to look at the SDK so I can't write and example using the SDK yet...but I did write a barebones program in C++ to do the equivalent operations as your maxscript program.

You're not going to believe this....but the C++ time was 37 seconds compared to 23 seconds to run the maxscript...


#include <stdio.h>
#include <time.h>

void main()
{
float m[] = {0.999391,-0.0348995,0, 0.0348995,0.999391,0, 0,0,1, 0,0,0};
float v[] = {11.1,22.2,33.3};
time_t startTime = time (NULL);
time_t endTime;
for (int i=0; i < 12345678; i++)
{
v[0] = m[0]*v[0] + m[1]*v[1] + m[2]*v[2];
v[1] = m[3]*v[0] + m[4]*v[1] + m[5]*v[2];
v[2] = m[6]*v[0] + m[7]*v[1] + m[8]*v[2];
}
endTime = time (NULL);
printf ("seconds: %ld", (endTime-startTime));
return;
}

arketip
03-15-2006, 09:00 AM
Wow :)
Maxscript seems favored in the matrix calculation.
Let us see on an equal base (with arrays) what it happens.


fn slowTask theVertex theRotMatrix = (
for i=1 to 12345678 do (
theVertex[1] = theRotMatrix[1][1]*theVertex[1] + theRotMatrix[1][2]*theVertex[2] + theRotMatrix[1][3]*theVertex[3]
theVertex[2] = theRotMatrix[2][1]*theVertex[1] + theRotMatrix[2][2]*theVertex[2] + theRotMatrix[2][3]*theVertex[3]
theVertex[3] = theRotMatrix[3][1]*theVertex[1] + theRotMatrix[3][2]*theVertex[2] + theRotMatrix[3][3]*theVertex[3]
)
)

It is difficult in that case to write a totally symmetric routine.

f97ao
03-15-2006, 10:20 AM
Wow :)
Maxscript seems favored in the matrix calculation.
Let us see on an equal base (with arrays) what it happens.
[/CODE]


You have to remember that max SDK has it's own functions for handling matrices, that probably are very optimized and fast. MaxScript uses the same functions.

I'm afraid that I'm not good enough yet with the max sdk to help you translate it. I have only done a few programs in the sdk and they didn't work with geometry at all.

In a few months I will be far more prepared to handle these tasks.

However I can explain a few things about efficiency in maxScript and SDK, that is worth thinking about. I've done a bit of heavy programs in Matlab which is similar to maxscript with regards to efficiency.

As long as put all the time intense areas into very FEW calls, scripts will be very fast. Probably as fast as if it was written in Max SDK.

The reason is that the calls take time. For example lets say one call in a script takes 0.0001 seconds to do. In c++ this takes practically 0 time (perhaps something like 0.000000001).

Lets also say we are detaching a face on a big object which takes 1 seconds to perform.

In maxScript this would take 1.00001 seconds to run and in the sdk it would take 1 second. As you see there is practically no difference at all. What takes time is to do the Detach, and this is programed in C++.

However If we would do a very big loop gathering data and then do the detach. Say a loop that repeats 100000 times, then it's very different.

for i=1 to 100000 do --this will take 10sec in maxScript and 0 time in c++
(
--get some data
)
Detach() --this takes 1sec.

In MaxScript this takes 11seconds and in c++ it takes only 1.

The way to avoid the slowness in maxScript here is if the big nasty loop can be done with fewer commands. Perhaps the data can be put in a matrix instead and called with one command. Or of course the big nasty loop can be done in c++ instead and exported into MaxScript like this:

GetAllData() --a c++ function that has been exported into MaxScript. Takes 0.000.. time to perform
Detach()--takes 1 sec.

Now we are using maxScript and c++ together and it's 11 times faster than it would be in maxScript.

The main important thing to realize here is that for many cases the Max SDK doesn't have to be any faster. It's also important to note that many things don't even have to be that fast. If it takes 0.0001 or 0.00000000000001sec to update an object normally doesn't matter.

Hope that gave some pointers.
/Andreas

arketip
03-15-2006, 10:56 AM
Thank you, Andreas

My conclusion is that it is not necessary to use cpp for the whole program.
I prefer to continue to use maxscript for the interface for example and all the fast calculations. But that is advantageous to use cpp just in certain function calls, where maxscript is slow.

I have to have several parameters between cpp and mascript (input and output parameters). To send and receive arrays for example.

That is probably not very complex.

galagast
03-15-2006, 01:18 PM
Hi, I havent looked at the new sdk helpfile yet, but I was hoping to see an example for probably a very simple function like:
fn add a b = a+b
and convert that to cpp...
I guess from that example, we might be able to get the basic idea and components in creating functions using the sdk...
(sorry if I'm not much of a help arketip...)

stuh505
03-15-2006, 02:41 PM
I have to have several parameters between cpp and mascript (input and output parameters). To send and receive arrays for example.

Sending and recieving arrays is very costly, so you would not want to put something like this in a loop. If you do all your arrays in c++ or all in maxscript, either way you can usually pass by reference which avoids making a copy.

f97 is right, the SDK will also have optimized functions. The main reason C++ will be faster is because it is designed for being fast and it provides compiled code, whereas maxscript is interpreted. This means that each maxscript command must first be translated into machine code while it's running...but the program written in C++ is already provided in machine code.

f97ao
03-15-2006, 02:59 PM
Hi, I havent looked at the new sdk helpfile yet, but I was hoping to see an example for probably a very simple function like:
fn add a b = a+b
and convert that to cpp...
I guess from that example, we might be able to get the basic idea and components in creating functions using the sdk...
(sorry if I'm not much of a help arketip...)

I may be able to put together an example like that in a while.

But just so you know, what you just wrote will probably need 5-10 pages of code just to start. :)

/Andreas

arketip
03-15-2006, 03:02 PM
galagast: I completely agree with you. The ideal would be to have a function like fn add a b written for cpp.

stuh: in fact to send and receive an array by reference (or other values) will be even more convenient for me... and about the structure of arrays ? It is the same in cpp and maxscript ?

EDIT:andreas: 5 to 10 pages of headers not really useful for us, isn't ?
Is not there any wizzard which generates the code for that automatically?

stuh505
03-15-2006, 05:08 PM
galagast: I completely agree with you. The ideal would be to have a function like fn add a b written for cpp.

The maxscript addition function is written in c++, so you already have that.

hblan
04-30-2006, 02:36 PM
if i want learn C++ , for sdk of max or other 3d software like softimage sdk . is here anybody show me a guide in learning process ? i think it is not necessary to learn whole c++ .

thanks .

AlpineMan
05-31-2006, 12:04 PM
Yes you will have to learn all of C++, and probably more. And you should learn as much as possible of C, and probably more. And you should learn the Windows Win32 API, and GDI, and some MFC wouldn't hurt either.

Chris J.

soshiant
06-04-2006, 04:09 PM
to learn C++:
The C++ Programming Language by Bjarne Strostrup.(the creator of c++)
Bruce Eckel's Thinking in C++ :thumbsup: http://www.mindview.net

for API programming:
Programming Windows 98 by petzold! MSPress

To learn ASM:
The Art of Assembly Language(AoA) by Randall Hyde!

As far as i remember if you read the SDK help you will find these clues...

or if you have TIME check out AMAZON.com for BOOOOKSSSS....

But this is just a start...C++ and programming is far from scripting.

f97ao
06-04-2006, 09:45 PM
if i want learn C++ , for sdk of max or other 3d software like softimage sdk . is here anybody show me a guide in learning process ? i think it is not necessary to learn whole c++ .

thanks .

Seriously, stay away from the max SDK if you are unfamilair with c++.
I would first learn maxScripting, then you quickly learn how to build useful programs.
To program in the sdk will need very serious dedication, there are many things you will have to learn such as windows programming, different libraries and just the main 3ds Max core code (which is extremely big). C++ in itself is the easy part I'm afraid.

/Andreas

CGTalk Moderation
06-04-2006, 09:45 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.