Master the Basics of Blender Python for Geo Nodes in Just One Video

Sdílet
Vložit
  • čas přidán 9. 07. 2024
  • This is a Blender Python Tutorial on the basics of using Python to create Geo Nodes! In this video, I will guide you through adding and connecting Geometry Nodes with Python, as well as updating the default input socket values.
    Initial Code
    github.com/CGArtPython/blende...
    Final Code
    github.com/CGArtPython/blende...
    Geo Nodes Python documentation
    docs.blender.org/api/current/...
    00:00 - Intro
    00:29 - Set up the workspace
    01:24 - Add a mesh and a geo nodes modifier
    02:50 - Create a reference to the node tree
    03:40 - Get the group output node
    04:16 - Add the Cube Mesh Geo Node
    05:25 - Add the Subdivide Mesh Geo Node
    06:30 - Update the position of the nodes
    08:00 - Define links between nodes
    11:00 - Refactor node creation code
    13:37 - Add the Triangulate Geo Node
    16:25 - Add the Split Edges Geo Node and the Scale Elements Geo Node
    19:00 - Update Geo Node default inputs
    #blender #python #scripting #tutorial #geometrynodes

Komentáře • 49

  • @SunshinePosseCrew
    @SunshinePosseCrew Před měsícem +1

    Thanks for the videos! I would highly recommend writing a better from-to node link helper method where you can specify the from-to types in the function call. Here's what I wrote below:
    #
    def link_nodes_by_socket_type(node_tree, from_node, from_node_type, to_node, to_node_type):
    node_tree.links.new(from_node.outputs[from_node_type], to_node.inputs[to_node_type])
    #
    With this, you can easily link the nodes in one line without updating from-to vars each time :-)
    #
    link_nodes_by_socket_type(node_tree, from_node = split_edges_node, from_node_type = "Mesh", to_node = separate_geometry_node, to_node_type = "Geometry")
    #

  •  Před rokem +2

    Fantastic!

  • @dolmenbretagne9060
    @dolmenbretagne9060 Před rokem +3

    It's awesome ! I had started a little but now I understand the lack of knowledge that I had! Thank you very much we must continue to do this kind of tutorial!!!!!

    • @CGPython
      @CGPython  Před rokem +1

      I'm happy that this helped. 😀
      What did you need more understanding in?

    • @dolmenbretagne9060
      @dolmenbretagne9060 Před rokem

      @@CGPython For now I'm trying to understand your tutorial. I do lots of tests! it's really super ! There are so many things in the work that you bring us that for the moment I "digest"! Thank you again for everything and I will keep you informed of my discoveries! A+

  • @stefanguiton
    @stefanguiton Před 11 měsíci

    Great video!

  • @aaayerus
    @aaayerus Před měsícem

    Hey CG Pyhton, plz make a video about importing and controlling the inputs of an already created geometry nodes file, making use of a custom n-menu panel.

  • @VladyVeselinov
    @VladyVeselinov Před 23 dny +1

    kinda sucks that there isn't a "formatGraph" function, setting x/y coordinates for nodes feels tedious

    • @CGPython
      @CGPython  Před 20 dny

      You can use something like this
      gist.github.com/CGArtPython/1d72f786b924bd517a9a4b92c1a004e0
      This code aligns using the 'node_arrange' add-on in Blender.

  • @VitalyBanny
    @VitalyBanny Před 11 měsíci

    Nice lesson! Thanks! Is there a way to pan and zoom view of geometry node editor with python?

    • @CGPython
      @CGPython  Před 11 měsíci

      I'm not sure about panning, but we might be able to `frame all` or `frame selected`
      I have tried to `frame all,` but I'm not getting it to work

  • @Pooriel
    @Pooriel Před 8 měsíci

    Thanks for this awesome tutorial.
    I have a problem with creating links between nodes.
    linking geometry (green dots) input/outputs works as intended but it doesn't work when I create a link between two attribute (diamonds) input/outputs.
    I checked the DATA API and saw that the links were actually created but they were kinda disabled (Is_Hidden is enabled and cannot be disabled).

    • @CGPython
      @CGPython  Před 7 měsíci

      Can you share what Blender version you are using and what nodes / what attributes you are trying to connect?
      You can try to do what I did in this video (link with timestamp) and list all the inputs
      czcams.com/video/Z7tVeI1D7CY/video.html
      You might need to use a "hidden" name for the socket.

  • @TPSNS
    @TPSNS Před rokem

    thank you for the tutorial I only have one question how do you use Vim commands in blender

    • @CGPython
      @CGPython  Před rokem

      You are welcome!
      I don't use Vim commands in Blender. I usually use VSCode for my script development needs
      Here is how to set it up
      czcams.com/video/zP0s1i9EXeM/video.html

  • @nicolassuarez2933
    @nicolassuarez2933 Před 28 dny

    Outstanding! Do you know how to export it as .gltf with materials to? Thanks!

    • @CGPython
      @CGPython  Před 20 dny

      The easiest way would be in the modifiers panel, where you will need to apply all the modifiers (including the Geo Nodes modifier).
      You will get a final mesh that you will be able to export as a .gltf.
      There might be a way to do this without needing to apply.

    • @nicolassuarez2933
      @nicolassuarez2933 Před 15 dny

      @@CGPython I solved with a to newbie code... :)
      import bpy
      bpy.context.active_object
      bpy.ops.export_scene.fbx(filepath='C:/Users/camis/Downloads/base_model.fbx', check_existing=True, filter_glob='*.fbx')
      bpy.ops.import_scene.fbx(filepath='C:/Users/camis/Downloads/base_model.fbx', filter_glob='*.fbx')
      bpy.ops.export_scene.gltf(filepath='C:/Users/camis/Downloads/final_model.glb', export_format='GLB')

  • @avinashkaushik7181
    @avinashkaushik7181 Před rokem

    fantastic,,,,can you please make a video on python rigging

    • @CGPython
      @CGPython  Před rokem +1

      Thank you! 😊
      That is a great idea!

  • @stefankarlssonse6026
    @stefankarlssonse6026 Před 7 měsíci

    started looking for the version of Blender. Didnt find in description text or in the first 5 min of the video, so didnt continue.

    • @CGPython
      @CGPython  Před 7 měsíci

      I think this was 3.4 or 3.5

    • @stefankarlssonse6026
      @stefankarlssonse6026 Před 7 měsíci

      @@CGPython Thanks for making good content in general. :) Make it even better by including the version of Blender you are using. This is particularly important for things like geometry nodes and the python API.

    • @CGPython
      @CGPython  Před 7 měsíci

      Yeah, that is a good point.
      Thanks

  • @abiuniverse
    @abiuniverse Před 18 dny

    How to add geometry node on a mesh created from json data in the python script

    • @CGPython
      @CGPython  Před 15 dny +1

      So you are trying to load some data from a JSON file and create a geometry node network?

    • @abiuniverse
      @abiuniverse Před 15 dny

      @@CGPython yes, I am actually loading from a csv file but idea is the same. Its a volume data (4D) so using geometry nodes to create a volume with a custom density profile. Or do other things on it. I think the problem is that after creating the vertices the newly created object is not active and adding geo nodes without selecting any object is not working.

  • @user-tn4ng2ll7x
    @user-tn4ng2ll7x Před rokem

    Hi sir, can you create a tutorial about matrix snapping for fk/ik snapp?

    • @CGPython
      @CGPython  Před rokem +3

      Thank you!
      I'll add this to the list of requests 📖

  • @ashlynmccann6394
    @ashlynmccann6394 Před rokem

    Thank you for the great tutorial! I used this as a foundation for some geo nodes I am using in an add on. However, I keep getting this error: {AttributeError: 'geo_nodes' object has no attribute 'node' } on the line where I define my out_node as group output. I am using Blender 3.5. Do you have any advice on solving this error? I am relatively new to blender and python

    • @CGPython
      @CGPython  Před rokem

      Glad it was useful!
      It is hard to say without seeing you code
      Can you add your code spinet here?

    • @ashlynmccann6394
      @ashlynmccann6394 Před rokem

      @@CGPython Hi Thank you for your response! I actually fixed the original problem but now I have a new problem :,), I put the problem I am facing and code in a google doc here: docs.google.com/document/d/1ZlRFXkbgwDnMKPEQoGwd7YHh3zQBk6OoK3-bWW41lTQ/edit?usp=sharing
      essentially, I do not know why a couple of my nodes are not linking. I will also be posting on stack exchange, but any advice you have would be so appreciated! Thank you!

    • @ashlynmccann6394
      @ashlynmccann6394 Před rokem

      I wanted to reply with an update, I got the code to work! My issue was certain nodes have more "hidden" sockets than what is displayed so cannot connect them by the label of the socket (I was getting this with sockets labeled "Value", so instead, you can index it with [0], [1], etc. I just did trial and error playing with the indexing numbers until it worked but I am sure there is a more sophisticated way to do this, anyways, thanks for the tutorial! it was super helpful!

    • @CGPython
      @CGPython  Před rokem +2

      Ah yeah!
      I explain how you can link the nodes in a better way here (time stamp)
      czcams.com/video/hlZ_cFSlaMs/video.html

  • @user-ui1jb6le9r
    @user-ui1jb6le9r Před 6 měsíci

    I am using Blenders Geometry Nodes to create models (not using python). When I have created a model using geometry nodes I would like to export all the coördinates from the vertices as processed in the Group Output. Is there a way to obtain the data using python (as is presented in the Spreadsheet)?

    • @user-ui1jb6le9r
      @user-ui1jb6le9r Před 6 měsíci

      Never mind I found the answer online:
      import bpy
      import bmesh
      depsgraph = bpy.context.evaluated_depsgraph_get()
      obj = bpy.data.objects['Cube']
      bm = bmesh.new()
      bm.from_object( obj, depsgraph )
      bm.verts.ensure_lookup_table()
      print( "----" )
      for v in bm.verts:
      print( v.co )
      bm.free()
      This seems to work.
      I just could not find it ... but as soon as I asked the question ..... weird how that works.

    • @user-ui1jb6le9r
      @user-ui1jb6le9r Před 6 měsíci

      In addition I am looking to retrieve al the starting and ending vertices per edge.
      For this I have created the following code:
      ______________________________________________________________________________________________________________________
      import bpy
      import bmesh
      import numpy as np
      # get a reference to the active object
      mesh_obj = bpy.context.active_object
      # create a new bmesh object
      bm = bmesh.new()
      # initialize the bmesh object using the mesh data
      bm.from_mesh(mesh_obj.data)
      #Extract all vertex coords and package for export
      vertices = np.empty((0,3),int)
      for v in bm.verts:
      vertex = np.array([v.co.x, v.co.y, v.co.z])
      vertices = np.append(vertices,[vertex], axis=0)

      edges = np.empty((0,2),int)
      for e in bm.edges:
      edge = np.array(e.verts)
      edges = np.append(edges,[edge], axis=0)

      print(vertices)
      print(edges)
      bm.free
      ___________________________________________________________________________________________________________________
      The result however is:
      ___________________________________________________________________________________________________________________
      [[ 1. 1. 1.]
      [ 1. 1. -1.]
      [ 1. -1. 1.]
      [ 1. -1. -1.]
      [-1. 1. 1.]
      [-1. 1. -1.]
      [-1. -1. 1.]
      [-1. -1. -1.]]
      [[
      ]
      [
      ]
      [
      ]
      [
      ]
      [
      ]
      [
      ]
      [
      ]
      [
      ]
      [
      ]
      [
      ]
      [
      ]
      [
      ]]
      ________________________________________________________________________________________________________________
      Why are the results of the indices not just the index number but {BMVert(@@@@) ?
      How can I obtain just the index numbers?

    • @here2race
      @here2race Před 3 měsíci

      @@user-ui1jb6le9r It's not weird, it's well know thing :) en.wikipedia.org/wiki/Rubber_duck_debugging

  • @surtvalheim
    @surtvalheim Před 6 měsíci

    Do you know how create a new modifier ? using class MyModifier(bpy.types.Modifier) do not add the modifer in the add modifier list, even with MyModifier registered in the script as usual.

    • @CGPython
      @CGPython  Před 6 měsíci

      The Blender devs have described creating modifiers via Python as an anti-feature
      wiki.blender.org/wiki/Reference/AntiFeatures
      Basically Python is too slow to perform the operation most modifiers would do.
      It is still possible to write a modifier, but you will need to use C++/C.

    • @surtvalheim
      @surtvalheim Před 6 měsíci

      @@CGPython In C++ as a plugin or will everyone have download my own version of Blender every update ?

    • @surtvalheim
      @surtvalheim Před 6 měsíci

      Got the answer in the link you gave.