Beginner Blender Python Tutorial: for loop practice
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
Wow the wild loop! sounds like fun. Oh ... the while loop. Darn
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!
Glad I could help!
Very well explained. Thank you for making these tutorials.
Valuable info thanks for putting time into this!
Amazing!
Thanks for another great video! Keep up the good work! Python is so handy to know!
I am happy you liked it! 🙂
Thanks You.for your work/
Neat channel! Keep it up
Thank you :)
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
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
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)
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.
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°)
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.