Skip to content

Commit

Permalink
Lots of updates and bugfixes, added support for colormaps
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrunnels committed Dec 18, 2023
1 parent 0e22659 commit f98aa38
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 60 deletions.
4 changes: 2 additions & 2 deletions bleMD/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,13 @@ def draw(self, context):
layout.operator("wm.read_lammps_file")

layout.label(text="Basic Shader",)
layout.prop(mytool, "my_shader")
layout.prop(mytool, "colorby_property")
layout.prop(mytool, "my_normallow")
layout.prop(mytool, "my_normalhigh")

layout.operator("wm.basic_shade")

layout.prop(mytool, "my_enum")
layout.prop(mytool, "colormap")
row = layout.row()
row.operator("wm.enumerator")

Expand Down
179 changes: 179 additions & 0 deletions bleMD/bleMDColormaps.py

Large diffs are not rendered by default.

76 changes: 37 additions & 39 deletions bleMD/bleMDNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,54 +51,52 @@ def create_material():
mat = bpy.data.materials.get("my_mat")
#if mat:
# return

mat = bpy.data.materials.new("my_mat")
obj = bpy.data.objects["MD_Object"]
obj.data.materials.append(mat)

mat.use_nodes = True
mat_nodes = mat.node_tree.nodes
material_output = mat.node_tree.nodes.get('Material Output')
default_BSDF = mat.node_tree.nodes.get('Principled BSDF')
mat.node_tree.nodes.remove(default_BSDF)

principled = mat.node_tree.nodes.new('ShaderNodeBsdfPrincipled')
principled.location.y = 350

attribute = mat_nodes.new('ShaderNodeAttribute')
attribute.location = (-900, 250)
if not "my_mat" in bpy.data.materials.keys():
mat = bpy.data.materials.new("my_mat")
mat.use_nodes = True
mat_nodes = mat.node_tree.nodes
material_output = mat.node_tree.nodes.get('Material Output')
default_BSDF = mat.node_tree.nodes.get('Principled BSDF')
mat.node_tree.nodes.remove(default_BSDF)
principled = mat.node_tree.nodes.new('ShaderNodeBsdfPrincipled')
principled.location.y = 350

attribute = mat_nodes.new('ShaderNodeAttribute')
attribute.location = (-900, 250)

Math1 = mat_nodes.new('ShaderNodeMath',)
Math1.location = (-700, 250)
bpy.data.materials["my_mat"].node_tree.nodes["Math"].operation = 'SUBTRACT'
Math2 = mat_nodes.new('ShaderNodeMath',)
Math2.location = (-550, 250)
bpy.data.materials["my_mat"].node_tree.nodes["Math.001"].operation = 'DIVIDE'

color_ramp = mat_nodes.new('ShaderNodeValToRGB')
color_ramp.location = (-350, 250)

mat.node_tree.links.new(attribute.outputs[2], Math1.inputs[0])
mat.node_tree.links.new(Math1.outputs[0], Math2.inputs[0])
mat.node_tree.links.new(Math2.outputs[0], color_ramp.inputs[0])
mat.node_tree.links.new(color_ramp.outputs[0], principled.inputs[0])
mat.node_tree.links.new(principled.outputs[0], material_output.inputs[0])
Math1 = mat_nodes.new('ShaderNodeMath',)
Math1.name="bleMD_MathNode1"
Math1.location = (-700, 250)
Math1.operation = 'SUBTRACT'
Math2 = mat_nodes.new('ShaderNodeMath',)
Math2.name="bleMD_MathNode2"
Math2.location = (-550, 250)
Math2.operation = 'DIVIDE'

color_ramp = mat_nodes.new('ShaderNodeValToRGB')
color_ramp.location = (-350, 250)

mat.node_tree.links.new(attribute.outputs[2], Math1.inputs[0])
mat.node_tree.links.new(Math1.outputs[0], Math2.inputs[0])
mat.node_tree.links.new(Math2.outputs[0], color_ramp.inputs[0])
mat.node_tree.links.new(color_ramp.outputs[0], principled.inputs[0])
mat.node_tree.links.new(principled.outputs[0], material_output.inputs[0])

else:
mat = bpy.data.materials["my_mat"]

obj = bpy.data.objects["MD_Object"]
obj.data.materials.append(mat)

def updateDefaultShader():
my_shader = bpy.context.scene.bleMD_props.my_shader
my_normalhigh = bpy.context.scene.bleMD_props.my_normalhigh
my_normallow = bpy.context.scene.bleMD_props.my_normallow
my_range = my_normalhigh - my_normallow

bpy.data.materials["my_mat"].node_tree.nodes["Attribute"].attribute_name = my_shader
bpy.data.materials["my_mat"].node_tree.nodes["bleMD_MathNode1"].inputs[1].default_value = my_normallow
bpy.data.materials["my_mat"].node_tree.nodes["bleMD_MathNode2"].inputs[1].default_value = my_range

