Aug-07-2023, 11:43 PM
(This post was last modified: Aug-08-2023, 02:05 AM by Larz60+.
Edit Reason: added error tag
)
I'm trying to learn how to make Python classes by following along with a tutorial video. One of the examples goes like this:
import bpy class SquareFaceCube: """ a class to represent a cube mesh object with square faces """ def _init_(self): self.verts=[ (-1.0,-1.0,-1.0), (-1.0,1.0,-1.0), (1.0,1.0,-1.0), (1.0,-1.0,-1.0), (-1.0,-1.0,1.0), (-1.0,1.0,1.0), (1.0,1.0,1.0), (1.0,-1.0,1.0), ] self.faces=[ (0,1,2,3), (0,4,5,1), (1,5,6,2), (2,6,7,3), (0,3,7,4), (4,7,6,5), ] self.mesh_data=None self.mesh_object=None def create_mesh_data(self): """ create mesh data from verts and faces data """ self.mesh_data=bpy.data.meshes.new("cube_data") self.mesh_data.from_pydata(self.verts,[],self.faces) def create_mesh_object_from_data(self): """ create object from mesh data """ self.mesh_object=bpy.data.objects.new("cube_object",self.mesh_data) def add_mesh_object_to_scene(self): """ add object into active scene by linking the object to the default scene collection """ bpy.context.collection.objects.link(self.mesh_object) def create_mesh_object(self): """ create mesh object from data and add new object into the scene """ self.create_mesh_data() self.create_mesh_object_from_data() self.add_mesh_object_to_scene() def add_into_scene(self, location): if self.mesh_object==None: self.create_mesh_object() self.mesh_object.location=location else: print("Warning: can't add mesh object because it is already added to the scene") def main(): square_face_cube=SquareFaceCube() square_face_cube.add_into_scene(location=(0,0,0)) if __name__=="__main__": main()I copied the whole script to try and do it my self but for some reason I keep coming out with this error message.
Error:Python: Traceback (most recent call last):
File "C:\Users\jacob\OneDrive\Documents\Blender\Blender Custom UI\Blender Custom UI.blend\Class ex 1", line 72, in <module>
File "C:\Users\jacob\OneDrive\Documents\Blender\Blender Custom UI\Blender Custom UI.blend\Class ex 1", line 69, in main
File "C:\Users\jacob\OneDrive\Documents\Blender\Blender Custom UI\Blender Custom UI.blend\Class ex 1", line 61, in add_into_scene
AttributeError: 'SquareFaceCube' object has no attribute 'mesh_object'
One of the first variables in self.mesh_object. Can someone please tell me what I am doing wrong with this code?