[Closed] Finding out if an object is an instance
Hello everyone,
I’m scripting a level exporter (just maxscript and a .X exporter). Now i’ve come to the part where optimizing is getting important. I should probably first explain what my exporter is doing: It’s looping through all objects in $geometry, outputting the materials, textures and shaders and writing their value’s to a file. Now i’ve come to the part where the model is getting exported. That’s not hard, but i don’t want to have 100 table.x files in my output folder if i have 100 tables. I thought it might be good to actually make all the tables instances of eachother when it’s necessary, so i’ll only have to export the transformation matrix to the file, since i’m able to duplicate the table inside the 3d engine i’m using.
Now we’ve come to my problem. I want to be able to find out if an object is an instance of some other object, and i want to be able to find out which object that might be.
Can someone point me in the right direction please? Thanks in advantage. Oh btw, i found InstanceMGR, but i’m not sure if that one will help me.
Greetings
Yeah, InstanceMgr is the ticket. Assuming you have obj, the object you want to find instances of:
instObjs = #()
InstanceMgr.GetInstances obj &instObjs
That leaves you with a populated array instObjs, which you can loop through and write transforms out for in your file. Here’s a more complete example that outlines what I think you’re after:
doneObjs = #()
for obj in geometry where (findItem doneObjs obj == 0) do (
-- write out first instance of an object
<mesh export code here>
-- Now loop through all instances of that object,
-- writing each transform to file. This includes the
-- base/first instance we exported above.
instObjs = #()
InstanceMgr.GetInstances obj &instObjs
for thisInst in instObjs do (
<write transforms out here>
append doneObjs thisInst -- mark this object done
)
)
I didn’t test the above, but that’s the pattern I’ve often used for that sort of thing.
Thanks, it works with just 1 correction, the getInstances also returns the object itself, so i removed that
EDIT: just noticed you wrote that already;) I adjusted it a bit, since i want to write the main object’s transform matrix at the same time i’m exporting the object as a .x file, but that’s just because of the engine i’m using, makes it slightly easier to import. So your solution works perfectly, thanks again