Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto generate geometry nodes and basic material #5

Closed
RH2 opened this issue Mar 17, 2022 · 1 comment
Closed

Auto generate geometry nodes and basic material #5

RH2 opened this issue Mar 17, 2022 · 1 comment

Comments

@RH2
Copy link

RH2 commented Mar 17, 2022

Hi, thanks for creating this updated ply importer!
I wrote something that creates a material and geometry node tree:

import bpy

mat_name = "pointcloud"
materials = bpy.data.materials    
if materials.get(mat_name) is None: 
    print("creating pointcloud material")   
    mat = materials.new(mat_name)
    mat.use_nodes = True
    materialtree = mat.node_tree.nodes
    materialtree.remove(materialtree.get("Principled BSDF"))
    materialtree.remove(materialtree.get("Material Output"))
    
    attributenode = materialtree.new(type="ShaderNodeAttribute")
    attributenode.attribute_name="Col"
    outputnode = materialtree.new(type="ShaderNodeOutputMaterial")
    pbrnode = materialtree.new(type="ShaderNodeBsdfPrincipled")
    mat.node_tree.links.new(attributenode.outputs[0],pbrnode.inputs[0])
    mat.node_tree.links.new(pbrnode.outputs[0],outputnode.inputs[0])
mat = materials.get(mat_name)

#import object
bpy.ops.import_mesh.ply(filepath=r"C:\Users\User\Desktop\file.ply")
importedObject = bpy.context.active_object
#create geometry node tree
bpy.ops.object.modifier_add(type='NODES')
nodeTree = bpy.context.active_object.modifiers[0].node_group

cubenode = nodeTree.nodes.new(type="GeometryNodeMeshCube")
CubeSize = 0.001
cubenode.inputs[0].default_value[0] = CubeSize
cubenode.inputs[0].default_value[1] = CubeSize
cubenode.inputs[0].default_value[2] = CubeSize

instancenode = nodeTree.nodes.new(type="GeometryNodeInstanceOnPoints")
materialnode = nodeTree.nodes.new(type="GeometryNodeSetMaterial")
materialnode.inputs[2].default_value = mat
shadenode = nodeTree.nodes.new(type="GeometryNodeSetShadeSmooth")
realizenode = nodeTree.nodes.new(type="GeometryNodeRealizeInstances")
geonode = nodeTree.nodes[0]
outnode = nodeTree.nodes[1]

nodeTree.links.new( geonode.outputs['Geometry'] , instancenode.inputs['Points'])
nodeTree.links.new( cubenode.outputs['Mesh'], shadenode.inputs['Geometry'])
nodeTree.links.new( shadenode.outputs['Geometry'], materialnode.inputs['Geometry'])
nodeTree.links.new( materialnode.outputs['Geometry'], instancenode.inputs['Instance'])
nodeTree.links.new( instancenode.outputs['Instances'], realizenode.inputs['Geometry'])
nodeTree.links.new( realizenode.outputs['Geometry'], outnode.inputs['Geometry'])

@TombstoneTumbleweedArt
Copy link
Owner

That's a nice little script, thank you!
For some reason the Geo and Shader nodes all pile on each other at creation:
RH-2
I tried to move them like usual with .location = <x,y> but strange things happen.
This wasn't anything you did wrong, just odd Blender behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants