calm down… your code is mostly correct but the four mistakes you made shows that you are still learning python so don’t worry, maybe you should try to learn about classes, inheritance, python stuff and maybe a bit of patience before diving into blender scripting.
First, you should be familiar with the following line:
using UnityEngine;
in blender there’s also an equivalent:
import bpy
bpy is the main blender module, you can think on it as the library that links python to blender and you cant call any bpy.something
without importing bpy first.
Depending on what you are doing you might also want to import other modules like:
import math # math helper functions like sqrt() and round()
import mathutils # blender math stuff like Vector, Matrix, Geometry
import bmesh # mesh editing helper module
import numpy # module for fast parallel math used to vectorize lots of algorithms
import json # python's json dumper and loader
import os # operating system stuff like opening files, navigating through folders
Now that you get what imports are lets move on to the basics of identation:
As you might have guessed, since python don’t use brackets, indentation matters.
class A:
variable_in_class = 5
def my_method():
print("I am inside class A")
class B:
variable_in_class = 5
def my_method():
print("I am not inside class B")
A.my_method() # prints: I am inside class A
B.my_method() # AttributeError: type object "B" has no attribute "my_method"
Basic inheritance
In python you can define a base class and use it to create other classes instead of repeating yourself over and over by writing the same thing
class Animal: # an generic animal class
species = "Animal"
sound = None
eats = None
def what_species(self):
print("I'm a " + self.species)
def talk(self):
print(self.sound)
def eat(self):
print("yum Yumm, i'm eating " + self.eats)
class Monkey(Animal): # Monkey inherits animal methods and vars
species = "Chimp"
sound = "Hu Hhu Hu Ha"
eats = "bananas"
# No need to define methods here since Animal class already have
class Dog(Animal): # same as monkey but its a dog
species = "Dog"
sound = "Woof Woof"
eats = "beef"
# No need to define methods here since Animal class already have
my_monkey = Monkey()
my_monkey.what_species()
my_monkey.talk()
my_monkey.eat()
my_dog = Dog()
my_dog.what_species()
my_dog.talk()
my_dog.eat()
# output:
# I'm a Chimp
# Hu Hhu Hu Ha
# yum Yumm, i'm eating bananas
# I'm a Dog
# Woof Woof
# yum Yumm, i'm eating beef
Now that you get all this, you should be able to spot the difference from your code to the correct code:
import bpy # Never forget to import bpy
# class SelectabilityTogglePanel:
class SelectabilityTogglePanel(bpy.types.Panel): # It iherits from Panel Class
bl_label = "this is a stoopid label"
bl_idname = "MYADDON_PT_this_is_the_panel_id" # Its for internal organization
bl_space_type = 'IMAGE_EDITOR'
bl_region_type = 'UI'
bl_category = 'Name Of My Tab'
# Pay attention to indentation!
def draw(self, context):
ui_layout = self.layout
# ui_layout.prop(context.scene, "my_custom_boolean", toggle=true)
ui_layout.prop(context.scene, "my_custom_boolean", toggle=True) # True is uppercase T
def register():
# You forgot to register your panel
bpy.utils.register_class(SelectabilityTogglePanel)
bpy.types.Scene.my_custom_boolean = bpy.props.BoolProperty()
if __name__ == "__main__":
register()