Search code examples
maxscript

How do I assign a property to a reference assignment for a function (without using execute)


I'm hoping to assign a property to a function for MAXScript.

Here's what I've tried:

fn getProp type: = (
  $.'type'
  )
getProp type:'pos'

I would like to pass the pos property as a variable to the function getProp and retrieve it as the same as this code:

$.pos.


For another example to get the scale of the selected object the code would look like this:

fn getProp type: = (
  $.'type'
  )
getProp type:'rotation'

Here's the execute version that I don't want to do as an example:

fn getProp type: = (
  execute("$."+type)
  )
getProp type:#pos
-- will return: [25.051,-115.88,0]

Edit

Full code of what I want to achieve in its context with what works and what doesn't. See line 55.

try(destroydialog ::lvRolTest)catch()
rollout lvRolTest "Check Mate.."
(

  local  _lvSetItemsInvertProp, _lvSetItemsInvertPropNotWorking

  label         countLabelPrefix       "How many you say ?" across:3
  label         countLabelVal          ""
  checkButton   chkLvToggleAllButton   "Toggle All..."
  dotNetControl lv                     "ListView" width:270 height:535 pos:[10,30]

  on lvRolTest open do (
    lv.View                  = (dotNetClass "View").Details
    lv.FullRowSelect         = true
    lv.MultiSelect           = true
    lv.Checkboxes            = true
    lv.Columns.add           ("Items")

    lv.columns.item[0].width = lvRolTest.width-55

    for x=1 to 100 do (
      lv.BeginUpdate()
      local newLvItem = dotNetObject "ListViewItem" ("Item " + x as string)
      newLvItem.checked = true
      lv.EndUpdate()
      lv.items.add newLvItem
      )
    lv.Focus()
    countLabelVal.text = (lv.CheckedItems.count) as string
    )

  on chkLvToggleAllButton changed state do (
    if state do chkLvToggleAllButton.text         = "Toggle All Again..."

    -- This is working
    --     _lvSetItemsInvertProp ("lvRolTest" as string) ("lv" as string) prop:#Checked

    -- This is not
    _lvSetItemsInvertPropNotWorking lv prop:#Checked

    if not state do chkLvToggleAllButton.text     = "Toggle All..."
    countLabelVal.text = (lv.CheckedItems.count) as string
    lv.Focus()
    )

  -- This is working
  fn _lvSetItemsInvertProp rolArg lvArg prop:#Checked = (
    execute("for i = 0 to "+rolArg+"."+lvArg+".items.count-1 do (
      local item = "+rolArg+"."+lvArg+".items.item[i]
      if item."+prop+" == true then item."+prop+" = false else item."+prop+" = true
      )")
    )

  --   This is not
  fn _lvSetItemsInvertPropNotWorking ListView prop:#Checked = (
    for i = 0 to lvRolTest.lv.items.count-1 do (
      local item = getProperty (lvRolTest.lv.items.item[i]) prop
      if item == true then item = false else item = true
      )
    )

  )

createDialog lvRolTest 300 575 pos:[1850,700] style:#(#style_SysMenu, #style_ToolWindow, #style_resizing)

EDIT 2. Updated code with the solution as per Swordslayers comment below.

try(destroydialog ::lvRolTest)catch()
rollout lvRolTest "Check Mate.."
(

  local  _lvSetItemsInvertProp

  label         countLabelPrefix       "How many you say ?" across:3
  label         countLabelVal          ""
  checkButton   chkLvToggleAllButton   "Toggle All..."
  dotNetControl lv                     "ListView" width:270 height:535 pos:[10,30]

  on lvRolTest open do (
    lv.View                  = (dotNetClass "View").Details
    lv.FullRowSelect         = true
    lv.MultiSelect           = true
    lv.Checkboxes            = true
    lv.Columns.add           ("Items")

    lv.columns.item[0].width = lvRolTest.width-55

    for x=1 to 100 do (
      lv.BeginUpdate()
      local newLvItem = dotNetObject "ListViewItem" ("Item " + x as string)
      newLvItem.checked = true
      lv.EndUpdate()
      lv.items.add newLvItem
      )
    lv.Focus()
    countLabelVal.text = (lv.CheckedItems.count) as string
    )

  on chkLvToggleAllButton changed state do (
    if state do chkLvToggleAllButton.text         = "Toggle All Again..."
    if not state do chkLvToggleAllButton.text     = "Toggle All..."
    _lvSetItemsInvertProp lv prop:#Checked
    _lvSetItemsInvertProp lv prop:#Selected
    countLabelVal.text = (lv.CheckedItems.count) as string
    lv.Focus()
    )

  fn _lvSetItemsInvertProp lvArg prop:#Checked = (
    for i = 0 to lv.items.count-1 do (
      local bool = getProperty (lvArg.items.item[i]) prop
      setProperty (lvArg.items.item[i]) prop (not bool)
      )
    )

  )

createDialog lvRolTest 300 575 pos:[1850,700] style:#(#style_SysMenu, #style_ToolWindow, #style_resizing)

Solution

  • You mention several things at once, mapped function (which I don't see a trace of in the sample code and will thus ignore) and getting properties given a variable property name using your own function - surely you tried using getProperty:

    getProperty $ type