[Closed] Maxscript issues, please help
Hi folks!
I’m coding my first maxscript, but I keep getting random issues.
Basically I am an architectural visualiser and I am coding a script that creates windows out of selected polygons.
To make this script work, you must create an object, put and editable poly modifier onto it and select at least one of the faces, then run the script set your values, pick your textures and click the Create Window button.
If all went well you should see a modelled window in your viewport.
Now to my problems:
[ol]
[li]Some times the script just fails to create a window, seems to happen randomly[/li][li]Some times I get an error along the lines of, “An unexpected exception occurred”[/li][li]Worst of all, after using this script on my max document, I save it, close max and when I try to reopen it the scene flickers for a second and max crashes with “An error has occurred and the application will now close” error[/li][/ol]Please take a look at my code below, I would be very grateful if anyone could help fix these issues and even offer advice to optimise this script, as I said it’s my first script and it’s probably not the best way to code a tool like this.
Thank you to all!
Dav
fn checkSelection =
(
try
(
theObject = modPanel.GetCurrentObject()
if (theObject != undefined and subobjectlevel > 3) then
(
if (classof theObject as string == "Edit_Poly") then
(
test = theObject.getSelection #Face
test.isEmpty -- returns true if no polys selected
)
else
(
true
)
)
else
(
true
)
)
catch
(
messagebox "Could not check selection" title:"Error"
return()
)
)
fn doInset &iObject iAmount =
(
try
(
iObject.modifiers[1].insetamount = iAmount
iObject.modifiers[1].insettype = 1
iObject.modifiers[1].ButtonOp #inset
)
catch
(
messagebox "Could not inset selection" title:"Error"
return()
)
)
fn doBevel &bObject bHeight bOutline =
(
try
(
bObject.modifiers[1].bevelHeight = bHeight
bObject.modifiers[1].bevelOutline = bOutline
bObject.modifiers[1].bevelType = 1
bObject.modifiers[1].ButtonOp #bevel
)
catch
(
messagebox "Could not bevel selection" title:"Error"
return()
)
)
fn doExtrude &eObject eHeight =
(
try
(
eObject.modifiers[1].extrudeFaceHeight = eHeight
eObject.modifiers[1].extrudeFaceType = 1
eObject.modifiers[1].ButtonOp #ExtrudeFace
)
catch
(
messagebox "Could not extrude selection" title:"Error"
return()
)
)
rollout createWindow "Create Window" width:165 height:350
(
group "Frame"
(
materialbutton frameMat "Pick Material"
spinner frameInsetSpn "Inset:" type:#worldunits fieldwidth:60
spinner frameBevelHeightSpn "Bevel Height:" range:[-100,100,0] type:#worldunits fieldwidth:60
spinner frameBevelOutlineSpn "Bevel Outline:" range:[-100,100,0] type:#worldunits fieldwidth:60
spinner frameExtrudeHeightSpn "Extrude:" range:[-1000,1000,0] type:#worldunits fieldwidth:60
)
group "Beading"
(
materialbutton beadingMat "Pick Material"
spinner beadingInsetSpn "Inset:" type:#worldunits fieldwidth:60
spinner beadingExtrudeHeightSpn "Extrude:" range:[-1000,1000,0] type:#worldunits fieldwidth:60
)
group "Glass"
(
materialbutton glassMat "Pick Material"
spinner glassShellInnerSpn "Inner Amount:" type:#worldunits fieldwidth:60
spinner glassShellOuterSpn "Outer Amount:" type:#worldunits fieldwidth:60
)
button createBtn "Create Window"
-- Generates an unknown exception?
on createWindow open do
(
global frameObjName = uniquename "Window_Frame"
global beadingObjName = uniquename "Window_Beading"
global glassObjName = uniquename "Window_Glass"
global frameMaterial
global beadingMaterial
global glassMaterial
frameInsetSpn.value = 50
frameBevelHeightSpn.value = -3
frameBevelOutlineSpn.value = -3
frameExtrudeHeightSpn.value = -50
beadingInsetSpn.value = 10
beadingExtrudeHeightSpn.value = -10
glassShellInnerSpn.value = 10
glassShellOuterSpn.value = 0
)
on frameMat picked mtl do
(
frameMaterial = mtl
)
on beadingMat picked mtl do
(
beadingMaterial = mtl
)
on glassMat picked mtl do
(
glassMaterial = mtl
)
on createBtn pressed do with undo on
(
if (selection.count < 1 or checkSelection() != false) then
(
messagebox "You must select at least one polygon" title:"Error"
return()
)
-- disableSceneRedraw() -- disable viewport redraw, speed, sometimes crashes
-- suspendEditing() -- disable Modify panel update, speed, always crashes
setWaitCursor() -- show wait cursor
baseObj = (getcurrentselection())[1] -- get current selection
baseObj.modifiers[1].DetachtoObject frameObjName -- detach frame
subobjectlevel = 0
-- -- Frame
frameObj = getnodebyname frameObjName
select frameObj
addmodifier frameObj (edit_poly())
max modify mode
subobjectlevel = 4
doInset frameObj frameInsetSpn.value
doBevel frameObj frameBevelHeightSpn.value frameBevelOutlineSpn.value
doExtrude frameObj frameExtrudeHeightSpn.value
frameObj.modifiers[1].DetachtoObject beadingObjName
frameObj.material = frameMaterial
subobjectlevel = 0
-- -- Beading
beadingObj = getnodebyname beadingObjName
select beadingObj
addmodifier beadingObj (edit_poly())
max modify mode
subobjectlevel = 4
doInset beadingObj beadingInsetSpn.value
doExtrude beadingObj beadingExtrudeHeightSpn.value
beadingObj.modifiers[1].DetachtoObject glassObjName
beadingObj.material = beadingMaterial
subobjectlevel = 0
-- -- Glass
glassObj = getnodebyname glassObjName
select glassObj
addmodifier glassObj (shell())
max modify mode
glassObj.modifiers[1].innerAmount = glassShellInnerSpn.value
glassObj.modifiers[1].outerAmount = glassShellOuterSpn.value
glassObj.material = glassMaterial
-- --
clearselection()
-- enableSceneRedraw()
redrawViews()
-- resumeEditing()
setArrowCursor() -- revert to arrow cursor
)
)
createDialog createWindow
No replies? :shrug:
Just an update, I have noticed that the corrupted file issue (problem 3 from above) seems to be linked to detaching a polygon face as a new editable poly and then working on the new detached object.
If I don’t detach the face then the file opens with max fine, trouble is I need to detach the frame from the object, the beading from the frame and the glass from the beading
Is there any way I can overcome this problem?
Thanks!
Dav
dont have max in front of me but it should be possible to detach the selected faces to a new element within the existing mesh saves having to make a bunch of new objects and it means that you should only have to call meshop for one object
Thanks for the reply!
I’m not sure I would understand how to do that, I think I’d still prefer to split the window sections into separate objects, it just a way of modelling I have become accustomed to.
Cheers!
After messing with my code over and over, it seems detachToObject is completely broken in at least max 2009 (I don’t have another version to test in).
Can anybody point me in the direction of a script that generates a window from a single selected polygon?
Thanks guys!