Beginner Blender Python Tutorial: for loop practice

Sdílet
Vložit
  • čas přidán 25. 07. 2024
  • This video tutorial for Blender Python beginners will go over a script that uses a for loop to create a scene. We will also examine how you can use a while loop instead of a for loop.
    Final code:
    gist.github.com/CGArtPython/0...
    Beginner Blender Python Exercise: Repeating code with for loops
    • Beginner Blender Pytho...
    Beginner Blender Python Exercise: Introduction to If Statements (part 1)
    • Beginner Blender Pytho...
    00:00 - Intro
    01:09 - Set up the workspace
    01:19 - Create a cuboid
    09:20 - Stack and rotate the cuboids
    18:40 - The 360-degree challenge
    22:49 - Adding the while loop
    20:01 - Outro
    Outro Music
    Geographer - Easy Shake
    #blender #python #scripting

Komentáře • 17

  • @mysticdavestarotmachinesho5093

    Wow the wild loop! sounds like fun. Oh ... the while loop. Darn

  • @galdocstutorials
    @galdocstutorials Před 29 dny

    You are criminally underrated. I've been wanting to get into coding stuff in blender for ages, and this was the thing that got it done. Hope you keep up the good work!

  • @kvdam8826
    @kvdam8826 Před rokem

    Very well explained. Thank you for making these tutorials.

  • @DanyGates0693
    @DanyGates0693 Před rokem

    Valuable info thanks for putting time into this!

  • @scipio1943
    @scipio1943 Před rokem

    Amazing!

  • @mind_of_a_darkhorse
    @mind_of_a_darkhorse Před rokem

    Thanks for another great video! Keep up the good work! Python is so handy to know!

    • @CGPython
      @CGPython  Před rokem

      I am happy you liked it! 🙂

  • @romanbardas7366
    @romanbardas7366 Před rokem

    Thanks You.for your work/

  • @josephk.9567
    @josephk.9567 Před rokem

    Neat channel! Keep it up

  • @Trowel-mw9wh
    @Trowel-mw9wh Před rokem

    Hi, great video, thanks for sharing!
    If you want to work more comfortably running everything from the script, you can add these lines to the code to remove all objects from the scene at the beginning of the script (after the imports, of course):
    # Delete all the objects in the scene
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()
    This also allows you to save the script in a .py file and run it from the command line with blender: Blender -P file.py

    • @CGPython
      @CGPython  Před rokem +1

      Hey
      Thanks for suggesting this!
      I'll think about starting to use this for the beginner videos.
      The catch is that I wanted to make the script minimal for beginners.
      I use a more complex cleaning function in my videos that are not for beginners.
      see `clean_scene()`
      here github.com/CGArtPython/bpy_building_blocks/blob/main/src/bpybb/utils.py

  • @wjyzxcv
    @wjyzxcv Před rokem

    Another great tutorial! I'm wondering if possible to create the new objects in parallel (parallel for loop?) and whether it would be beneficial. By pre-calculating the matrices for translation and rotation each object does not rely on the previous one:
    def mat_mul_list(n, mat):
    lst = [mat]
    for i in range(n):
    lst.append(lst[i]@mat)
    return lst
    def spiral_stack(obj, n_step: int, angle_offset: float, height_offset: float):
    """stack object on local Z axis vertically with rotation"""
    #make list of translation matrices
    trans_mat = Matrix.Translation((0.0, 0.0, height_offset))
    lst_trans_mat = mat_mul_list(n_step, trans_mat)
    #make list of rotation matrices
    rotation_mat = Matrix.Rotation(angle_offset, 4, "Z")
    lst_rotation_mat = mat_mul_list(n_step, rotation_mat)
    for i in range(n_step):
    #copy(linked) object
    new_obj = obj.copy()
    #apply translation and rotation
    new_obj.matrix_basis @= lst_trans_mat[i]
    new_obj.matrix_basis @= lst_rotation_mat[i]
    #add new object to scene
    C.scene.collection.objects.link(new_obj)

    • @CGPython
      @CGPython  Před rokem

      Hey!
      It would be very beneficial if this were possible.
      To get true parallelism, you would need to write code in C/C++ or Rust (this is advanced-level stuff). Here is an example
      czcams.com/video/TJXtio1YDCw/video.html
      As far as I know, the `multiprocessing` Python package is not supported with Blender Python. `multiprocessing` is the standard Python library package that is usually used to work with threads and create processes to work on processing tasks in parallel. Note: there might be a third-party Python package that you can use for these calculations.
      Also, I haven't done a lot of research on this and might be wrong about if it is possible or not.

  • @shmuelisrl
    @shmuelisrl Před rokem

    you made a mistake.
    since you are using the diameter (being that you are rotating from the center of the mesh) and not the radius you want to rotate it by pi (180°) and not tau (360°)

    • @CGPython
      @CGPython  Před rokem

      The 360 degree rotation is a design choice, not a math one.
      I understand I can get the same-looking mesh just by rotating 180 degrees, but I would like that extra twist here.