Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Class help
#1
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?
Reply
#2
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
Reply
#3
You need __init__ (double underscores), not _init_. The single underscores around init result in the method not being called when our create an instance.
Reply
#4
Are you running this Python script inside Blender? Would you mind sharing which tutorial is this?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting c++ class to python class panoss 12 11,934 Jul-23-2017, 01:16 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020