Difference between revisions of "Query Command Values example"

From The Foundry MODO SDK wiki
Jump to: navigation, search
Line 1: Line 1:
 +
== Querying the item.name command ==
 +
 +
item.name's ''name'' argument can be queried to get the names of the currently selected items.
 +
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 +
# by Gwynne Reddck
 
# Need a command service object
 
# Need a command service object
 
cmd_svc = lx.service.Command()
 
cmd_svc = lx.service.Command()
Line 28: Line 33:
 
#  methods for it.
 
#  methods for it.
 
# Here we use GetString(), but you'll generally want to use whatever is
 
# Here we use GetString(), but you'll generally want to use whatever is
#  appropriate for this argument's datatype,
+
#  appropriate for this argument's datatype.  The 0 indicates that we're
 +
#  getting the first value in the array.  item.name will return an array
 +
#  with the names of all of the currently selected items, so we have to
 +
#  tell it which one we want.
 
name = va.GetString(0)
 
name = va.GetString(0)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
[[Category: API Examples]]
+
== Querying the pref.value for a color ==
[[Category: Python API]]
+
 
[[Category: lx.service]]
+
The pref.value command is used to set and get the values of many preferences in the application.  Not all prefs use pref.value; some use custom command instead, but all are displayed in the Preferences forms and represent some global option.
 +
 
 +
pref.value's first argument is the preference we want to query or set, and the second argument is the value of the preference.  The datatype of the second argument is defined by the preferences specified in the first.  In this example, the color.wireframe preference is queried, which is defined as a ''color'' datatype.  We choose to parse the value as a string, which is in the form of ''<nowiki>[R G B]</nowiki>'', where the color components are floating point numbers.
 +
 
 +
<syntaxhighlight lang="python">
 +
# Pick the colors from the preferences
 +
# by David Ballesteros
 +
 
 +
# The command string that would be typed into the command history is "pref.value color.wireframe ?".
 +
#  We're going to be a bit more verbose and use "pref.value name:color.wireframe value:?".
 +
 
 +
# First we need to get the command service
 +
cmd_svc = lx.service.Command()
 +
 
 +
# Next we spawn the pref.value command.
 +
cmd = cmd_svc.Spawn(lx.symbol.iCTAG_NULL, 'pref.value')
 +
 
 +
# We pass the arguments without the command name, marking the argument we want to query the
 +
#  value of with a question mark.  As with the other example the second argument (flags) is 0 so
 +
#  that no dialogs will open on failure, and the last argument is 0 (includCmd) indicating that the
 +
#  argument string does not include the command's name.
 +
# The returned values are a value array containing all of the queried values, which in this case
 +
#  will only be a single value, and the index of the argument that was queried.
 +
va, idx = cmd_svc.QueryArgString(cmd, 0, 'name:color.wireframe value:?', 0)
 +
 
 +
# We then get the value from the array as a string, and split it using the Python string split() method.
 +
#  This just separates the string at the spaces, returning the components as an array.
 +
rgblist = va.GetString(0).split()
 +
</syntaxhighlight>
  
 
== See Also ==
 
== See Also ==
Line 41: Line 77:
 
* [[ILxCommandService (index)#(21) SDK: Declarations|Alert Flags]]
 
* [[ILxCommandService (index)#(21) SDK: Declarations|Alert Flags]]
 
* [[Command System]]
 
* [[Command System]]
 +
 +
[[Category: API Examples]]
 +
[[Category: Python API]]
 +
[[Category: lx.service]]

Revision as of 13:37, 16 August 2013

Querying the item.name command

item.name's name argument can be queried to get the names of the currently selected items.

# by Gwynne Reddck
# Need a command service object
cmd_svc = lx.service.Command()
 
# With that we can spawn an instance of the command we're interested in.
#  The first argument is pretty much always CTAG_NULL; the second is the
#  name of the command.
cmd = cmd_svc.Spawn(lx.symbol.iCTAG_NULL, 'item.name')
 
# We then pass the arguments for the command to 'QueryArgString()'.
# The second argument is the alert flags, which are usually 0 since you
#  rarely want alert dialogs opening when you query.
# The third argument is the argument string to be parsed.  The argument
#  marked with a '?' (in this case, "name') is the one that will be queried.
#  Since this is parsing an argument string, standard command string
#  parsing behaviors apply.
# The final argument is indicates if the string includes the command's
#  name or not; ours doesn't so we set it to 0 (false).
va, idx = cmd_svc.QueryArgString(cmd, 0, 'name:?', 0)
 
# The above gives us a value array object containing the queried value,
#  from which we can extract using the correct method for the argument
#  type as 'va' It also gives us the argument index (as 'idx') that was
#  queried (meaning, the one marked with the '?'), which is useful if we
#  want to execute the command with the queried value.  Furthermore,
#  any other arguments passed to QueryArgString() as the command
#  string will now be set as though you had called one of the Set...()
#  methods for it.
# Here we use GetString(), but you'll generally want to use whatever is
#  appropriate for this argument's datatype.  The 0 indicates that we're
#  getting the first value in the array.  item.name will return an array
#  with the names of all of the currently selected items, so we have to
#  tell it which one we want.
name = va.GetString(0)

Querying the pref.value for a color

The pref.value command is used to set and get the values of many preferences in the application. Not all prefs use pref.value; some use custom command instead, but all are displayed in the Preferences forms and represent some global option.

pref.value's first argument is the preference we want to query or set, and the second argument is the value of the preference. The datatype of the second argument is defined by the preferences specified in the first. In this example, the color.wireframe preference is queried, which is defined as a color datatype. We choose to parse the value as a string, which is in the form of [R G B], where the color components are floating point numbers.

# Pick the colors from the preferences
# by David Ballesteros
 
# The command string that would be typed into the command history is "pref.value color.wireframe ?".
#   We're going to be a bit more verbose and use "pref.value name:color.wireframe value:?".
 
# First we need to get the command service
cmd_svc = lx.service.Command()
 
# Next we spawn the pref.value command.
cmd = cmd_svc.Spawn(lx.symbol.iCTAG_NULL, 'pref.value')
 
# We pass the arguments without the command name, marking the argument we want to query the
#  value of with a question mark.  As with the other example the second argument (flags) is 0 so
#  that no dialogs will open on failure, and the last argument is 0 (includCmd) indicating that the
#  argument string does not include the command's name.
# The returned values are a value array containing all of the queried values, which in this case
#  will only be a single value, and the index of the argument that was queried.
va, idx = cmd_svc.QueryArgString(cmd, 0, 'name:color.wireframe value:?', 0)
 
# We then get the value from the array as a string, and split it using the Python string split() method.
#  This just separates the string at the spaces, returning the components as an array.
rgblist = va.GetString(0).split()

See Also