PDA

View Full Version : execute string in loop


floatingworld
07-20-2011, 09:51 AM
Hello. The code below is meant to place custom attribute parameters on a spline but all it does is place the first one (morphs_used [1] = up ) on it. I've tried every variation of "execute str" and placement of the string but nothing seems to get the desired result. Any suggestions?
Thank you for your time.

ctrl = getnodebyname "FaceControl001"

morphs_used = #("up", "down", "turnaround")

addmodifier ctrl (EmptyModifier ())

str = "for i = 1 to 3 do (
(attdef = attributes facialctrl
( parameters face

(
" + morphs_used[i]+" type:#float;


)

)
)

)
"

execute str

custAttributes.add ctrl.modifiers[1] attdef




--This doesn't seem to work either:

ctrl = getnodebyname "FaceControl001"
morphs_used = #("up", "down", "turnaround")
addmodifier ctrl (EmptyModifier ())

for i = 1 to 3 do (
str = " (attdef = attributes facialctrl
( parameters face

(
" + morphs_used[i]+" type:#float;


)

)
)

"
)
execute str

custAttributes.add ctrl.modifiers[1] attdef

JHN
07-20-2011, 01:40 PM
Please wrap the code bits in a code block in the editor press the "#" button!
It's unreadable.

Thanks,
-Johan

floatingworld
07-20-2011, 01:45 PM
yes. I have done so. Don't know if it makes matters clearer, however.

JHN
07-20-2011, 02:14 PM
Maybe some more effort in proper formatting with yield some faster feedback, but..
your first idea is not sound at all, the for loop in wrapped in a string that makes 3 attributes, and the result of the for loop, what I assume will be undefined or void is what you assign to the custAttributes. This makes no sense.
The second idea is better, but doesn't work because of bad code(!?).
So rewriting what you want to do gives me code that looks like this:


ctrl = $
-- ctrl = getnodebyname "FaceControl001"

morphs_used = #("up", "down", "turnaround")

addmodifier ctrl (EmptyModifier ())

for i = 1 to morphs_used.count do
(
local attStr = ""
attStr += "attdef = attributes facialctrl\n"
attStr += "(\n"
attStr += " parameters face\n"
attStr += " (\n"
attStr += " " + morphs_used[i] + " type:#float\n"
attStr += " )\n"
attStr += ")"

execute attStr
custAttributes.add ctrl.modifiers[1] attdef
)

That leaves me with one question, why would you want to have a custom attribute per morph channel used, wouldn't it make more sense to make 1 custom attribute with an array parameter and write to the array param the channels you used. Image having 20 channels, resulting in 20 CA's on this modifier alone.... Something to think about.

-Johan

eek
07-20-2011, 02:54 PM
... why would you want to have a custom attribute per morph channel used, wouldn't it make more sense to make 1 custom attribute with an array parameter and write to the array param the channels you used. Image having 20 channels, resulting in 20 CA's on this modifier alone.... Something to think about.

-Johan

One reason for me anyway is to store the order they were created for combination (corrective), and to store the space world value, pre turned into a corrective. Wish CA's could store nested arrays..

For this instance thought - you could just iterated though the channels something like so:

for c = 1 to 100 where WM3_MC_HasData morpher c do
(
append myAttrib.myStringArray morpher[c].name
)

floatingworld
07-20-2011, 05:56 PM
Thank you both, but running your code, I get three CAs, when what I need are three parameters under one CA,( 'facial control')....The parameters are necessary in order to instance controllers so I can have a holder for poses on the spline.
Yes it would be better to have a parameter array - that was my original idea - but how do I get rid of the string quotes? morphs_used[1] is "up", but I want just up.

JHN
07-20-2011, 06:05 PM
Did you read my post? Obviously I was wondering the same thing. What you want is already there just shuffle the for loop.

-Johan

floatingworld
07-20-2011, 06:36 PM
yes JHN. Of course I read your post, and I have done a lot - and I mean a LOT of shuffling of the for loop, and got nowhere... either I get 'name expected 'errors, or I just get the first member of the array as parameter.

JHN
07-20-2011, 06:45 PM
(
ctrl = $
-- ctrl = getnodebyname "FaceControl001"

morphs_used = #("up", "down", "turnaround")

addmodifier ctrl (EmptyModifier ())



local attStr = ""
attStr += "attdef = attributes facialctrl\n"
attStr += "(\n"
attStr += " parameters face\n"
attStr += " (\n"

for i = 1 to morphs_used.count do
(
attStr += " " + morphs_used[i] + " type:#float\n"
)

attStr += " )\n"
attStr += ")"

execute attStr
custAttributes.add ctrl.modifiers[1] attdef
)


-Johan

floatingworld
07-21-2011, 05:20 AM
Well, thanks heaps JHN. It works. However, the compilier didn't like the local declaration...said something about :"no local declarations at top level: attStr"...apart from that, when I got rid of the local declaration, it ran fine. Also, the first two parameters appear in upper case under the CA modifier, the third in lower case. Weird. Are you up on why that happened? Anyways, thanks - youre a godamn genius,man.

JHN
07-21-2011, 07:17 AM
It happens because you didn't copy the whole code, notice the ( and ) around my code. You're probably working without those in your code. I highly recommend to always use that around all code, your code will improve and probably debug better, because less globals floating around. Read up on local and global variables.

-johan

floatingworld
07-21-2011, 09:37 AM
yes, good point. I don't know why I couldn't do it the first time I tried. I get very confused when mixing strings and literals and maxscript commands and executes...I don't know what to enclose in quotes and what not to....thanks for the clarification.I was going cross-eyed. There's got to be an easier way! I'm writing to that guy who came up with maxscript in UK and giving him a piece of my mind.

CGTalk Moderation
07-21-2011, 09:37 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.