Forum Groups
  All forums
    Help & Feedback
      Questions
      Work in progress
      Finished Art
      Non-Max related
    Community
      Offtopic
      News
    Hardware
    Photography



Maxunderground news unavailable

Script Fails when in Button
show user profile  ccampbell
Ok... here we go again..........(i've had this problem in the past but cant remember how i fixed it)

I created a script to automate a bunch of manual processes for generating splines from the borders of some planer geometry.
(SCRIPT AND SCENE OBJECT HERE www.cicanimation.com/temp_download/ScriptButtonProblem/Script_Button_Problem.rar)

when i evaluate by selecting the content and hitting enter it works just as expected.

When i execute using a button(exact same script within a button) the script fails and appears to skip lines of the code... i have posted the code below for anybody who has the time to look. I feel as though i have exhausteds my options.

I have tried
Filein ""
Maxscript UI > Run script
adding script block as function and calling func


Out of ideas... been working on this one for 3 days...... :/


newfloat = newrolloutfloater "splinegen" 200 75
knotArray=#()
rollout splineGen "Border Generator"
(
button btn1 "Generate Border Splines" width:130 height:30 align:#center

on btn1 pressed do
(
--CREATE BORDERS FROM UNWRAPPED GEOMETRY
copySel= copy $
converttopoly $
$.EditablePoly.SetSelection #Edge ((polyOp.getOpenEdges $) as bitarray)
polyop.createshape $ #selection name:"Borders"
delete $

--SEPPERATE AND RENAME BORDER SPLINES
select $Borders
copySel = Copy $
select copySel
$.name = "RH_spline"
max modify mode
subobjectlevel = 3
setsplineselection $ #(1)
splineops.delete $
select $borders
$.name="OB_Spline"
max modify mode
subobjectlevel = 3
setsplineselection $ #(2)
splineops.delete $

--OFFSET SPLINES AND DELETE OLD
--outer spline
select $OB_Spline
applyoffset $ 5.0
select $OB_Spline
max modify mode
subobjectlevel = 3
setsplineselection $ #(1)
splineops.delete $
--inner spline
applyoffset $RH_Spline 5.0
select $RH_spline
max modify mode
subobjectlevel = 3
setsplineselection $ #(1)
splineops.delete $

--SPLINE PARAMETERS
select #($OB_spline,$RH_Spline)
$.optimize = false
$.steps = 3
$.render_displayrendermesh =true
$.thickness = 5

--SET SPLINE CURVE TYPE
-- convert segments from line to curve.
Select $OB_Spline
numsegments $ 1
for S = 1 to (numsegments $ 1) do
(
setsegmenttype $ 1 S #curve
)
Select $RH_Spline
numsegments $ 1
for S = 1 to (numsegments $ 1) do
(
setsegmenttype $ 1 S #curve
)
/*
Select $RH_Spline
for i = 1 to (numsegments $ 1) do
(
setsegmenttype $ 1 i #curve
)
*/
--CONVERT POINTS TO SMOOTH
select $OB_Spline
for i = 1 to numknots $ do
(
setknottype $ 1 i #smooth
setknotselection $ 1 #(i) keep:true
)
--OB SPLINE OPTIMIZATION
max modify mode
subobjectlevel=1
for k = 1 to 2 do
(
numknot = numknots $
numloops = (numknot/2.0) as integer
knotArray=#()
for i = 1 to numloops do
(
x=(i+i)-1
append knotArray x
)--end nested i loop
setknotselection $ 1 knotArray
max delete
updateshape $
)--end master k loop
--WELD POINTS IF CLOSE-- possibly add as sepperate button
for i = 1 to numknots $ do
(
setknotselection $ 1 #(i) keep:true
)
weldspline $ 15
updateshape $
--RH SPLINE OPTIMIZATION
select $RH_Spline
max modify mode
subobjectlevel=1
for k = 1 to 1 do
(
numknot = numknots $
numloops = (numknot/2.0) as integer
knotArray=#()
for i = 1 to numloops do
(
x=(i+i)-1
append knotArray x
)--end nested i loop
setknotselection $ 1 knotArray
max delete
updateshape $
)--end master k loop
-------------------
--CREATE INNER BORDER SPLINE
applyoffset $OB_Spline 25.4
Select $Ob_Spline
copySel = Copy $
CopySel.name = "IB_Spline"
Select $IB_Spline
deletespline $ 1
updateshape $
Select $Ob_Spline
deletespline $ 2
numsplines $
updateshape $
--IB SPLINE CLEAN UP
Select $IB_Spline
max modify mode
subobjectlevel=1
for k = 1 to 1 do
(
numknot = numknots $
numloops = (numknot/2.0) as integer
knotArray=#()
for i = 1 to numloops do
(
x=(i+i)-1
append knotArray x
)--end nested i loop
setknotselection $ 1 knotArray
max delete
updateshape $
)--end master k loop
--WELD POINTS IF CLOSE-- possibly add as sepperate button
Select $IB_Spline
for i = 1 to numknots $ do
(
setknotselection $ 1 #(i) keep:true
)
weldspline $ 20
updateshape $
)--end BTN 1
)--end Rollout
addrollout splineGen newfloat rolledup:false






$Entrepreneur = if((Designer + Engineer)*Programmer){Problem Solver};



read 772 times
9/22/2011 8:02:05 PM (last edit: 9/22/2011 8:03:02 PM)
show user profile  Nik Clark
I haven't got time to look at it properly (sorry!), but personally, I'd be using functions instead of a long list of commands. This makes it easier to narrow problems down.

Using try/catch with some debug output can help too.






read 756 times
9/22/2011 8:35:09 PM (last edit: 9/22/2011 8:35:09 PM)
show user profile  Garp
Did you wrap it in a macroscript construct?

macroscript MyUberMacro
category:"Howdy"
tooltip:"Heya"
-- etc...
(
my uber code here
)




read 746 times
9/22/2011 9:58:34 PM (last edit: 9/22/2011 9:58:34 PM)
show user profile  ccampbell
Thanks a lot for replying so quickly guys.

Nik Clark - Thanks alot - i have tried writing functions and calling the functions and i get the exact same error no matter what i do. not sure how to use try/catch yet.. about 6 months into maxscript and still learning everyday.

Garp - I did not have the file in a macroscript construct but i just tried and i get the same error either way... :/

.. still working on it. its not very well written so perhaps i should go back to the drawing board... any other thoughts are highly appreciated. - i have provided the geometry and max sript file above if anybody has some time to waste it would be invaluable to me to figure this out!




$Entrepreneur = if((Designer + Engineer)*Programmer){Problem Solver};



read 741 times
9/22/2011 10:54:55 PM (last edit: 9/22/2011 10:54:55 PM)
show user profile  Davious
reading this thread is like watching a foreign film without subtitles. Fascinating, but I have no idea whats being said, however hopefully at some point the lead actress will end up naked :)

