Skip to content

Commit

Permalink
Moving from scene to object properties, adding the ability to add fro…
Browse files Browse the repository at this point in the history
…m menut
  • Loading branch information
bsrunnels committed Dec 31, 2023
1 parent 35c08e8 commit c5f4424
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 70 deletions.
76 changes: 56 additions & 20 deletions bleMD/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class WM_OT_bleMDReadLAMMPSFile(Operator):
my_tmp_cntr = 0

def execute(self, context):
pipeline = startOvito()
loadUpdatedData(pipeline)
ob, pipeline = startOvito()
loadUpdatedData(ob, pipeline)

return {'FINISHED'}

Expand All @@ -92,8 +92,9 @@ class WM_OT_bleMDRigKeyframes(Operator):

def execute(self, context):
scene = bpy.context.scene
nlammpsframes = scene.bleMD_props.number_of_lammps_frames
stride = scene.bleMD_props.lammps_frame_stride
obj = bpy.context.object
nlammpsframes = obj.bleMD_props.number_of_lammps_frames
stride = obj.bleMD_props.lammps_frame_stride

keyInterp = context.preferences.edit.keyframe_new_interpolation_type
context.preferences.edit.keyframe_new_interpolation_type = 'LINEAR'
Expand All @@ -104,9 +105,9 @@ def execute(self, context):
for key in ob.data.shape_keys.key_blocks:
ob.shape_key_remove(key)

pipeline = startOvito()
ob, pipeline = startOvito()
for i in range(nlammpsframes):
loadUpdatedData(pipeline)
loadUpdatedData(ob, pipeline)
print(i)
scene.frame_set(i*stride)

Expand Down Expand Up @@ -136,20 +137,21 @@ class WM_OT_bleMDRenderAnimation(Operator):

def execute(self, context):
scene = bpy.context.scene
obj = bpy.context.obj

# Remove shape keyframes if there are any
ob = bpy.data.objects['MD_Object']
if ob.data.shape_keys:
for key in ob.data.shape_keys.key_blocks:
ob.shape_key_remove(key)

pipeline = startOvito()
ob, pipeline = startOvito()
for frame in range(scene.frame_start, scene.frame_end + 1):
print("Rendering ", frame)
scene.render.filepath = scene.bleMD_props.renderpath + \
scene.render.filepath = obj.bleMD_props.renderpath + \
str(frame).zfill(4)
scene.frame_set(frame)
loadUpdatedData(pipeline)
loadUpdatedData(ob, pipeline)
bpy.ops.render.render(write_still=True)

return {'FINISHED'}
Expand All @@ -170,7 +172,8 @@ class WM_OT_Enumerator(Operator):

def execute(self, context):
scene = context.scene
mytool = scene.bleMD_props
obj = context.object
mytool = obj.bleMD_props

mat = bpy.data.materials.get("my_mat")
mat_nodes = mat.node_tree.nodes
Expand Down Expand Up @@ -209,7 +212,12 @@ class OBJECT_PT_bleMDPanel(Panel):

@classmethod
def poll(self, context):
return context.object is not None
if not len(bpy.context.selected_objects): return False
if not context.object: return False
ob = bpy.context.object
if 'bleMD_object' not in ob.data.keys(): return False
return True
#return context.object is not None

def execute(self, context):
return {'FINISHED'}
Expand All @@ -220,7 +228,8 @@ def draw_header(self, context):
def draw(self, context):
layout = self.layout
scene = context.scene
mytool = scene.bleMD_props
obj = context.object
mytool = obj.bleMD_props

layout.prop(mytool, "override_defaults")

Expand All @@ -243,11 +252,11 @@ def draw(self, context):
#
# DATA FIELDS
#
if len(scene.datafieldlist):
if len(obj.datafieldlist):
layout.label(text="Data fields from file")
row = layout.row()
row.template_list("bleMDDataFieldsList", "The_List", scene,
"datafieldlist", scene, "list_index")
row.template_list("bleMDDataFieldsList", "The_List", obj,
"datafieldlist", obj, "list_index")
layout.operator("wm.read_lammps_file")

layout.label(text="Basic Shader",)
Expand All @@ -272,6 +281,30 @@ def draw(self, context):
layout.operator("wm.render_animation")


class bleMDOpenFileDialogOperator(bpy.types.Operator):
bl_idname = "object.blemd_open_file_dialog"
bl_label = "Open MD file"

filepath: StringProperty(subtype='FILE_PATH')

def execute(self, context):
#resetDefaultsForMD()
#print("MY DATAFILE=",self.filepath)
ob, pipeline = startOvito(hardrefresh=True,filename=self.filepath)
#loadUpdatedData(ob, pipeline)
#bpy.context.object.bleMD_props.lammpsfile = self.filepath
return {'FINISHED'}

def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}


# Only needed if you want to add into a dynamic menu.
def bleMDOpenFileDialogOperator_menu(self, context):
self.layout.operator_context = 'INVOKE_DEFAULT'
self.layout.operator(bleMDOpenFileDialogOperator.bl_idname, text="Dialog Operator")


# ------------------------------------------------------------------------
# Registration
Expand All @@ -287,6 +320,7 @@ def draw(self, context):
bleMDDataFieldsLIProperty,
bleMDDataFieldsList,
OBJECT_PT_bleMDPanel,
bleMDOpenFileDialogOperator,
)


Expand All @@ -311,10 +345,10 @@ def register():

bpy.app.handlers.frame_change_post.clear()

