CMB6 - Access resource in related operator?

Hi Guys,

SMTK / CM question,

I am able to create a resource through the Operator panel and assign session in my operator

 # Create malone Resource
  rsrc = cheers.malone.Resource.create()
        # Assign malone session to malone resource
        sess = cheers.malone.Session.create()

How do I access this resource in a related operator?

Thanks,

Corey

Hi Corey,

An operator must explicitly state that is related to a resource. For example, here is the description for smtk::bridge::mesh::Write:

<?xml version="1.0" encoding="utf-8" ?>
<!-- Description of the CMB mesh Model "write" Operation -->
<SMTK_AttributeResource Version="3">
  <Definitions>
    <include href="smtk/operation/Operation.xml"/>
    <AttDef Type="write" Label="Model - Write Resource" BaseType="operation">
      <AssociationsDef>
          <Accepts><Resource Name="smtk::bridge::mesh::Resource"/></Accepts>
      </AssociationsDef>
    </AttDef>
    <!-- Result -->
    <include href="smtk/operation/Result.xml"/>
    <AttDef Type="result(write)" BaseType="result"/>
  </Definitions>
</SMTK_AttributeResource>

Please note the <AssociationsDef> tag above.

An operation’s associated resources are used to determine the availability of the operation in ModelBuilder. For example, if an operation has no associations, then it is always available for execution. When an operation is associated with a smtk::bridge::mesh::Resource, it will be available for selection from the Operations panel when a smtk::bridge::mesh::Resource is selected in the Resources panel.

Finally, in SMTK 2 a session is associated with a resource, not an operation. This is a departure from SMTK 1, where a session described the operators it supported. In SMTK 2, all operators can be created, but many have inputs that are scoped to a specific resource type.

To summarize, in your operation, you would

  1. Mark your operation as associated with a resource
  2. Access the resource in the operation’s operateInternal() method via the operation’s parameters()
  3. Access the session through the resource’s session() method.

Sincerely,
T.J.

HI TJ,

The operator is related to malone but I am not sure if malone resource is created. I see the cube. I am assumed the resource was created.
resource-screen

Thanks

Hmm… would you mind giving the resource a dummy location (rsrc.setLocation('/path/to/foo'))? If this change makes the resource have associated text, then I have a fix for this bug.

Dummy location confirmed to move us down the road so looks like a bug has been found.

After rsrc.setLocation. I am able to see the text in the resource. The operator doesn’t recognize this resource until I create a model. In the related operator, I am trying to get the resource with self.parameters().associations(). Is it possible for me to convert this to a malone resource through Python?

A trick we use to do just this can be seen in smtk/bridge/multiscale/pybind11/PybindResource.h:30:

    .def_static("CastTo", [](const std::shared_ptr<smtk::resource::Resource> i) {
        return std::dynamic_pointer_cast<smtk::bridge::multiscale::Resource>(i);
      })

This worked with the .def_static as you described. Thanks!