Isotropix Forums

Get matching contexts and a relative filter

Clarisse Scripting related topics

Get matching contexts and a relative filter

Unread postby alexey » Fri Aug 14, 2020 7:56 pm

Hello,

I am a bit confused about how the get_matching_contexts method works. The documentation says that the base_context argument is used if the filter argument is relative. The similar matching methods are clarifying that the base context is "context to start looking from if a filter is relative (starts with ./ )".
I've done some tests, and I see that the relative filter doesn't work, or I've missed something important. I need to find specific nested contexts in a parent context. Please run the code below and check outputs for the relative filter tests. All outcomes from the relative filter tests are empty.

Code: Select all
import ix

def get_matching_contexts(context, expression):
    if isinstance(context, str):
        if context and ix.item_exists(context):
            context_item = ix.get_item(context)
        else:
            ix.application.log_error("The given context path doesn't exist")
    elif isinstance(context, ix.api.OfContext):
        context_item = context
    else:
        ix.application.log_error("The first argument should be either a string or an ix.api.OfContext")
        return

    ctx_set = ix.api.OfContextSet()
    ix.application.get_matching_contexts(ctx_set, expression, context_item)
    ctx_array = ctx_set.get_items()

    result = []
    for item in range(ctx_array.get_count()):
        result.append(ctx_array[item])
    return result

#Run
root_ctx = ix.application.get_factory().get_root().get_full_name()
ix.set_current_context(root_ctx)
lgt_ctx = ix.cmds.CreateContext("lights", "project://").get_full_name()

for i in range(1,10):
    lgt_grp_ctx = ix.cmds.CreateContext("lgt_grp_{}".format(i), lgt_ctx)
    search_ctx = ix.cmds.CreateContext("search_context".format(i), lgt_grp_ctx)

ix.set_current_context(lgt_ctx)
ix.application.copy(lgt_ctx)
ix.set_current_context(root_ctx)
for i in range (1,10):
    ix.application.paste()
#
print "-------------------"
# relative test 1
parent_ctx = "{}/lgt_grp_2".format(lgt_ctx)
fltr = "./search_ctx"
print "parent context:",parent_ctx
print "filter:", fltr
if ix.item_exists(parent_ctx):
    print "Run the realtive test1:"
    for i in get_matching_contexts(parent_ctx, fltr):
        print i
else:
    ix.application.log_error("The parent path doesn't exist")

# Relative test2
parent_ctx = lgt_ctx
fltr = "./*"
print "\nparent context:",parent_ctx
print "filter:", fltr
if ix.item_exists(parent_ctx):
    print "Run the realtive test2:"
    for i in get_matching_contexts(parent_ctx, fltr):
        print i
else:
    ix.application.log_error("The parent path doesn't exist")

#Absolute test
parent_ctx = parent_ctx = "{}/lgt_grp_2".format(lgt_ctx)
fltr = "*search_context"
print "\nparent context:",parent_ctx
print "filter:", fltr
if ix.item_exists(parent_ctx):
    print "Run the absolute test:"
    for i in get_matching_contexts(parent_ctx, fltr):
        print i
else:
    ix.application.log_error("The parent path doesn't exist")

alexey
 
Posts: 6
Joined: Fri Jun 05, 2020 1:11 am

Re: Get matching contexts and a relative filter

Unread postby anemoff » Mon Aug 17, 2020 11:01 am

Hi,

Are you using 3.6? The script doesn't run in 4.0.
Please always specify wich version you are using so that we can investigate and reproduce in the correct environment.

We'll take a look at the API and keep you posted.

Cheers,
Anthony Nemoff
Isotropix
R&D Engineer
User avatar
anemoff
 
Posts: 330
Joined: Wed Jan 13, 2016 10:10 am

Re: Get matching contexts and a relative filter

Unread postby alexey » Mon Aug 17, 2020 2:16 pm

Hi Anthony,
Thank you for looking into this. Yes, I use 3.6. Anyways, I've made this code running for 4.0 as well. Still can't get the proper results for the relative filter.

python code

import ix

def get_matching_contexts(context, expression):
if isinstance(context, str):
if context and ix.item_exists(context):
context_item = ix.get_item(context)
else:
ix.application.log_error("The given context path doesn't exist")
elif isinstance(context, ix.api.OfContext):
context_item = context
else:
ix.application.log_error("The first argument should be either a string or an ix.api.OfContext")
return