bpy.types.Scene.bleMD_props = PointerProperty(type=bleMDProperties)
bpy.types.Object.bleMD_props = PointerProperty(type=bleMDProperties)

bpy.types.Scene.datafieldlist = CollectionProperty(type=bleMDDataFieldsLIProperty)
bpy.types.Scene.list_index = IntProperty(
bpy.types.Object.datafieldlist = CollectionProperty(type=bleMDDataFieldsLIProperty)
bpy.types.Object.list_index = IntProperty(
name="Index for datafieldlist", default=0)


Expand All @@ -328,14 +362,16 @@ def register():
icons_dir = os.path.join(os.path.dirname(__file__), "resources")
custom_icons.load("ovito", os.path.join(icons_dir, "ovito.png"), 'IMAGE')

bpy.types.VIEW3D_MT_add.append(bleMDOpenFileDialogOperator_menu)


def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
del bpy.types.Scene.bleMD_props
del bpy.types.Object.bleMD_props
bpy.utils.previews.remove(custom_icons)

bpy.types.VIEW3D_MT_view.remove(bleMDOpenFileDialogOperator_menu)


if __name__ == "__main__":
Expand Down
21 changes: 13 additions & 8 deletions bleMD/bleMDNodes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import bpy

def create_geonodes():
obj = bpy.data.objects["MD_Object"]
#obj = bpy.data.objects["MD_Object"]
obj = bpy.context.object
if not "bleMD_object" in obj.data.keys():
return

geo_nodes = obj.modifiers.get("build_geonode")
if not geo_nodes:
Expand All @@ -12,7 +15,7 @@ def create_geonodes():


def create_group(name="geonode_object"):
mytool = bpy.context.scene.bleMD_props
mytool = bpy.context.object.bleMD_props

group = bpy.data.node_groups.get(name)
# check if a group already exists
Expand Down Expand Up @@ -86,19 +89,21 @@ def create_material():
else:
mat = bpy.data.materials["my_mat"]

obj = bpy.data.objects["MD_Object"]
obj.data.materials.append(mat)
#obj = bpy.data.objects["MD_Object"]
obj = bpy.context.object
if "bleMD_object" in obj.data.keys():
obj.data.materials.append(mat)

def updateDefaultShader():
my_normalhigh = bpy.context.scene.bleMD_props.my_normalhigh
my_normallow = bpy.context.scene.bleMD_props.my_normallow
my_normalhigh = bpy.context.object.bleMD_props.my_normalhigh
my_normallow = bpy.context.object.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

def defaultSettings():
if not bpy.context.scene.bleMD_props.override_defaults:
if not bpy.context.object.bleMD_props.override_defaults:
return

for scene in bpy.data.scenes:
Expand All @@ -112,7 +117,7 @@ def defaultSettings():
if space.type == 'VIEW_3D':
space.shading.type = 'RENDERED'

bpy.context.space_data.context = 'OBJECT'
#bpy.context.space_data.context = 'OBJECT'


def makeSun():
Expand Down
31 changes: 18 additions & 13 deletions bleMD/bleMDProperties.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class bleMDProperties(bpy.types.PropertyGroup):

def updateFrameStride(self, context):
scene = context.scene
mytool = scene.bleMD_props
obj = context.object
mytool = obj.bleMD_props
nframes = mytool.number_of_lammps_frames
stride = mytool.lammps_frame_stride
bpy.context.scene.frame_start = 0
Expand Down Expand Up @@ -69,9 +70,9 @@ def updateFrameStride(self, context):
max=100
)

def openLAMMPSFile(self, context):
resetDefaultsForMD()
startOvito(hardrefresh=True)
# def openLAMMPSFile(self, context):
# resetDefaultsForMD()
# startOvito(hardrefresh=True)


lammpsfile: StringProperty(
Expand All @@ -80,7 +81,7 @@ def openLAMMPSFile(self, context):
default="",
maxlen=1024,
subtype='FILE_PATH',
update=openLAMMPSFile,
#update=openLAMMPSFile,
)

renderpath: StringProperty(
Expand All @@ -92,8 +93,8 @@ def openLAMMPSFile(self, context):
)

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_normalhigh = bpy.context.object.bleMD_props.my_normalhigh
my_normallow = bpy.context.object.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
Expand Down Expand Up @@ -122,7 +123,7 @@ def colormap_update(self,context):
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)
K,R,G,B = getkeys(context.object.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):
Expand All @@ -149,10 +150,14 @@ def colormap_update(self,context):

def updateRadius(self, context):
scene = context.scene
mytool = scene.bleMD_props
obj = context.object
mytool = obj.bleMD_props
radius = mytool.my_radius
obj = bpy.data.objects["MD_Object"]
if not obj: return
obj = context.object
if not "bleMD_object" in obj.data.keys():
return
#obj = bpy.data.objects["MD_Object"]
#if not obj: return
geonodes = obj.modifiers["build_geonode"]
if not geonodes: return
nodegroup = geonodes.node_group
Expand All @@ -169,11 +174,11 @@ def updateRadius(self, context):

def colorby_property_items(self,context):
ret = []
for item in context.scene.datafieldlist:
for item in context.object.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
prop = context.object.bleMD_props.colorby_property
print(prop)
bpy.data.materials["my_mat"].node_tree.nodes['Attribute'].attribute_name = prop
colorby_property: EnumProperty(
Expand Down
Loading

0 comments on commit c5f4424

Please sign in to comment.