Using Python classes in Blender

Поделиться
HTML-код
  • Опубликовано: 21 окт 2024

Комментарии • 25

  •  Год назад +7

    Very complete introduction. Thank you!

  • @samdavepollard
    @samdavepollard Год назад +4

    superb video
    i really appreciate the way you share your code
    said it before, i'll say it again - bonkers that you don't have more views and subs; blender is huge, python is massive - where the heck are all the coders???
    you should write a book (seriously)

    • @CGPython
      @CGPython  Год назад +1

      Thanks for your kind words! ❤️
      It is excellent that you get value from these tutorials.
      It's a marathon, not a sprint. 🏃
      We will get there eventually. 🏁💪😊

  • @stefanguiton
    @stefanguiton 2 года назад +2

    Great in-depth video!

    • @CGPython
      @CGPython  2 года назад

      Thanks Stefan! 😃

  • @powerday4678
    @powerday4678 Год назад +1

    thank you..your wonderful guide...

    • @CGPython
      @CGPython  Год назад +1

      I'm pleased to hear that👏
      Thank you

  • @yossy3Dcraft
    @yossy3Dcraft Год назад +1

    I am glad to meet you
    I watched the tutorial video and tried the With functions example 1 with VSCode and Blender, but it was not reflected in Blender!
    I could not input directly or copy it. Is this sample only available in the script tab of Blender?
    Blender version 3.6.2

    • @CGPython
      @CGPython  Год назад +2

      I am glad to meet you as well!
      Short Answer:
      You can just move the call to `main()` out of `if __name__ == "__main__":`
      Long Answer:
      There is a bug/issue in the original Blender Dev extension for VSCode when you have scripts with
      if __name__ == "__main__":
      main()
      I have fixed it some time ago in a fork of Blender Dev extension for VSCode
      marketplace.visualstudio.com/items?itemName=cgpython.blender-development-experimental-fork
      I have been procrastinating to push this fix into the main extension.

    • @yossy3Dcraft
      @yossy3Dcraft Год назад +1

      @@CGPython Thank you.
      After flying to the appropriate site and installing the software.
      After restarting VSCode, call the script
      I was able to check it in Blender without any problems!

  • @modofino1556
    @modofino1556 Год назад

    How does the code know which function is called on line 73? Because in the class you just create the function (or method) and it is never called -> create_mesh_object.. in example 1 with Classes.. I found it! Is in the line 66, inside the function "add_into_scene". Perfect! Yours tutos really helps me a lot! Thanks

  • @pasionporblender
    @pasionporblender Год назад +1

    would you please make an example of panel instance ok let me explain you have a parent panel and want to place into it a variable number of child panels so the child panels must be created dynamically i hope i am clearly enough

    • @CGPython
      @CGPython  Год назад +2

      Hi! 👋
      Thank you for the tutorial suggestion!

  • @chriswong225
    @chriswong225 Год назад +1

    I feel very difficult to use class in blender. Because blender have its own class structure need to register into the scene. I dont know when I should add a class to contain it or not. like I have a object(class) need to store data into the scene(propertyGroup), and doing some operator(bpy.props.Operator), and also have UI itself (bpy.props.panel), i am okay to follow what blender told me to do. register the class and use it. but it should be a one class contained it on the logic of the class. but how

    • @CGPython
      @CGPython  Год назад

      I'm not 100% sure what your question here is.
      You would split your add-on logic/data into three parts:
      "Storage" of your settings/data (via bpy.types.PropertyGroup)
      "Performing an action" on that data or using that data (via bpy.types.Operator)
      "Display" the settings, and provide a way to trigger your operator from the UI (bpy.types.Panel)
      Having everything in one class is not a good idea. This would mean you can't easily reuse your code, and it would become a code maintenance problem.
      I guess you are looking at this?
      docs.blender.org/api/current/bpy.props.html
      What add-on do you want to write?
      Do you have a basic script that is working? (without a PropertyGroup, without a Operator, and without a Panel)

    • @chriswong225
      @chriswong225 Год назад

      ​ @CGPython OMG, thank you for your detail respond.
      i was planned the Baker class is contained everything inside. like properties, operators, and also panels. But feel it just make things more complex... and also blender is not design to use on this way.
      and I try another way, but I feel I define a class just quite useless.
      class Baker:
      def __init__():
      self.output_path = ""
      def bakeBasecolor():
      ... context......
      def bakeRoughness():
      ... context......
      def setOutputPath():
      ... context......
      def getOutputPath():
      ... context......
      // more functions...
      class Baker_Settings(bpy.props.PropertyGroup):
      output_path = bpy.props.StringProperty()
      class Bake_OT_bakerBaseColor(bpy.types.Operator):
      path : bpy.props.StringProperty()
      def execute(self, context):
      # I need to define the class I created inside
      bake = Baker()
      bake.setOuptutPath(self.path)
      bake.bakeBasecolor()
      class Bake_OT_getOutput(bpy.types.Operator):
      def execute(self, context):
      # I need to define the class I created inside
      bake = Baker()
      bake.getOutputPath()
      I feel the class I define is useless, the __init__ "self." variable is not shared and update to another functions(operators).
      so in blender all the shared property need to define to bpy.conotext.scene.* propertygroup. the PropertyGroup registy is like def __init__(): self.varaibles...
      do not feel have any moment need to define a custom class not (bpy.types.*) ?
      the class self.variable is not sharing to each other anyway...?
      I just feel to make functions in global and call them repeatly rather then make classes. Just cannot find the way its useful and no just make things more complex or indirect

    • @CGPython
      @CGPython  Год назад

      You still need to define classes for the PropertyGroup, and the Operators.
      But if you want, you can just define functions instead of Baker.

  • @konradkisiel2067
    @konradkisiel2067 Год назад +1

    I have watched some of your videos, and they are great.
    But in this tutorial, I think, in the first class example, you show unproper use of get and set methods.
    Explanation below:
    Firstly, get and set methods are used for data encapsulation (hiding it).
    In your example data can be also accessed via attributes, as I show below:
    class ExampleClass:
    def __init__(self, example_parameter):
    self.example_data = 33
    self.example_param = example_parameter
    def example_method(self):
    print(self.example_param)
    def get_example_data(self):
    return self.example_data
    def set_example_data(self, value):
    self.example_data = value
    >>> example_class_object.
    example_data
    example_method()
    example_param
    get_example_data()
    set_example_data(
    # access via attributes is possible (they are not hidden, data is not encapsulated which is the main purpose of get and set methods)
    >>> example_class_object = ExampleClass(example_parameter = 123)
    >>> example_class_object.example_param
    123
    >>> example_class_object.example_param = 50
    >>> example_class_object.example_param
    50
    >>> example_class_object.example_data
    33
    >>> example_class_object.example_data = 45
    >>> example_class_object.example_data
    45
    Secondly, I think in Blender is better to use attributes instead of get and set methods.
    To my knowledge, this is the usual way to access default Blender object attributes.
    My first example: The same class but with access to the data only by attributes, class data is not hidden.
    class ExampleClass:
    def __init__(self, example_parameter):
    self.example_data = 33
    self.example_param = example_parameter
    def example_method(self):
    print(self.example_param)
    >>> example_class_object = ExampleClass(example_parameter = 123)
    >>> example_class_object.example_
    data
    method()
    param
    >>> example_class_object.example_param
    123
    >>> example_class_object.example_param = 50
    >>> example_class_object.example_param
    50
    >>> example_class_object.example_data
    33
    >>> example_class_object.example_data = 45
    >>> example_class_object.example_data
    45
    My last example: the proper use of get and set methods
    Note: "__" before attribute means private, it limits the visibility of the attribute to the class internals
    class ExampleClass:
    def __init__(self,example_parameter):
    self.__example_data = 33
    self.__example_param = example_parameter
    def example_method(self):
    print(self.__example_param)
    def get_example_data(self):
    return self.__example_data
    def set_example_data(self, value):
    self.__example_data = value
    >>> example_class_object = ExampleClass(example_parameter = 123)
    >>> example_class_object.
    example_method()
    get_example_data()
    set_example_data(
    #access to the class data is impossible via attributes, it is possible only via get and set methods
    >>> example_class_object.example_method()
    123
    >>> example_class_object.get_example_data()
    33
    >>> example_class_object.set_example_data(45)
    >>> example_class_object.get_example_data()
    45

    • @CGPython
      @CGPython  Год назад +3

      Hi,
      Thank you for your comment!
      I do understand that the data in my example class is not hidden and that you can still access these instance attributes by name.
      Note: this is a tutorial for beginners, and I didn't intend to go into the concepts of private data and the name mangling mechanism.
      This video is intended to give a basic mental model of classes.

    • @konradkisiel2067
      @konradkisiel2067 Год назад

      ​@@CGPython Yes, in the example there is no reason to use get and set methods, like in other Blender objects attributes are usuall access way to an object's data.
      On the other hand, the main reason for using getters and setters in programming is limiting access to the class data, so implementation of getters and setters which allow access to object data via attributes is a kind of bad programming habit, which can lead to problems in larger codebases.

    • @konradkisiel2067
      @konradkisiel2067 Год назад +1

      ​@@CGPython
      Overall it is a great tutorial, with a lot of knowledge packed into 30 min video. The getters and setters use is just the only issue which I found. In my opinion, classes example 1 without get and set methods would be better (because they are improperly used).
      Thank you for this tutorial, I learn from it a lot.

    • @CGPython
      @CGPython  Год назад +1

      @@konradkisiel2067 I agree that I could have created a better example there. 🙂
      Thank you for providing your feedback ❤

  • @Artistjourney4991
    @Artistjourney4991 Год назад

    if you payme hahaha, i can add subtitles in spanish if you want haha.

    • @CGPython
      @CGPython  Год назад

      Interesting idea 📝