" Difficult, yes. Impossible , no..."
read 732 times
9/22/2011 11:47:29 PM (last edit: 9/22/2011 11:47:29 PM)
show user profile  ccampbell
So after rewriting the script line by line and evaluating... line by line... i managed to get the bra off! ha-ha no... really, Ive gotten passed the error i was having before. Still working on it but it seems to be far more stable now..

Thanks a lot guys for taking the time to look into my amature a=$s script :/




$Entrepreneur = if((Designer + Engineer)*Programmer){Problem Solver};



read 724 times
9/23/2011 2:11:50 AM (last edit: 9/23/2011 2:11:50 AM)
show user profile  khamski
your link is off color... uhm.. is not working


read 715 times
9/23/2011 2:23:47 PM (last edit: 9/23/2011 2:23:47 PM)
show user profile  Dave
For what it's worth I did have a crack... but it looked more and more like a complete re-write jobby. So I gave up!

Strangely... my very first script was also to do with creating splines from (selected) egdes, and I took a very similar approach to the way your script is layed out. I was soon slapped about by the likes of Garp and am still learning how to do things "better".

"I flew over Egypt once"

read 708 times
9/23/2011 3:35:23 PM (last edit: 9/23/2011 3:35:23 PM)
show user profile  ccampbell
Khamski - The link works its just knot a hyperlink. Sorry about that. thanks for checking.

Dave - I'm actually currently working on my first script still. a user interface for the guys in the office that dont know max to build some complex geometry's with out my assistance. The ui script is currently going on 2k lines! I assure you though its not all as bad as the code i posted :P. Ive created something that i think is a bit more flexible and more efficient. I posted the new script below for kicks.

Just to Note: Ive been working with max-script for about 6mo.(11yrs of max). Yes - I have am shameful!

Thanks alot - again to all that have stopped by to help. I should be spending alot more time here contribute where maxscript is less applicable! :D

