Notifications
Clear all

[Closed] Dynamic object list

I have a script that can create a variable number of objects. All the objects have an object name that starts with “cg_”, such as:

cyl1=chamfercyl name:“cg_cyl1” radius:globalradius.value

I have a spinner (“globalangle”) on my UI that can rotate any object created by this script by grouping all the objects whose names start with “cg_”, and it works great – like this:

on globalangle changed true do
(
allcyls = $cg_*
for i in allcyls do
(
i.rotation.controller.Z_rotation = labelangle.value – cylangle.value
)
)

Now, for a different reason, I might want to use the same artifice, but not with object names, but with the object variable name, in my case, all variable names starting with “cyl”. Using the same “$cyl*” wildcard doesn’t work. Is such a thing possible? I’ve searched but I’m having a lot of trouble finding relevant info. Thank you!

3 Replies

If I understand you correctly, no.

  Instead you would put the newly created objects into an array, then reference the members of that array. Seeing as you're creating the objects yourself, that should be trivial.

EDIT: Actually, technically, you could. You would use “execute” to access the variable’s names programatically, but there are partciular disadvantages with this method (speed, scope) and it’s generally accepted that using execute where it can be avoided is bad programming practice.

The first method is by far the best.

AllObjs = #() 
AllCyls = #()
 
AllObjs = $objects as array
for i = 1 to AllObjs.count do
(
 if MatchPattern AllObjs[i].name pattern:"cg_*" == true then append AllCyls AllObjs[i] 
)


Or as a Function:


fn getObjs ObjFilter = 
(
 local AllObjs = #() 
 local Filtered = #()
 
 AllObjs = $objects as array
 for i = 1 to AllObjs.count do
 ( 
  if MatchPattern AllObjs[i].name pattern:ObjFilter == true then 
  ( 
   append filtered AllObjs[i] 
  ) 
 )
 Filtered
)
AllCyls = GetObjs "cg_*"


thatoneguy: thank you so much, that worked perfectly! I used your simple code version, but I will use the fn for the next version of this script. Brilliant!

davestweart: thanks for your input as well. Right now I’m mostly worried about makign things work rather than being programatically elegant but I do appreciate what you’re saying. Cheers