Isotropix Forums

Auto Material Create/Assign

Find useful scripts shared by the community here

Auto Material Create/Assign

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


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:
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"):

if len(items) > 0:
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)
create_material(item, item_name, geo, material_class, i)


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')

# 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)

Support Team
User avatar
Posts: 1024
Joined: Thu Jan 31, 2013 11:10 am

Re: Auto Material Create/Assign

Unread postby paq » Fri Mar 13, 2015 9: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
Posts: 375
Joined: Fri Jan 25, 2013 11:33 pm
Location: Montréal

Re: Auto Material Create/Assign

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


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.
Support Team
User avatar
Posts: 1024
Joined: Thu Jan 31, 2013 11:10 am

Return to Useful Community Scripts