Script
-------------------------------
newfloat = newrolloutfloater "splinegen" 200 75
rollout splineGen "Border Generator"
(
button btn1 "Generate Border Splines" width:130 height:30 align:#center
on btn1 pressed do
(
--CREATE BORDERS FROM UNWRAPPED GEOMETRY
copySel= copy $
converttopoly $
$.EditablePoly.SetSelection #Edge ((polyOp.getOpenEdges $) as bitarray)
polyop.createshape $ #selection name:"OB_Spline"
delete $
--SEPPERATE SPLINES
select $OB_Spline
copyborders = copy $OB_Spline
deletespline $ 2
select copyborders
deletespline $ 1
copyborders.name = "RH_Spline"
--CONVERT SPLINE SEGS TO #CURVE
for SegNum = 1 to (numsegments $OB_Spline 1) do
(
setsegmenttype $OB_Spline 1 SegNum #curve
)
updateshape $OB_Spline
for SegNum = 1 to (numsegments $RH_Spline 1) do
(
setsegmenttype $RH_Spline 1 SegNum #curve
)
updateshape $RH_Spline
--CONVERT KNOTS TO #SMOOTH
for knotSM = 1 to numknots $OB_Spline do
(
setknottype $OB_Spline 1 knotSM #smooth
setknotselection $OB_Spline 1 #(knotSM) keep:true
)
updateshape $
for knotSM = 1 to numknots $RH_Spline do
(
setknottype $RH_Spline 1 knotSM #smooth
setknotselection $RH_Spline 1 #(knotSM) keep:true
)
--CLEAN SPLINE KNOTS
select $OB_Spline
max modify mode
subobjectlevel = 1
for k = 1 to 2 do
(
numloops = (numknots $OB_Spline)/(2)-1
knotArray=#()
for Opt = 1 to numloops do
(
x=(opt+opt)
append knotArray x
)--end nested i loop
setknotselection $OB_Spline 1 knotArray
max delete
updateshape $OB_Spline
)--end master k loop
--
select $RH_Spline
max modify mode
subobjectlevel = 1
for k = 1 to 2 do
(
numloops = (numknots $RH_Spline)/2
knotArray=#()
for Opt = 1 to numloops do
(
x=(opt+opt)-1
append knotArray x
)--end nested i loop
setknotselection $RH_Spline 1 knotArray
max delete
updateshape $RH_Spline
)--end master k loop
--APPLY OFFSETS TO SPLINES
applyoffset $OB_Spline 5.0
deletespline $OB_Spline 1
updateshape $OB_Spline
applyoffset $RH_Spline 5.0
deletespline $RH_Spline 1
updateshape $RH_Spline
--CREATE INNER BORDER
applyoffset $OB_Spline 25
--select $OB_Spline002
--copyspline = copy $OB_Spline002
--deletespline copyspline 1
--deletespline $OB_Spline002 2

)--end BTN 1
)--end Rollout
addrollout splineGen newfloat rolledup:false

Chris




$Entrepreneur = if((Designer + Engineer)*Programmer){Problem Solver};



read 701 times
9/23/2011 6:46:03 PM (last edit: 9/23/2011 6:49:51 PM)
show user profile  Garp
Just a free tip. You'll thank me later.
You should always put your scripts in their own block. Otherwise variables at the upper scope level are global and sooner or later you (or someone else) will be running two scripts with variables with the same name and if they're global one script will change the value used by the other.
Unless you need globals of course. But that should be extremely rare.




read 693 times
9/23/2011 7:07:04 PM (last edit: 9/23/2011 7:07:04 PM)
show user profile  ccampbell
your absolutely right!! i have been very careful to use different variable names for this. i have tried to make a decent effort not to declare local or global variables directly(i know its wrong) because while i get their purpose i don't fully understand the scope of there implications yet.

among the tons of stuff i dont know i'm going to be looking into "try/catch" and while/do & do/while loops(always crashes max cause i suck!)



On a Side note - just wanted to say i've managed to get the effect i was looking for with this one.

Thank you all very much for your time and awesome help!




$Entrepreneur = if((Designer + Engineer)*Programmer){Problem Solver};



read 684 times
9/23/2011 9:00:54 PM (last edit: 9/23/2011 9:00:54 PM)
show user profile  Garp
For a clear explanation of scope (and some more): maxscript 101

edit:

> i have been very careful to use different variable names for this
That won't work for long. After a dozen or so scripts you'll spend more time trying to find variable names you haven't used yet than writing code.

> i have tried to make a decent effort not to declare local or global variables directly
On the contrary, it's a much better pratcice to always declare local and global variables explicitely. It keeps the code much clearer and saves time when debugging.

The try/catch construct is simple. If the code in the try block triggers an error, instead of stopping the execution with an error message in the listener, it executes the code in the catch block instead.

Loops (for/while/do-while) and tests (if/case) are the essential building blocks of the logic in the scripts. They're among the first things that need to be mastered (along with the syntax ;)




read 670 times
9/24/2011 7:03:04 AM (last edit: 9/24/2011 12:05:46 PM)
show user profile  ccampbell
Garp - you are very right. thanks a lot for the link! you actually gave that to me awhile back when i posted a question about making a slider to move vertex points and Ive watch most of it a few times! i actually just searched to find that posting recently so i could get the link to use the chain maker script from the tuts for a script i had to make haha. I am going to watch the rest there are a few videos ive yet to hit.

I never did get a chance to thank you for helping me out with that slider script before soo... THANKS! :D

I'm slowly catching on. but i have to teach my self and do it while i perform my regular job duty's :/ though its extremely addicting so I'm loving every minute of it.




$Entrepreneur = if((Designer + Engineer)*Programmer){Problem Solver};



read 656 times
9/24/2011 7:43:24 PM (last edit: 9/24/2011 7:44:08 PM)
#Maxforums IRC
Open chat window


Support Maxforums.org