|
created Maxscript: Accessing a property via a variable syntax?
on 08-11-2010 01:03 PM
I can't seem to figure out how to write this. I want a function to be able to modify a property.
Code:
fn myFunc n attribute value =
(
n.attribute = value
)
myFunc $ height 50
It will tell me that my object n doesn't have a property called 'attribute'.
What's the syntax for this?
|
, polygon,
700 Posts,
Join Date Nov 2008,
Location Vancouver
|
Dont know maxscript but I think you need to specifiy what attribute you want to access so like x,y,z etc. (I should really learn max so I can do maxscript stuff)
WebGL Project - In a state of constant flux so might be down when you check it. Pretty old now.
Junior Technical Artist at Digital Extremes.
|
, dedicated polycounter,
1,855 Posts,
Join Date Nov 2009,
Location Toronto
|
hmm.
well I'll go on a limb here - to do what you are trying to do, look up the 'execute' command. In all honesty using it is usually shonky, but if it's for a quick and dirty script why the hell not try it.
There is another way to check for the presence of attributes/properties and then work on them - look up hasproperty and isproperty and how they work.
Have fun!
|
, polycounter,
776 Posts,
Join Date Jan 2006,
Location UK
|
You need to use the execute command to assemble a string to evaluate.
You will also need to pass "height" as a string - currently you're trying to pass it as a variable, therefore it will end up passing undefined at the moment (unless you defined height as a string value of "height" earlier in the code, which seems unlikely).
Try this:
Code:
fn myFunc n attribute value =
(
cmd = ( "$" + n.name + "." + attribute + " = " + value as string )
execute cmd
)
myFunc $ "height" 50
Edit: Beaten to the punch by cw! But mine has a tested working code example ;)
Edit2: Actually this is really dodgy since I'm using $<name> to set the property, which is bad since you might have multiple objects in the scene with the same name.
There's probably a better way to get the actual node in there, I'm just too lazy to look it up at the moment 
|
, MoP,
11,603 Posts,
Join Date Oct 2004,
Location London, UK
|
Beautiful, thanks MoP. I knew I needed to concatenate with the '+' somehow, but it would still complain about the syntax. execute command to the rescue!
Cheers all.
|
, polygon,
700 Posts,
Join Date Nov 2008,
Location Vancouver
|
O I see what he was trying to do....now I look like an idiot  Sounds like a nifty script...
just confused as to why you have to use the execute command within the function
Last edited by haiddasalami; 08-11-2010 at 01:32 PM..
WebGL Project - In a state of constant flux so might be down when you check it. Pretty old now.
Junior Technical Artist at Digital Extremes.
|
, dedicated polycounter,
1,855 Posts,
Join Date Nov 2009,
Location Toronto
|
haiddasalami: You have to use "execute" because you're assembling a command as a string.
You have to pass the variables to the function as strings because you can't just access the node properties for any generic attribute (since height on its own is just a variable name, not a property of an object).
|
, MoP,
11,603 Posts,
Join Date Oct 2004,
Location London, UK
|
Quote:
Originally Posted by MoP
haiddasalami: You have to use "execute" because you're assembling a command as a string.
You have to pass the variables to the function as strings because you can't just access the node properties for any generic attribute (since height on its own is just a variable name, not a property of an object).
|
ah thanks for clearing that up MoP 
WebGL Project - In a state of constant flux so might be down when you check it. Pretty old now.
Junior Technical Artist at Digital Extremes.
|
, dedicated polycounter,
1,855 Posts,
Join Date Nov 2009,
Location Toronto
|
Quote:
Originally Posted by MoP
You need to use the execute command to assemble a string to evaluate.
You will also need to pass "height" as a string - currently you're trying to pass it as a variable, therefore it will end up passing undefined at the moment (unless you defined height as a string value of "height" earlier in the code, which seems unlikely).
Try this:
Code:
fn myFunc n attribute value =
(
cmd = ( "$" + n.name + "." + attribute + " = " + value as string )
execute cmd
)
myFunc $ "height" 50
Edit: Beaten to the punch by cw! But mine has a tested working code example ;)
Edit2: Actually this is really dodgy since I'm using $<name> to set the property, which is bad since you might have multiple objects in the scene with the same name.
There's probably a better way to get the actual node in there, I'm just too lazy to look it up at the moment 
|
Ok MoP, I see your code example and raise you another (just for fun, like..)
Code:
fn myFunc n attribute value =
(
--cmd = ( "$" + n.name + "." + attribute + " = " + value as string )
--execute cmd
-- proper way! ;)
objs = n as array
for obj in objs do
(
if hasproperty obj attribute then
(
setProperty obj attribute value
)
)
)
myFunc $ "height" 50
There are long and complex reasons why I didn't post one originally, revolving around sudoku and nintendo DS. I shan't bore you with all that though. Have fun!
Last edited by cw; 08-12-2010 at 05:48 AM..
|
, polycounter,
776 Posts,
Join Date Jan 2006,
Location UK
|
That's much better, cw - thanks for the good example!
I knew execute seemed pretty dodgy... I've been out of the loop on maxscript for a few years now though 
|
, MoP,
11,603 Posts,
Join Date Oct 2004,
Location London, UK
|
cw: Thanks, that's cleaner. I actually needed to do some different things than set a basic property in my real function, but your reply set me down the path to a cleaner solution.
Strangely, 'hasProperty' will return false if you're querying about a custom attribute. 'isProperty', however, will work just fine.
I ran into issues earlier when I also needed to test the state of a property as well, since I couldn't get at it with the usual object '.' notation. But I ran across a getProperty function after seeing your example using setProperty.
Code:
fn myFunc n attribute =
(
return (isProperty n attribute and getProperty n attribute)
)
myFunc $ "internalCustomAttribute"
I love simple solutions.
|
, polygon,
700 Posts,
Join Date Nov 2008,
Location Vancouver
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Copyright 1998-2012 A. Risch
|