Python Forum
Python Class help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python Class help (/thread-40503.html)



Python Class help - the_jl_zone - Aug-07-2023

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?


RE: Python Class help - Larz60+ - Aug-08-2023

the_jl_zone Wrote:One of the first variables in self.mesh_object. Can someone please tell me what I am doing wrong with this code?

"==" i think you mean "=" common mistake when first learning python


RE: Python Class help - deanhystad - Aug-08-2023

You need __init__ (double underscores), not _init_. The single underscores around init result in the method not being called when our create an instance.


RE: Python Class help - carecavoador - Aug-08-2023

Are you running this Python script inside Blender? Would you mind sharing which tutorial is this?