# CMB6 - Access resource in related operator?

**URL:** https://discourse.kitware.com/t/cmb6-access-resource-in-related-operator/143
**Category:** Computational Model Builder (CMB)
**Created:** [August 17, 2018, 4:47pm UTC](https://discourse.kitware.com/t/cmb6-access-resource-in-related-operator/143 "2018-08-17T16:47:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![chart3388](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@chart3388](https://discourse.kitware.com/u/chart3388)
#### Post date: [August 17, 2018, 4:47pm UTC](https://discourse.kitware.com/t/cmb6-access-resource-in-related-operator/143/1 "2018-08-17T16:47:29Z")

</div>

Hi Guys,

SMTK / CM question,

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

```auto
 # 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

---

<div class="post-metadata">

### Author: ![tj.corona](https://discourse.kitware.com/user_avatar/discourse.kitware.com/tj.corona/32/35_2.png) [@tj.corona](https://discourse.kitware.com/u/tj.corona)
#### Post date: [August 17, 2018, 5:53pm UTC](https://discourse.kitware.com/t/cmb6-access-resource-in-related-operator/143/2 "2018-08-17T17:53:14Z")

</div>

Hi Corey,

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

```auto
<?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.

---

<div class="post-metadata">

### Author: ![chart3388](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@chart3388](https://discourse.kitware.com/u/chart3388)
#### Post date: [August 17, 2018, 6:35pm UTC](https://discourse.kitware.com/t/cmb6-access-resource-in-related-operator/143/3 "2018-08-17T18:35:47Z")

</div>

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](https://discourse.kitware.com/uploads/default/original/1X/0f8593755b2e7bd45aa2cb965b8722c98f7aee24.png)

Thanks

---

<div class="post-metadata">

### Author: ![tj.corona](https://discourse.kitware.com/user_avatar/discourse.kitware.com/tj.corona/32/35_2.png) [@tj.corona](https://discourse.kitware.com/u/tj.corona)
#### Post date: [August 17, 2018, 6:40pm UTC](https://discourse.kitware.com/t/cmb6-access-resource-in-related-operator/143/4 "2018-08-17T18:40:05Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![chart3388](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@chart3388](https://discourse.kitware.com/u/chart3388)
#### Post date: [August 17, 2018, 7:54pm UTC](https://discourse.kitware.com/t/cmb6-access-resource-in-related-operator/143/5 "2018-08-17T19:54:29Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![tj.corona](https://discourse.kitware.com/user_avatar/discourse.kitware.com/tj.corona/32/35_2.png) [@tj.corona](https://discourse.kitware.com/u/tj.corona)
#### Post date: [August 17, 2018, 7:59pm UTC](https://discourse.kitware.com/t/cmb6-access-resource-in-related-operator/143/6 "2018-08-17T19:59:27Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![chart3388](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@chart3388](https://discourse.kitware.com/u/chart3388)
#### Post date: [August 22, 2018, 7:53pm UTC](https://discourse.kitware.com/t/cmb6-access-resource-in-related-operator/143/7 "2018-08-22T19:53:53Z")

</div>

This worked with the .def\_static as you described. Thanks!
