@@ -27,39 +27,88 @@ class MeshObject(Entity):
27
27
Every instance of this class is a mesh which can be rendered in the scene. It can have multiple materials and
28
28
different configurations of vertices with faces and edges.
29
29
"""
30
+ def materials (self ) -> int :
31
+ """ Returns the number of material slots of the object.
30
32
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.
33
75
76
+ :link: The mode specifying whether to get materials linked to the object or object's data.
77
+ Available: ["DATA", "OBJECT", "VISIBLE"]. Type: str
34
78
:return: A list of materials.
35
79
"""
36
- return MaterialLoaderUtility . convert_to_materials ( self .blender_obj . data . materials )
80
+ return [ self . get_material ( index , link ) for index in range ( self .materials ())]
37
81
38
82
def has_materials (self ) -> bool :
39
83
"""
40
84
Returns True if the object has material slots. This does not necessarily mean any `Material` is assigned to it.
41
85
42
86
:return: True if the object has material slots.
43
87
"""
44
- return len ( self .blender_obj . data . materials ) > 0
88
+ return self .materials ( ) > 0
45
89
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.
48
92
49
93
:param index: The index to set the material to.
50
94
: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
51
97
"""
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 )
53
102
54
103
def add_material (self , material : Material ):
55
- """ Adds a new material to the object.
104
+ """ Adds a new material to the object's data .
56
105
57
106
:param material: The material to add.
58
107
"""
59
108
self .blender_obj .data .materials .append (material .blender_obj )
60
109
61
110
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 .
63
112
64
113
:param name: The name of the new material.
65
114
"""
@@ -68,11 +117,11 @@ def new_material(self, name: str) -> Material:
68
117
return new_mat
69
118
70
119
def clear_materials (self ):
71
- """ Removes all materials from the object. """
120
+ """ Removes all materials from the object's data . """
72
121
self .blender_obj .data .materials .clear ()
73
122
74
123
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.
76
125
77
126
:param material: A material that should exclusively be used as new material for the object.
78
127
"""
0 commit comments