Master the Basics of Blender Python for Geo Nodes in Just One Video
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
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")
#
Fantastic!
Many thanks! ❤️
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!!!!!
I'm happy that this helped. 😀
What did you need more understanding in?
@@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+
Great video!
Thanks!
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.
kinda sucks that there isn't a "formatGraph" function, setting x/y coordinates for nodes feels tedious
You can use something like this
gist.github.com/CGArtPython/1d72f786b924bd517a9a4b92c1a004e0
This code aligns using the 'node_arrange' add-on in Blender.
Nice lesson! Thanks! Is there a way to pan and zoom view of geometry node editor with python?
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
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).
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.
thank you for the tutorial I only have one question how do you use Vim commands in blender
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
Outstanding! Do you know how to export it as .gltf with materials to? Thanks!
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.
@@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')
fantastic,,,,can you please make a video on python rigging
Thank you! 😊
That is a great idea!
started looking for the version of Blender. Didnt find in description text or in the first 5 min of the video, so didnt continue.
I think this was 3.4 or 3.5
@@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.
Yeah, that is a good point.
Thanks
How to add geometry node on a mesh created from json data in the python script
So you are trying to load some data from a JSON file and create a geometry node network?
@@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.
Hi sir, can you create a tutorial about matrix snapping for fk/ik snapp?
Thank you!
I'll add this to the list of requests 📖
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
Glad it was useful!
It is hard to say without seeing you code
Can you add your code spinet here?
@@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!
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!
Ah yeah!
I explain how you can link the nodes in a better way here (time stamp)
czcams.com/video/hlZ_cFSlaMs/video.html
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)?
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.
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?
@@user-ui1jb6le9r It's not weird, it's well know thing :) en.wikipedia.org/wiki/Rubber_duck_debugging
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.
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.
@@CGPython In C++ as a plugin or will everyone have download my own version of Blender every update ?
Got the answer in the link you gave.