ctx_set = ix.api.OfContextSet()
ix.application.get_matching_contexts(ctx_set, expression, context_item)
ctx_array = ctx_set.get_items()

result = []
for item in range(ctx_array.get_count()):
result.append(ctx_array[item])
return result

#Run
root_ctx = ix.application.get_factory().get_root().get_full_name()
ix.set_current_context(root_ctx)
lgt_ctx = ix.cmds.CreateContext("lights", "project://").get_full_name()

for i in range(1,10):
lgt_grp_ctx = ix.cmds.CreateContext("lgt_grp_{}".format(i), lgt_ctx)
search_ctx = ix.cmds.CreateContext("search_context".format(i), lgt_grp_ctx)

ix.set_current_context(lgt_ctx)
ix.application.copy()
ix.set_current_context(root_ctx)
for i in range (1,10):
ix.application.paste()
#
print "-------------------"
# relative test 1
parent_ctx = "{}/lgt_grp_2".format(lgt_ctx)
fltr = "./search_ctx"
print "parent context:",parent_ctx
print "filter:", fltr
if ix.item_exists(parent_ctx):
print "Run the realtive test1:"
for i in get_matching_contexts(parent_ctx, fltr):
print i
else:
ix.application.log_error("The parent path doesn't exist")

# Relative test2
parent_ctx = lgt_ctx
fltr = "./*"
print "\nparent context:",parent_ctx
print "filter:", fltr
if ix.item_exists(parent_ctx):
print "Run the realtive test2:"
for i in get_matching_contexts(parent_ctx, fltr):
print i
else:
ix.application.log_error("The parent path doesn't exist")

#Absolute test
parent_ctx = parent_ctx = "{}/lgt_grp_2".format(lgt_ctx)
fltr = "*search_context"
print "\nparent context:",parent_ctx
print "filter:", fltr
if ix.item_exists(parent_ctx):
print "Run the absolute test:"
for i in get_matching_contexts(parent_ctx, fltr):
print i
else:
ix.application.log_error("The parent path doesn't exist")
alexey
 
Posts: 6
Joined: Fri Jun 05, 2020 1:11 am

Re: Get matching contexts and a relative filter

Unread postby anemoff » Mon Aug 17, 2020 3:54 pm

Thanks for confirming it's on 3.6.
I checked, and this function hasn't changed in 4.0 and behaves the same.

By the way, there are a few issues in your script:
- the copy/paste in your script isn't working but it doesn't affect the lack of results for tests 1 and 2.
- the first test is searching for "./search_ctx" instead of "./search_context", I suppose it was a typo

In any case the API documentation isn't very detailed and lacks examples.

I suggest you create a bug report: https://www.isotropix.com/user/bugtracker
Anthony Nemoff
Isotropix
R&D Engineer
User avatar
anemoff
 
Posts: 330
Joined: Wed Jan 13, 2016 10:10 am

Re: Get matching contexts and a relative filter

Unread postby alexey » Mon Aug 17, 2020 4:58 pm

Thank you, Anthony!
I've created a ticket. Yes, you right about the copy/paste block and the typo. I simplified the code for the ticket as well.
alexey
 
Posts: 6
Joined: Fri Jun 05, 2020 1:11 am

Re: Get matching contexts and a relative filter

Unread postby anemoff » Mon Aug 17, 2020 5:04 pm

There's indeed a bug with relative rules. So you can definitely go ahead a report a bug.

A workaround is to always add the full path of the parent context in the filter expression when you want to do a relative search.

For example for test 1 (this one in particular looks redundant because it's equivalent provide the full path of the searched context):

python code

parent_ctx = "{}/lgt_grp_2".format(lgt_ctx)
#fltr = "./search_context" # this should work but doesn't
fltr = "{}/search_context".format(parent_ctx)


And for test 2:

python code

parent_ctx = lgt_ctx
#fltr = "./*" # this should work but doesn't
fltr = "{}/*".format(parent_ctx )


This might not be very convenient in some cases, but SeExpr can help (available in 4.0).
For example here: viewtopic.php?p=19810#p19810
Anthony Nemoff
Isotropix
R&D Engineer
User avatar
anemoff
 
Posts: 330
Joined: Wed Jan 13, 2016 10:10 am

Re: Get matching contexts and a relative filter

Unread postby alexey » Tue Aug 18, 2020 1:33 am

Thank you, Anthony! It definitely might help me.
alexey
 
Posts: 6
Joined: Fri Jun 05, 2020 1:11 am


Return to Scripting