PDA

View Full Version : problem using "Case Of" expression...


cbass
05-12-2008, 09:07 PM
Hi everyone,


Im having an issue in the following script.

In an earlier version of this script, an pre-drawn object (Pyramid01 ) is copied and aligned to one of its 12 faces. The face is referenced using a variable n and a number between 1 and 12 (changing the n variable value from 1-12 changes the location of the new object). This worked fine.

However, I wanted to designate the face using letters instead of numbers, so I added the case of statement. When the script runs, some letters (like d for instance) dont return the right value and the newly placed shape ends up in the wrong place. Other times, the letters dont seem to affect the outcome at all and the shape keeps getting placed at the same location (is this a scope issue?). I probably have incorrect syntax for the case of expression too, but Ive looked at the help and online and this is the best Ive come up with (and like I said, it has worked intermittently in the past).

Im including both the script that works with numbers, the script Im having issues with and the file that goes with either script, if anyone has the time/inclination to help. Im still a noob when it comes to programming, so any suggestions are welcome.

TIA!


Carlo




---------------------------
original working code


-- This is a sample code using "Rhombic Dodecahedron super clean 05 10 2008.max" file.
-- The file has one object of Editable-Poly whose pivot is located (0,0,0) and named as "Pyramid01"
-- This is to move a copied object to the position of other face(n).

a = $Pyramid01 -- original object
b = copy a -- keep one
n = 2 -- index of face in Box01 range [1 ~ 12] -- Change this value to change position of object
firstID = 2 -- there are two cases 2 or 4 to speciy the X local axis using the 2nd or 4th vertex.
theCenter = polyOp.getFaceCenter a n --Nth face's center
axisZ = polyOp.getFaceNormal a n --the normal direction of Nth face
theVert = (a.GetFaceVertex n firstID) -- get the second vertexID of Nth face
theVertPos=polyOp.GetVert a theVert -- get the world position of the vertex
axisX =normalize [(theVertPos.x - theCenter.x), (theVertPos.y - theCenter.y), (theVertPos.z-theCenter.z)] -- create local X-axis
axisY = normalize (cross axisX axisZ) -- create local Y-axis
theTM = matrix3 axisX axisY axisZ theCenter -- create Transform Matrix
b.transform = theTM -- move the copied objec to the Nth face of Original obj.






----------------------
non-working code



FaceSel = c -- input a letter from "a" to "l" to change position of new object
case FaceSel of

(
a : n=1
b : n=2
c : n=3
d : n=4
e : n=5
f : n=6
g : n=7
h : n=8
i : n=9
j : n=10
k : n=11
l : n=12
)

t = $Pyramid01 -- original object
s = copy t -- keep one
n -- index of face in Box01 range [1 ~ 12]
firstID = 2 -- there are two cases 2 or 4 to speciy the X local axis using the 2nd or 4th vertex.
theCenter = polyOp.getFaceCenter t n --Nth face's center
axisZ = polyOp.getFaceNormal t n --the normal direction of Nth face
theVert = (t.GetFaceVertex n firstID) -- get the second vertexID of Nth face
theVertPos=polyOp.GetVert t theVert -- get the world position of the vertex
axisX =normalize [(theVertPos.x - theCenter.x), (theVertPos.y - theCenter.y), (theVertPos.z-theCenter.z)] -- create local X-axis
axisY = normalize (cross axisX axisZ) -- create local Y-axis
theTM = matrix3 axisX axisY axisZ theCenter -- create Transform Matrix
s.transform = theTM -- move the copied objec to the Nth face of Original obj.

davestewart
05-12-2008, 10:21 PM
Hi Carlo,
I'm not too sure what you're aiming to do with the letters (is this for keyboard input of something?) but you might like to use strings, not variables.


FaceSel = "c"
case FaceSel of
(
"a" : n=1
"b" : n=2
"c" : n=3
-- etc


But as I said, I can't really see what you're trying to do. Seems to me that you'd just be better off with an index... are you over complicating things!? (a common noob practice!)

Hope that helps,
Dave

cbass
05-12-2008, 10:55 PM
Hey, thanks Dave! Using strings certainly did the trick.

I may be guilty of overcomplicating things. It wouldn't be the first time (nor probably the last!)

As for what I'm trying to do, its a script that will eventually position several identical objects in space based on a pattern of values. Sort of like turtle graphics. Ideally the script will get its information from a string without delimeters. I'm including an image of all 12 possible values.

The reason I was using letters is because there are 12 possible values and there is no way to discriminate between "1+12" and "11+2" in the string "112". So iI figured using letters would be more suitable (i.e. "al" must equal 1+12 based on the case expression I've established).

What is an index? I didn't find it listed under "controlling program flow" in the help file. Do you have an example or could you link to a site that does?... That might be exactly what I need.

Thanks!

Carlo

davestewart
05-13-2008, 12:02 AM
Hey again Carlo,
An index basically means "a number within a range", so in this case I was referring to your range of 1 - 12. Indices are always integers and are normally associated with arrays (although they can be associated with strings as well, if you consider them an array of characters):

arr = #("a","b","c")
index = 2
print arr[index]
-- "b"


It sounds like you may be better off using an array in this case, rather than converting your numbers to letters, then working out what's what by converting back again:

arr = #(4, 5, 12)
for i = 1 to arr.count do print arr[i]
-- 4
-- 5
-- 12

Arrays are great, you'll use them all the time once you get into programming. It's a handy way to group related items. Arrays have useful functions like "findItem" and properties like "count", so you always know how many items are in them.

arr.count
--3

findItem arr 4
-- 1

findItem arr 12
-- 3

Good luck!
Dave

CGTalk Moderation
05-13-2008, 12:02 AM
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.