[Closed] Using Name of Selected
I know this must be here somewhere… but I can’t find it.
If had an object selected that was named “objectA”, and I wanted to create a Dummy helper that included the selected object’s name… what would be the syntax for that? For example to end up with “objectA_DUMMY”.
Thanks!
$.name+"_Dummy"
.name returns the name property of the current object as string. It works for anything that has a name property so $.material.name would return the name of that objects material. To loop through a selection make sure to get a single object and then retrieve the name property. Since you have 2 strings you can now do a simple + operation to combine them.
-Eric
Thanks for the info Eric!
I tried the code below but I’m not sure why it’s not working:
dummy name:$.name+"_original_object_location_DUMMY"
Thanks again.
try:
for obj in selection do
(
dummy name:(obj.name + "_original_object_location_DUMMY") pos:obj.pos
)
I think the reason yours isn’t working is because you are calling $ which returns selection… if you had more than one object selected it would return an array, which doesn’t have a name. You need to reference it directly from an object at that point. not sure about if you only had one selected… in theory maybe it should work, but in practice I don’t think calling $ while trying to create an object works. The code I gave you will work on an entire selection of objects or a single obj as well.
Finally got back to this.
Thanks very much for the ideas! That does work but then led me to this which also works and is a bit simpler yet:
dummy name:($.name + "_original_object_location_DUMMY")
Thanks again.
Be careful with using the $ global directly like that. If you’d have multiple objects selected, your script would crash.
This is because the type of $ will change:
-with no objects selected, $ will be of undefined type.
-with one object selected, $ is an object type.
-with more than one object selected, $ is an array of objects.
Therefore, Solitude’s solution is safer. He only typed selection instead of $selection
Personally, I always work like this with selections:
(
local sel = getCurrentSelection() --Always returns an array
for obj in sel do
(
--do something with the object.
)
)
Pier,
Thanks very much for the tips! Didn’t realize that.
Thanks again.
I usually do something very similar to yours… I wonder if there is a difference, but have yet to open max today.
sel = selection as array
From the maxscript documentation:
getCurrentSelection()
Returns the current selection as an array. This is similar to selection as array, however selection as array returns a copy of the selection.
That does make me wonder, if getCurrentSelection() isn’t a copy, then what would happen if you’d start modifying the array? hmm