@@ -27,39 +27,88 @@ class MeshObject(Entity):
2727 Every instance of this class is a mesh which can be rendered in the scene. It can have multiple materials and
2828 different configurations of vertices with faces and edges.
2929 """
30+ def materials (self ) -> int :
31+ """ Returns the number of material slots of the object.
3032
31- def get_materials (self ) -> List [Optional [Material ]]:
32- """ Returns the materials used by the mesh.
33+ :return: The number of material slots.
34+ """
35+ return len (self .blender_obj .material_slots )
36+
37+ def get_material_slot_link (self , index : int ) -> str :
38+ """ Returns whether object's or object's data material is used in the material slot.
39+
40+ :return: "DATA" if the material slot is linked to data material or "OBJECT" otherwise.
41+ """
42+ return self .blender_obj .material_slots [index ].link
43+
44+ def set_material_slot_link (self , index : int , link : str ):
45+ """ Sets whether object's or object's data material is used in the material slot.
46+ Available: ["DATA", "OBJECT"]. Type: str
47+ """
48+ self .blender_obj .material_slots [index ].link = link
49+
50+ def get_material (self , index : int , link = "VISIBLE" ) -> Material :
51+ """ Returns the material used by the object.
52+
53+ :link: The mode specifying whether to get material linked to the object or object's data.
54+ Available: ["DATA", "OBJECT", "VISIBLE"]. Type: str
55+ :return: A list of materials.
56+ """
57+ link = link .upper ()
58+ if link == "VISIBLE" and self .get_material_slot_link (index ) == "DATA" :
59+ link = "DATA"
60+ link2get = "OBJECT" if link == "VISIBLE" else link
61+
62+ link2return = self .get_material_slot_link (index )
63+ self .set_material_slot_link (index , link2get )
64+ material = self .blender_obj .material_slots [index ].material
65+ self .set_material_slot_link (index , link2return )
66+
67+ # If there is no material in the `OBJECT` slot then the 'DATA' material is displayed.
68+ if material is None and link == "VISIBLE" :
69+ return self .get_material (index , "DATA" )
70+ else :
71+ return MaterialLoaderUtility .convert_to_material (material )
72+
73+ def get_materials (self , link = "VISIBLE" ) -> List [Optional [Material ]]:
74+ """ Returns the materials used by the object.
3375
76+ :link: The mode specifying whether to get materials linked to the object or object's data.
77+ Available: ["DATA", "OBJECT", "VISIBLE"]. Type: str
3478 :return: A list of materials.
3579 """
36- return MaterialLoaderUtility . convert_to_materials ( self .blender_obj . data . materials )
80+ return [ self . get_material ( index , link ) for index in range ( self .materials ())]
3781
3882 def has_materials (self ) -> bool :
3983 """
4084 Returns True if the object has material slots. This does not necessarily mean any `Material` is assigned to it.
4185
4286 :return: True if the object has material slots.
4387 """
44- return len ( self .blender_obj . data . materials ) > 0
88+ return self .materials ( ) > 0
4589
46- def set_material (self , index : int , material : Material ):
47- """ Sets the given material at the given index of the objects material list.
90+ def set_material (self , index : int , material : Material , link = "DATA" ):
91+ """ Sets the given material at the given index of the object's material list.
4892
4993 :param index: The index to set the material to.
5094 :param material: The material to set.
95+ :link: The mode specifying whether to link material to the object or object's data.
96+ Available: ["DATA", "OBJECT"]. Type: str
5197 """
52- self .blender_obj .data .materials [index ] = material .blender_obj
98+ keep_link = self .get_material_slot_link (index )
99+ self .set_material_slot_link (index , link )
100+ self .blender_obj .material_slots [index ].material = None if material is None else material .blender_obj
101+ self .set_material_slot_link (index , keep_link )
53102
54103 def add_material (self , material : Material ):
55- """ Adds a new material to the object.
104+ """ Adds a new material to the object's data .
56105
57106 :param material: The material to add.
58107 """
59108 self .blender_obj .data .materials .append (material .blender_obj )
60109
61110 def new_material (self , name : str ) -> Material :
62- """ Creates a new material and adds it to the object.
111+ """ Creates a new material and adds it to the object's data .
63112
64113 :param name: The name of the new material.
65114 """
@@ -68,11 +117,11 @@ def new_material(self, name: str) -> Material:
68117 return new_mat
69118
70119 def clear_materials (self ):
71- """ Removes all materials from the object. """
120+ """ Removes all materials from the object's data . """
72121 self .blender_obj .data .materials .clear ()
73122
74123 def replace_materials (self , material : bpy .types .Material ):
75- """ Replaces all materials of the object with the given new material.
124+ """ Replaces all materials of the object's data with the given new material.
76125
77126 :param material: A material that should exclusively be used as new material for the object.
78127 """
0 commit comments