View Full Version : Maxscript: Accessing a property via a variable syntax?
I can't seem to figure out how to write this. I want a function to be able to modify a property.
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?
haiddasalami
08-11-2010, 01:15 PM
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)
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!
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:
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 :)
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.
haiddasalami
08-11-2010, 01:30 PM
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
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).
haiddasalami
08-11-2010, 03:25 PM
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 :)
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:
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..)
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!
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 :(
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.
fn myFunc n attribute =
(
return (isProperty n attribute and getProperty n attribute)
)
myFunc $ "internalCustomAttribute"
I love simple solutions.
vBulletin® v3.8.4, Copyright ©2000-2013, Jelsoft Enterprises Ltd.