Isotropix Forums

Auto Material Create/Assign

Clarisse Scripting related topics

Auto Material Create/Assign

Unread postby support_team » Thu May 22, 2014 7:00 pm

Hi,

This script creates automatically material of the specified class and attached them the selected geometry shading groups.
When the option Skipped Assigned Materials is checked, the script skips shading groups that already have materials different from project://default/material assigned to them.

This script supports multi-selection.


python code

def list_materials():
materials = []
mats = ix.application.get_factory().get_classes().get_classes("Material")
for i in range(mats.get_count()):
if mats[i].is_abstract() == False:
materials.append(mats[i])
return materials


def create_material(item, item_name, geo, material_class, sg_idx):
name = item.get_name() + "_" + geo.get_shading_group(sg_idx) + "_mat"
material = ix.cmds.CreateObject(name, material_class, 'Global', ix.application.get_current_context().get_full_name())
ix.cmds.SetValues([item_name + '[' + str(sg_idx) + ']'], [material])

def create_materials(material_class, skip):
items = []
default_mat = ix.get_item("project://default/material")
for item in ix.selection:
if item.is_kindof("Geometry"):
items.append(item)

if len(items) > 0:
ix.enable_command_history()
ix.begin_command_batch('CreateAndAssignMaterials(\"' + material_class + '\")')
for item in items:
geo = item.get_module()
item_name = item.get_full_name() + ".materials"
for i in range(geo.get_shading_group_count()):
if skip:
if geo.get_material(i) == 0 or geo.get_material(i).get_object() == default_mat:
create_material(item, item_name, geo, material_class, i)
else:
continue
else:
create_material(item, item_name, geo, material_class, i)

ix.end_command_batch()
ix.disable_command_history()

def display_script_interface():
materials = list_materials()
default_ctx = 'project://default'
ctx = ix.get_item(default_ctx)
name = '__ix_material_assign__'
fullname = default_ctx + '/' + name
item = ix.item_exists(fullname)
if item == None:
item = ix.create_object(name, 'ProjectItem', ctx)
attr = ix.add_attribute(item, 'material_class', 'long')
for i in range(len(materials)):
c = materials[i]
title = ix.api.CoreString(c.get_name()).get_title()
title = title.replace('Material', '')
if title[0] == ' ': title = title[1:]
attr.add_preset(title, str(i))

attr = ix.add_attribute(item, 'skipped_assigned_materials', 'bool')
attr.set_bool(True)

# pop up the object in an attribute editor
if ix.application.inspect(item, ix.api.AppDialog.cancel(), ix.api.AppDialog.STYLE_OK_CANCEL).is_ok():
mat_class = materials[item.attrs.material_class.attr.get_long()].get_name()
skip = item.attrs.skipped_assigned_materials[0]
create_materials(mat_class, skip)

display_script_interface()
Isotropix
Support Team
User avatar
support_team
 
Posts: 983
Joined: Thu Jan 31, 2013 12:10 pm

Re: Auto Material Create/Assign

Unread postby paq » Fri Mar 13, 2015 10:47 pm

Thanks very much !
Please forgive me for all the dumb questions lately, but is there any reason I can't use this script on imported reference .abc file ? (it works fine if I import my .abc as scene)
User avatar
paq
 
Posts: 301
Joined: Sat Jan 26, 2013 12:33 am
Location: Montréal

Re: Auto Material Create/Assign

Unread postby support_team » Fri Apr 03, 2015 9:35 am

Hi,

You can't as the script tries to create the material in the referenced context (which is not allowed). The script should be modified to specify the destination context on which materials would be created.
Isotropix
Support Team
User avatar
support_team
 
Posts: 983
Joined: Thu Jan 31, 2013 12:10 pm


Return to Scripting