Isotropix Forums

get list of all contexts in the scene

Clarisse Scripting related topics

get list of all contexts in the scene

Unread postby 1111 » Sun Apr 29, 2018 5:09 am

how to get list all contexts that exist in the scene with python?
regards..
1111
 
Posts: 1
Joined: Sun Dec 24, 2017 9:42 pm

Re: get list of all contexts in the scene

Unread postby bvaldes » Thu May 03, 2018 4:15 pm

Hi,

Sorry for the late reply. Take a look to this code snippet:

python code

def getSubContext(context = ix.get_current_context(), recursive = True):
"""
This function return a list of all context contained in the context given as arguement.
If the 'context' argument is empty, the start point is the current context in Clarisse

@arg context: The start point of the search. If you want for the whole project give 'project:/' as argument
@arg recusive: Set it to True if you want the looking for in the sub-context of sub-context. If you only want the first children, set to False

@return: The list of all the context found.

Example:
getSubContext() => will return all the context in current context. Also included the sub-context
getSubContext('project:/') => will return all the context in the whole project
getSubContext('project:/', False) => will return the first level of context contained in 'project:/'
"""
result = []
context = ix.get_item(str(context))
sub_contexts = []
for i in range(context.get_context_count()):
result.append(context.get_context(i))
sub_contexts.append(context.get_context(i))
if recursive:
while len(sub_contexts)>0:
workOn = sub_contexts
sub_contexts = []
for context in workOn:
for i in range(context.get_context_count()):
sub_contexts.append(context.get_context(i))
result.append(context.get_context(i))
return result


If you need help, feel free to ask. Regards,
Benoit VALDES
Isotropix
Clarisse QA
User avatar
bvaldes
 
Posts: 315
Joined: Mon Sep 26, 2016 10:44 am

Re: get list of all contexts in the scene

Unread postby jeroendesmet » Wed Jan 30, 2019 7:10 pm

hi!

so I've tried to use this getSubContext function, which seems to work fine on first sight...

I want to check if a specific context already exists, and if it doesn't, create that context.
And if it does exist, don't create it.
This is to avoid having Clarisse create a new context with a different name (like context, context2, etc...)

Now when I try to use an IF statement to go through the getSubContext-list and see if an item matches one from that list... it doesn't work.

Code: Select all
contexts = getSubContext("project://ASSETS")

######## THIS WORKS #########
for c in contexts:
    print c

####### THIS DOESN'T ########
testList = ['project://ASSETS/context1', 'project://ASSETS/context2']

for t in testList:
    if t in contexts:
        print ("context exists >> " + t)
    else:
        print ("context doesn\'t exist >> " + t)



So in my scene, I've created a context 'ASSETS', and inside that one I created 2 contexts: 'context1' and 'context2'.
So:
project://ASSETS/context1
project://ASSETS/context2

With this setup, I'm running the script above... which should print a "context exists", since it's in the testList... however it doesn't.


Any idea? What am I missing here?

Thx!!
J.
jeroendesmet
 
Posts: 9
Joined: Sat Nov 15, 2014 9:08 pm

Re: get list of all contexts in the scene

Unread postby ayanik » Wed Jan 30, 2019 8:00 pm

Hoi Jeroen,

Looks like you are comparing Strings to OfContext objects. I have two scripts in my toolkit similar to script posted above, one for recursive contexts and one for items. Below I have modified my script to suit your needs. I can't test it right now since I am away from my computer and on my mobile phone.

Code: Select all
def get_sub_contexts(ctx, search="", max_depth=0, current_depth=0):
    """Gets all subcontexts."""
    current_depth += 1
    results = []
    for i in range(ctx.get_context_count()):
        sub_context = ctx.get_context(i)
        results.append(sub_context)
        # 0 is infinite
        if current_depth <= max_depth or max_depth == 0:
            for result in get_sub_contexts(sub_context, name, max_depth, current_depth):
                if result not in results:
                    results.append(result)
    if search:
        for sub_ctx in results:
            if str(sub_ctx) == search:
                return sub_ctx
        return []
    return results


def get_items(ctx, kind=(), max_depth=0, current_depth=0, return_first_hit=False):
    """Gets all items recursively."""
    result = []
    items = ix.api.OfItemVector()
    sub_ctxs = get_sub_contexts(ctx, max_depth=max_depth, current_depth=current_depth)
    sub_ctxs.insert(0, ctx)
    for sub_ctx in sub_ctxs:
        if sub_ctx.get_object_count():
            objects_array = ix.api.OfObjectArray(sub_ctx.get_object_count())
            flags = ix.api.CoreBitFieldHelper()
            sub_ctx.get_all_objects(objects_array, flags, False)
            for i_obj in range(sub_ctx.get_object_count()):
                if kind:
                    for k in kind:
                        if objects_array[i_obj].is_kindof(k):
                            if return_first_hit:
                                return objects_array[i_obj]
                            items.add(objects_array[i_obj])
                else:
                    items.add(objects_array[i_obj])
    for item in items:
        result.append(item)
    return result
ayanik
 
Posts: 41
Joined: Wed Nov 26, 2014 9:10 pm

Re: get list of all contexts in the scene

Unread postby jeroendesmet » Wed Jan 30, 2019 8:15 pm

hi!

thanks for the reply. I'll have a look later today.

Seems like I can also fix it by creating a new list and appending each item (or OfContext-object, whatever that is) to this list as a string:


Code: Select all
contexts=[]
for c in getSubContext("project://ASSETS"):
    contexts.append(str(c))
print contexts


printing 'contexts' will give me a proper list. Yay!
jeroendesmet
 
Posts: 9
Joined: Sat Nov 15, 2014 9:08 pm

Re: get list of all contexts in the scene

Unread postby ayanik » Wed Jan 30, 2019 11:10 pm

Yeah you can just cast them to strings and put them in another list. That would work also.
ayanik
 
Posts: 41
Joined: Wed Nov 26, 2014 9:10 pm

Re: get list of all contexts in the scene

Unread postby bvaldes » Thu Jan 31, 2019 10:02 am

Hi,

I did some useful functions a few years earlier. You can use them if you want:

python code

def getSubContext(context = ix.get_current_context(), recursive = True):
result = []
context = ix.get_item(str(context))
sub_contexts = []
for i in range(context.get_context_count()):
result.append(context.get_context(i))
sub_contexts.append(context.get_context(i))
if recursive:
while len(sub_contexts)>0:
workOn = sub_contexts
sub_contexts = []
for context in workOn:
for i in range(context.get_context_count()):
sub_contexts.append(context.get_context(i))
result.append(context.get_context(i))
return result

def getItems(context = ix.get_current_context(), kind = None, recursive=True):
result = []
workOn = [ix.get_item(str(context))]
vItems = ix.api.OfItemVector()
if recursive:
for subContext in getSubContext(workOn[0], recursive = True):
workOn.append(subContext)

for context in workOn:
if (context.get_object_count()):
objects_array = ix.api.OfObjectArray(context.get_object_count())
context.get_objects(objects_array)
for i_obj in range(context.get_object_count()):
if kind is not None:
if objects_array[i_obj].is_kindof(kind):
vItems.add(objects_array[i_obj])
else:
vItems.add(objects_array[i_obj])

for item in vItems:
result.append(item.get_full_name())
return result


I hope this will help you.

Regards
Benoit VALDES
Isotropix
Clarisse QA
User avatar
bvaldes
 
Posts: 315
Joined: Mon Sep 26, 2016 10:44 am


Return to Scripting