Python script: NameError: name 'Script' is not defined

Thanks, I actually have this post copied to my local guide for reference (I mean, that's where I got those _FlagAsMethods from), just haven't read it completely and thoroughly enough and didn't know the answer was already there :slight_smile:

doesn't seem like it, I get objects of type 'dict' can not be converted to a COM VARIANT

You mean something like this?

dict = {'DEBUG': 'DbgDescription1', 'DEBUG2': 'DbgDescription2'}
myMap = factory.Map()
for i, k in enumerate(dict):
  myMap[k] = dict[k]
inputData.config_desc = myMap

Is this still preferable over using the Map directly (also curious to know why)?

Regarding using python types vs DOpus, it's just a matter of convenience in fact, but it's easy to fall into DOpus types hole when you forget that they really are just necessary COM-related workarounds.
I will try to add some smart handling of DOpusFactory stuff in dopus_proxy.

By the way, even when I define some config initData.config.CfgName1 that are supposed to be also attached to the Script.config, this fails:

for v in Script.config:
  DOpus.Output(v)

...so I can't enumerate all my config Names and also can't get them like Script.config("CfgName1")(I get COM Error: Invalid index) instead of Script.config.CfgName1 (I thought I could store config values in one loop instead of having to manually address each)

This is expected, right, due to these COM workarounds and such?

Can you paste the whole block of code? Hard to follow what you have on mind.

See comment #1, #2, #3 in OnTestCmd1 (this is simplified, I use Sv.set("hideAttr", Sc.hideAttr) type of commands in a separate function that can be called in def OnScriptConfigChange(configChangeData): to update script vars)

import win32traceutil
def OnInit(iData): #Init script
  iData.name          	= "test";
  iData.version       	= "1.0";
  iData.copyright     	= "20";
  # iData.url         	= "";
  iData.desc          	= "test";
  iData.default_enable	= True;
  iData.min_version   	= "12.5";

  iData.config.DebugOutput = True;
  iData.config.DEBUG = "dbgF";
  iData.config.hideAttr = "a"
  DOpus._FlagAsMethod('Create')
  factory = DOpus.Create()
  factory._FlagAsMethod('Map') # Map["Set"]= "" # Map("Get")
  dict = {'DEBUG': 'DbgDescription', 'hideAttr': 'hideAttrDescription'}
  myMap = factory.Map()
  for i, k in enumerate(dict):
    myMap[k] = dict[k]
  iData.config_desc = myMap

  iData._FlagAsMethod('AddCommand')
  cmd         = iData.AddCommand()
  cmd.name    = 'TestCmd1'
  cmd.method  = 'OnTestCmd1'
  cmd.desc    = iData.desc
  cmd.label   = 'TestCmd1'

def OnTestCmd1(scriptCmdData):
  Sv, Sc = Script.vars, Script.config

  #1# I can manually set script variables rom script configs
  Sv.set("hideAttr", Sc.hideAttr)
  # Sv.set("DEBUG", Sc.DEBUG)
  # #... and later retrieve them
  dbg(Sv.get("hideAttr"))

  #2# But I can't enumerate Script.config
  for cfg in Sc:
    dbg(cfg)
  # And I can't set in a way that would accept strings
  Sv.set("hideAttr", Sc["hideAttr"]) #TypeError: This object does not support enumeration
  Sv.set("hideAttr", Sc("hideAttr")) # COM Error: Invalid index.

  #3# So I can't get iterate config properties to save them like so
  for cfg in Sc:
    Sv.set(cfg, Sc(cfg))
  dbg(Sv.get("DEBUG")) # Fails

def dbg(text):
  try:
    if (Script):
      pass
    if (0 or Script.config.DebugOutput):
      DOpus.Output(str(text))
  except Exception as e:
    DOpus.Output(str(type(e)))
    print(e) # print(e.hresult, e.strerror, e.excepinfo, e.argerror)
    DOpus.Output(str(e))

I don't have the beta DO, so I cannot play with Script, but some remarks:

  • Script.config looks like a namespace, not collection, so no surprise it's not iterable. No surprise that you cannot access it with setitem/call notation either.
  • To avoid all this conundrums, I would simply pickled/shelved my stuff into known location. As all this would probably remain in disk cache throughout all script execution, filesystem overhead of accessing data would be totally negligible.
  • If you insist on Script API, put there a known item with index of all other stuff you attach to it.