Abstract作者:mightyang由于特殊原因,我们需要重新整理Clarisse,所以需要整理一下它的pythonapi,方便写脚本。按照惯例,先提供官方文档的基本介绍(翻译)。官方文档Clarisse对象模型介绍本章介绍Clarisse对象模型,是学习ClarisseAPI的关键。Clarisse项目中存储了什么在Clarisse中,项目是不同类型的OfObject或OfClass实例(实例)的集合,存储在一个具有层次关系的OfContext中作为结构。上下文类似于文件系统中的文件夹,它也控制可见性。要了解有关上下文的更多信息,请参阅用户手册。整个项目的入口是通过对象工厂OfObjectFactory来访问的。在Python中,您可以通过调用ix.application.get_factory()获取对象工厂,而在C++中则通过AppObject::get_factory获取。(从这里可以猜到,如果想查看ix.application有哪些属性和方法供Python调用,直接查看文档中的AppObject即可,帮助文档并没有直接提供Python内容)什么是类?项目的类型由项目的OfClass定义。他们还可以决定在Clarisse处理它们时要做什么。类及其属性、可调用对象可以被继承。比如在Clarisse中OfClass|ProjectItem就是一个非常重要的类。当一个类的实例直接或间接继承自OfClass|ProjectItem类时,会自动保存在project工程文件中。请注意,为了避免C++类和OfClass之间的混淆,我们在文档中为所有OfClass引用添加了前缀OfClass。例如OfClassGeometryPolyMesh将显示为OfClass|GeometryPolymesh。例如,OfClass|GeometryPolyfile类定义一个多边形面存储在继承自OfClass|ProjectItem的外部文件中。这句话解释了为什么OfClass|GeometryPolyfile条目保存在project工程文件中。如果您想在Clarisse中查看所有类型的层次结构图,您可以使用ClassExplorer小部件(窗口)来查看它们。只需单击主窗口菜单Window>ClassExplorer...。请注意,ClassExplorer不显示OfClass的实际名称。相反,它们根据真实姓名以更易于理解的方式显示。要显示这些项目的实际类名,只需启用浏览器的类型列。要启用或禁用此列,请在浏览器右侧,右键单击列标题的空白区域并选择它。如果想查看Polyfile(OfClass|GeometryPolyfile)的类型层次图,可以看到如下:SceneItem)0):printix.selection[0].get_module().get_class_info_name()onClarisse在对软件进行了基本的介绍之后,就可以大致摸清Clarisse的整体架构了。下面是我自己的记录说明,这篇文章主要是解决手头项目的记录,所以暂时不全部介绍。PythonAPI介绍Clarisse中Python的接口应该是通过swig生成的。关于swig的内容请自行百度。接口主要是ix模块。ix有两个子模块:apicmds一个子类:ApplicationSelection两个成员:application(gui.ClarisseApp)selection(ix.ApplicationSelection实例)以及一系列的成员函数:add_attribute(obj,attr_name,attr_type,category='General')begin_command_batch(batch_name)check_need_save()create_context(path)create_generic_object(object_name)create_object(item_name,class_name,destination_context=None)delete_item(item)disable_command_history()disable_echo_command()enable_command_history()enable_echo_command()end_command_batch_()export_project(上下文)export_context_as_project_with_dependencies(上下文,文件名)export_geometries(文件名,geometry_objects)export_geometry(文件名,geometry_object)export_render_archive(文件名)get_current_context()get_current_frame()get_item(item_name)import_geometries(文件名)import_geometry(文件名)import_image(文件名)import_images(filenames)import_import(文件名、类名、后缀)import_map_files(文件名、类名、后缀)import_pr对象(文件名)import_scene(文件名)import_volume(文件名)import_volumes(文件名)inspect(item)is_context_exists(context_name)is_gui_application()is_interactive_application()is_process_application()item_exists(item_name)load_project(文件名)log_error(消息)log_info(消息)log_warning(消息)make_absolute_of_path(路径)reference_export_context(上下文,文件名)reference_file(working_context,文件名)reference_files(working_context,文件名)reference_make_local(上下文)render_image(图像)save_bmp(图像,文件名,lut_name='')save_exr16(图像,文件名,lut_name='',compression=1)save_exr32(image,filename,lut_name='',compression=1)save_image(image,filename,format,lut_name='',compression=1)save_jpg(image,filename,lut_name='')save_png16(图像,文件名,lut_name='')save_png8(图像,文件名,lut_name='')save_project(文件名)save_tga(图像,文件名,lut_name='')save_tif16(图像,文件名,lut_name='')save_tif32(图像,文件名,lut_name='')save_tif8(image,filename,lut_name='')set_current_context(context_path)set_current_frame(frame_number)application成员application是gui.ClarisseApp的一个实例,不知道为什么属于gui,根据help显示,是应该是一个综合接口,会简单介绍一下它继承的parent,方便查找文档:我们可以查看一下sdk中相关的ClarisseApp类的内容是没有问题的。selectionmemberselectionmember,顾名思义,应该是和selection有关的。这些方法是:__getitem__(self,index)add(self,item)deselect_all(self)get(self,index)get_contexts(self)get_count(self)get_objects(self)is_empty(self)select(self,item)select_all(self)selection是为Python写的一个成员,C++也有一个AppSelection类,也是用来做selection的,具体可以查看文档。实际使用:代码风格从上面的成员和成员函数的写法可以知道,Clarisse的写法是使用下划线作为单词分隔符,全部小写,可以方便记忆。获取选中内容的ix.selectionix成员,可以通过下标直接获取内容,但不支持直接forin迭代ix.application.get_selection()继承自AppObject的成员函数,遍历返回AppSelection类可能是自动生成swig的原因,Clarisse中所有列表形式的数据都不能像python中那样用forin进行迭代。只能通过列表的下标形式访问,比如遍历选中的内容:ix.selection的访问方法:foriinrange(ix.selection.get_count()):print(ix.selection[i].get_full_name())AppSelection访问方法:ss=ix.applcation.get_selection()items=ss.get_items()#返回一个可以通过下标访问的CoreVector类foriinrange(ss.get_count()):print(items[i].get_full_name())OfContexttype上面提到了Clarisse的整个项目是由OfContext和各种类型的对象(OfClass)组成的,而OfContext在Clarisse的层级中表现为一个文件夹。所以在获取上面选择的内容时,如果选择了某个目录,获取的结果是一个OfContext类型。获取OfContext下的所有geometry在3.6版本中,geometry的类型如下:GeometryAbcMeshalembic引用后创建了一个OfContext,里面的shape就是这个类型的SceneObjectCombiner,相当于把其他geometry分组,然后引用GeometryPolyfileObj。文件引用是该类型的其他内置类型,这里不再介绍。注意,5.0版本的Clarisse发生了很多变化,但是对于写程序来说,就是增加了更集中的类型。下一步就是从选择的内容中选择我们想要的:获取子对象并存储在OfObjectVector中objs=ix.api.OfObjectVector()#创建一个Vector来存储对象,这个和Python有点不同,需要在范围内为i创建和存储(ix.selection.get_count()):ix.application.get_match_object()以过滤ObjectVector