[Closed] Need help: SlateME script gives garbage collection errors
A lot of people have griped that Slate doesn’t have a simple menu or button command to load up the material from the selected object. I thought I’d take a stab at creating a useful script to do this. What I’m doing is having maxscript create a new view in slate showing the material node of the currently selected object. When you switch objects, the material in the view updates to the new object’s material automatically. The code below accomplishes this, however after clicking on a few objects, 4 or 5, a “Maxscript Garbage Collection Error” pops up in a messagebox. “An unknown error occurred while maxscript was performing garbage collection. If you get this error multiple times, recommend restarting Max.” I can’t seem to tweak the code to get rid of this. Anyone have any ideas?
fn updateSlateSelected =
(
if sme.isOpen() == true then
(
if $ != undefined and (selection as array).count == 1 then --continue if 1 and only 1 object is selected
(
if sme.getViewByName "Selected" != 0 then --if the "Selected" view already exists
(
vIndex = sme.getViewByName "Selected" --get the view index for "selected"
sme.deleteView vIndex false --delete the view
sme.createView "Selected" --create a new clean view
)
else --if it doesn't already exist
(
sme.createview "Selected"
vIndex = sme.getViewByName "Selected"
)
vNode = sme.getview vIndex
curSelMat = $.material
if curSelMat != undefined then vNode.createNode curSelMat [0,0]
)
)
)
callbacks.addScript #selectionSetChanged "updateSlateSelected()"
global mse_oneOnly = off -- one or multi selection toggle
fn mseShowSelected oneOnly:mse_oneOnly = if sme.isOpen() do
(
if (id = sme.getViewByName "Selected") != 0 do sme.deleteView id off
if (not oneOnly and selection.count > 0) or (selection.count == 1) do
(
view = sme.getview (sme.createview "Selected")
for node in selection where (mat = node.mat) != undefined do view.createNode mat [0,0]
-- Update Layout:
hwnd = windows.getChildHWND 0 "Slate Material Editor"
WM_COMMAND = 0x111
ID = 0xA02C -- Layout All Button source ID
windows.sendMessage hwnd[1] WM_COMMAND ID 0 -- press button
)
)
callbacks.removeScripts id:#mse_show_selected
callbacks.addScript #selectionSetChanged "mseShowSelected()" id:#mse_show_selected
your code has to work as well but it needs a little cleanup and the using of unique callback ID
to prevent multiple callback registration (probably it was an issue).
Hey Denis,
As usual, thanks for taking some time to help out with this! I like how you’ve shortened up the code, and also added the ability to show more than one material at a time. I had considered attempting this, but backed off in the event people might select a huge number of objects at once. I wasn’t sure it’d be a good idea to have slate load up 100+ materials. But I never tested it either, so maybe it’s not a big deal.
Anyway, in testing out your code I still get that Garbage Collection error. Were you able to switch your selection around several times without any errors? My max2011 installation is still brand new, and I’m curious if there might be something wrong with it (if you don’t get the errors).
Oh… also… can you point me to where I can find a list of those message codes? Is that activeX or dotnet?
Thanks!!!
One more question- I don’t quite understand the usage of the “oneOnly” argument. Can you explain how this is working? I had used (selection as array).count because if only one object is selected, the selection does not have a “.count” property. So maybe it has something to do with this?
Thanks…
I don’t quite understand the usage of the “oneOnly” argument. Can you explain how this is working?
if you set mse_oneOnly global in my sample to ON and I will show only ONE selected node at the time. By default mse_oneOnly is set to OFF.
if only one object is selected, the selection does not have a “.count” property.
selection has a .count property at any case including if nothing selected. $ has a count property only in case of multiple selection.
Anyway, in testing out your code I still get that Garbage Collection error. Were you able to switch your selection around several times without any errors? My max2011 installation is still brand new, and I’m curious if there might be something wrong with it (if you don’t get the errors).
I don’t have this problem with garbage collection. Try to increase Initial heap memory allocation in Preference Settings (I recommend 128 MBytes).
[i]Oh… also… can you point me to where I can find a list of those message codes? Is that activeX or dotnet?
[/i]it’s more like c++. It’s system (Windows) messages. here is the list:
http://www.autohotkey.com/docs/misc/SendMessageList.htm
Thanks for the explanations Denis. I like the optional on/off idea. I tried a big selection and with 2000+ objects and probably 100+ materials, it definitely took some time to load. What I think I’ll do is figure a way to let the user specify a max number of materials to load, since I’d think it’d be quite rare to work on more than 5-10 at any given time.
Also, it looks like the heap size is what was causing my Garbage Collection errors. It’s set to 15 MB by default. Since increasing it I’m not seeing the error anymore.
Thanks again!
SME doesn’t load the same material multiple times but it checks it for uniqueness. It takes the time. So you can collect all materials from selection first, make the list unique, and create sme nodes after. But for me only 2-3 materials at the same view make sense (it’s comparison or parameters copy/x-change reason). So the user’s option of the number of sme nodes in view should be 1 or 2(3, whatever you define.). I think that 2 materials in the vertical layout should be default.
PS. But personally I hate SME in MAX same as I hate Hypershade in Maya.