Isotropix Forums

How to add Alembic file (context) to a ref list?

Clarisse Scripting related topics

How to add Alembic file (context) to a ref list?

Unread postby atnreg » Tue Jun 05, 2018 3:43 pm

Hi!

I would like to do some processing with selected Alembic files and I would like to get the Alembic contexts (as the files themselves are not visible in Clarisse) to a ScriptedClass that has Reference List attribute.

Let's say I have:
item=ix.get_item("project://scene/mycontext")

How can I:
1. detect if it is actually Alembic context? (both normal and Alembic context gives get_class_info().get_name() -> OfContext)
2. add the Context to the Reference List attribute? It does not work by add_values. For other items (geometry, texture) it works but Context tries to create the context in current context instead of adding it to the list :o

This might be very easy but I cannot figure it out, please help :)

Thank you!

Antti
Antti
AMD Ryzen Threadripper 2990wx (32c/64t),64GB RAM,NVIDIA GTX 1080ti,Win10
Clarisse 4.0SP5b,Houdini,Blender2.80,Fusion360,Onyx,ZBrush,SubstPnt...
Started: Clarisse 2016/10 (Py 2017/01), Python 2016/11
No business, just fun :)
atnreg
 
Posts: 533
Joined: Mon Sep 19, 2016 4:20 pm
Location: Helsinki, Finland

Re: How to add Alembic file (context) to a ref list?

Unread postby bvaldes » Wed Jun 06, 2018 9:12 am

Hi,

What do you call alembic context? A context can only be reference or not, there no several type of context.
When you import an alembic file, a context is created with the name of the file and then the content of the alembic file is created in it. It's just a matter of hierarchy, if you want to get the content of the context out of it and delete the context there is no problem with that (except if it's a reference context).

This is not possible to add a context to a reference list. If you do it inside of Clarisse "by hand" (with a combiner for example), you can see in the log of Clarisse that this is the content of the context that's added to the reference attribute.
By script, what you have to do is get the content of a context and add this list to the attribute you want.
The following function allow you to get all the items inside of a context (I wrote it for specific things, do not hesitate to simplify it for your use).

python code

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

When you have the content of the context, you just have to add it like you used to with the add_values function.

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

Re: How to add Alembic file (context) to a ref list?

Unread postby atnreg » Wed Jun 06, 2018 2:55 pm

Hi!
Thank you very much, that makes perfect sense and thank you for the sample code :)

The only thing I'm still wondering is that if the Alembic context is like normal context, how Clarisse can still show the attribute editor with filename for it? But it is still OfContext :o
This is more like curiosity question but if it would be possible to detect that a context is actually Alembic, it would be useful :)

Antti
Antti
AMD Ryzen Threadripper 2990wx (32c/64t),64GB RAM,NVIDIA GTX 1080ti,Win10
Clarisse 4.0SP5b,Houdini,Blender2.80,Fusion360,Onyx,ZBrush,SubstPnt...
Started: Clarisse 2016/10 (Py 2017/01), Python 2016/11
No business, just fun :)
atnreg
 
Posts: 533
Joined: Mon Sep 19, 2016 4:20 pm
Location: Helsinki, Finland

Re: How to add Alembic file (context) to a ref list?

Unread postby bvaldes » Thu Jun 07, 2018 8:49 am

Hi,

If you try to IMPORT an ABC you can see that on the context, there is no filename attribute. The filename attribute is only for REFERENCE context (project or abc it doesn't matter). The filenames of the abc file are in the item themselves.

As i said there is no alembic context so obviously you can't detect that. Try to explain me what you try to achieve, I could maybe help you to find a way.

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

Re: How to add Alembic file (context) to a ref list?

Unread postby atnreg » Thu Jun 07, 2018 9:06 am

Ah of course, sorry, yes I use references for Alembic :)

What I am trying to do is tool for importing The Grove (Blender comm. addon) trees to Clarisse. The idea was that by detecting it is an Alembic I could get it's filename and use it to automatically name the items my script will create (materials and scatterer etc.).
But I just realized that in Alembic the all items are class *Abc* right? That way I can separate normal context and Alembic one :)

Anyway, I just decided to change my tool idea completely, I was planning to create the needed items by selecting a context and then pressing toolbar button. But of course it is better to make the whole process through the button i.e. also do the reference/import because in that case I know it is Alembic :)

Thank you for opening my eyes for the obvious :D

But is it possible to get the filename of the referenced context? It gives OfContext as class so I can only use is_reference() to detect if it is referenced or not but there is no method to get the referenced filename. This could also be useful to know or to know if it is not possible in Python :)

Antti
Antti
AMD Ryzen Threadripper 2990wx (32c/64t),64GB RAM,NVIDIA GTX 1080ti,Win10
Clarisse 4.0SP5b,Houdini,Blender2.80,Fusion360,Onyx,ZBrush,SubstPnt...
Started: Clarisse 2016/10 (Py 2017/01), Python 2016/11
No business, just fun :)
atnreg
 
Posts: 533
Joined: Mon Sep 19, 2016 4:20 pm
Location: Helsinki, Finland

Re: How to add Alembic file (context) to a ref list?

Unread postby bvaldes » Thu Jun 07, 2018 2:44 pm

Hi again,

I think you are over complicating things, you can simply check that the filename attribute exists on a context and then theck the extension, if abc you can do you stuff.

python code

ctx = ix.selection[0]
attr = ctx.attribute_exists("filename")
if attr and attr.get_string().endswith(".abc"):
print attr.get_string()
# you can do you stuff here

So the best thing to do is to search for all the context of your project, check if their are reference (if they have a filename) and check if the filename is an ABC file. Then you do you stuff when you get this list.

Small gift : a way to find all the context of a project:

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


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

Re: How to add Alembic file (context) to a ref list?

Unread postby atnreg » Thu Jun 07, 2018 3:32 pm

Ah it's so simple when you know the methods to use ;)
But yes you are right, I was over complicating the whole thing, now I already have button that asks for the Alembics and processes them :)

Thank you very much again, these code snippets save a lot of time!

Antti
Antti
AMD Ryzen Threadripper 2990wx (32c/64t),64GB RAM,NVIDIA GTX 1080ti,Win10
Clarisse 4.0SP5b,Houdini,Blender2.80,Fusion360,Onyx,ZBrush,SubstPnt...
Started: Clarisse 2016/10 (Py 2017/01), Python 2016/11
No business, just fun :)
atnreg
 
Posts: 533
Joined: Mon Sep 19, 2016 4:20 pm
Location: Helsinki, Finland


Return to Scripting