bpy.data.materials["my_mat"].node_tree.nodes["Math"].inputs[1].default_value = my_normallow
bpy.data.materials["my_mat"].node_tree.nodes["Math.001"].inputs[1].default_value = my_range

return my_shader


def defaultSettings():
if not bpy.context.scene.bleMD_props.override_defaults:
return
Expand Down Expand Up @@ -128,4 +126,4 @@ def setup():
create_material()
create_geonodes()
defaultSettings()
makeSun()
#makeSun()
70 changes: 51 additions & 19 deletions bleMD/bleMDProperties.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

from . bleMDUtils import *

from . bleMDColormaps import cm, getkeys


# ------------------------------------------------------------------------
# Scene Properties
Expand Down Expand Up @@ -89,35 +91,49 @@ def openLAMMPSFile(self, context):
subtype='DIR_PATH'
)

my_shader: StringProperty(
name="Select shader data",
description="Type in desired data field to shade by:",
default="c_csym",
maxlen=1024,
subtype='NONE'
)

def my_normalhigh_normallow_update(self,context):
my_normalhigh = bpy.context.scene.bleMD_props.my_normalhigh
my_normallow = bpy.context.scene.bleMD_props.my_normallow
my_range = my_normalhigh - my_normallow
bpy.data.materials["my_mat"].node_tree.nodes["bleMD_MathNode1"].inputs[1].default_value = my_normallow
bpy.data.materials["my_mat"].node_tree.nodes["bleMD_MathNode2"].inputs[1].default_value = my_range

my_normalhigh: FloatProperty(
name="Data Max (for normalization)",
description="Insert maximum value to shade by",
default=1,
update=my_normalhigh_normallow_update,
)

my_normallow: FloatProperty(
name="Data Min (for normalization)",
description="Insert minimum value to shade by",
default=0,
update=my_normalhigh_normallow_update,
)

my_enum: bpy.props.EnumProperty(
name="Colormap",
description="Select a colormap",
items=[('OP1', "Viridis", ""),
('OP2', "Plasma", ""),
('OP3', "Inferno", ""),
('OP4', "Jet", ""),
('OP5', "gnuplot2", "")]

def colormap_items(self,context):
return [(n,n,n) for n in cm.keys()]
def colormap_update(self,context):
while True:
elements = bpy.data.materials['my_mat'].node_tree.nodes['Color Ramp'].color_ramp.elements
if len(elements) == 1:
bpy.data.materials['my_mat'].node_tree.nodes['Color Ramp'].color_ramp.elements[0].position=0
break
bpy.data.materials['my_mat'].node_tree.nodes['Color Ramp'].color_ramp.elements.remove(elements[-1])

K,R,G,B = getkeys(context.scene.bleMD_props.colormap)
bpy.data.materials['my_mat'].node_tree.nodes['Color Ramp'].color_ramp.elements[0].color = (R[0],G[0],B[0],1)

for k,r,g,b in zip(K,R,G,B):
elem = bpy.data.materials['my_mat'].node_tree.nodes['Color Ramp'].color_ramp.elements.new(position=k)
elem.color=(r,g,b,1)

colormap: bpy.props.EnumProperty(
name="Colormap",
description="Select a colormap",
items=colormap_items,
update=colormap_update
)

#
Expand All @@ -135,19 +151,35 @@ def updateRadius(self, context):
scene = context.scene
mytool = scene.bleMD_props
radius = mytool.my_radius

obj = bpy.data.objects["MD_Object"]
if not obj: return
geonodes = obj.modifiers["build_geonode"]
if not geonodes: return
nodegroup = geonodes.node_group
m2p = nodegroup.nodes["Mesh to Points"]
m2p.inputs['Radius'].default_value = radius

my_radius: FloatProperty(
name="Atom Radius",
description="Radius",
default=1,
min=0,
update=updateRadius,
)


def colorby_property_items(self,context):
ret = []
for item in context.scene.datafieldlist:
if item.enable: ret.append((item.name,item.name,item.name))
return ret
def colorby_property_update(self,context):
prop = context.scene.bleMD_props.colorby_property
print(prop)
bpy.data.materials["my_mat"].node_tree.nodes['Attribute'].attribute_name = prop
colorby_property: EnumProperty(
items=colorby_property_items,
update=colorby_property_update,
name="Color by:",
default=None,
description="Property to use for color shading",
)
7 changes: 7 additions & 0 deletions bleMD/bleMDUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ def loadUpdatedData(pipeline):

me.update()


#props_for_selector = ()
#for a in attrs.keys():
# props_for_selector.add((attr,attr,attr))
#bpy.context.scene.bleMD_props.colorby_property.items=props_for_selector


# Call setup function - Jackson
setup()

0 comments on commit f98aa38

Please sign in to comment.