1
$\begingroup$

enter image description here

I am not a programmer but managed to write a python script to draw a slab (Cube) with lots of vertices. I want to create a menu that will list the z values only and allow me to edit them. It is very difficult to go into the python script and try to edit the z values. Is it even possible to do in blender? I know I can use Transform to edit as well but it takes too long too especially if my slab is big and has a lot of elevations.

bl_info = {
    "name": "Slab",
    "author": "Ash Hafez, P.E.",
    "version": (1, 0),
    "blender": (2, 90, 1),
    "location": "View3D > Object",
    "description": "Adds a Slab Mesh Object",
    "warning": "",
    "doc_url": "",
    "category": "Object",
}


import bpy
from bpy.types import Operator
from bpy.props import FloatVectorProperty
#from bpy_extras.object_utils import AddObjectHelper, object_data_add
from mathutils import Vector
from bpy.types import (
    AddonPreferences,
    Operator,
    Panel,
    PropertyGroup,
)
from bpy.props import (IntProperty)

class OBJECT_OT_cube(Operator):
    bl_label = "Slab"
    bl_idname = "object.cube"
    #bl_description = "Adds a Slab Mesh Object"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    #bl_options = {'REGISTER', 'UNDO'}
    bl_catagory = "Slab"
    
    cubeResolution: bpy.props.IntProperty(
        name = "Cube Resolution",
        default = 6,
        min = 1,
        max = 15,
    description = "Octree Depth used in the Remesh Modifier",
)

   
    scale: FloatVectorProperty(
        name="scale",
        default=(1.0, 1.0, 1.0),
        subtype='TRANSLATION',
        description="scaling",
    )

        #def poll(cls, context):
        #return context.object.select_get() and context.object.type == 'CUBE' or     context.object.type == 'CURVE'

        #def invoke(self, context, event):
        #return context.window_manager.invoke_props_dialog(self)
    

#Distances are in 5 meters. Change distances as per your project
#Define verices,faces,edges

verts = [
(0,0,0),(0,5,0),(5,5,0),(5,0,0),(0,0,5),(0,5,5),(5,5,5),(5,0,5),(0,10,0),(5,10,0),(0,10,5),(5,10,5),(10,0,0),(10,5,0),(10,5,5),(10,0,5),
(15,0,0),(15,5,0),(15,5,5),(15,0,5),
(20,0,0),(20,5,0),(20,5,5),(20,0,5),
(0,15,0),(5,15,0),(0,15,5),(5,15,5),
(10,10,0),(10,10,5),(10,15,0),(10,15,5),
(15,10,0),(15,10,5),(15,15,0),(15,15,5),
(20,10,0),(20,10,5),(20,15,0),(20,15,5),
(0,20,0),(5,20,0),(0,20,5),(5,20,5),
(10,20,0),(10,20,5),(15,20,0),(15,20,5),
(20,20,0),(20,20,5),(25,0,0),(25,5,0),(25,0,5),(25,5,5),
(25,10,0),(25,10,5),(25,15,0),(25,15,5),(25,20,0),(25,20,5),
(0,25,0),(0,25,5),(5,25,0),(5,25,5),(10,25,0),(10,25,5),
(20,25,0),(20,25,5),(25,25,0),(25,25,5),
(15,25,0),(15,25,5)]

faces = [
(0,1,2,3),(7,6,5,4),(0,4,5,1),(1,5,6,2),(2,6,7,3),(3,7,4,0),
(1,8,9,2),(5,10,11,6),(2,6,11,9),(1,5,10,8),(8,9,11,10),
(7,15,12,3),(15,14,13,12),(6,14,13,2),(7,15,14,6),(3,12,13,2),
(12,16,17,13),(16,19,18,17),(13,17,18,14),(12,16,19,15),(15,19,18,14),
(16,17,21,20),(19,18,22,23),(23,22,21,20),(17,18,22,21),(16,19,23,20),(8,9,25,24),(11,10,26,27),(25,27,26,24),(9,11,27,25),(8,10,26,24),
(21,36,32,17),(22,37,33,18),(21,22,37,36),(17,18,33,32),(32,33,37,36),(18,14,29,33),(17,13,28,32),(13,14,29,28),(32,33,29,28),(28,29,11,9),(28,13,2,9),(29,14,6,11),(9,25,30,28),(29,11,27,31),(25,27,31,30),(28,29,31,30),(32,28,30,34),(30,31,35,34),(34,35,39,38),(36,32,34,38),
(32,33,35,34),(36,37,39,38),(37,33,35,39),(33,29,31,35),
(24,40,41,25),(25,41,44,30),(30,44,46,34),(34,46,48,38),
(24,26,42,40),(25,27,43,41),(31,45,44,30),(34,35,47,46),(38,39,49,48),
(27,26,42,43),(27,43,45,31),(35,31,45,47),(39,35,47,49),
(40,41,43,42),(41,44,45,43),(44,46,47,45),(47,46,48,49),(46,70,64,44),
(20,21,51,50),(20,23,52,50),(50,52,53,51),(21,22,53,51),(23,22,53,52),
(48,49,67,66),(58,48,66,68),(58,59,49,48),(69,67,66,68),(59,49,67,69),(59,69,68,58),(40,42,61,60),(41,43,63,62),(44,45,65,64),
(42,61,63,43),(40,60,62,41),(61,60,62,63),
(44,41,62,64),(45,43,63,65),(70,46,47,71),(47,45,65,71),(71,65,64,70),
(66,48,46,70),(49,47,71,67),
(36,37,55,54),(36,21,51,54),(38,39,57,56),(36,54,56,38),
(38,48,58,56),
(22,37,55,53),(51,53,55,54),(55,37,39,57),(54,55,57,56),
(39,49,59,57),(56,57,59,58),(67,71,70,66),(65,63,62,64)]


#Define mesh and object
mesh = bpy.data.meshes.new("Cube")
object = bpy.data.objects.new("Cube",mesh)

#Set location and scene of object
object.location = bpy.context.scene.cursor.location
bpy.context.collection.objects.link(object)

#Create mesh
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)



def execute(self, context):

        add_object(self, context)
      
        return {'FINISHED'}

def menu_func(self, context):
    self.layout.operator(OBJECT_OT_cube.bl_idname)


# Registration

def register():
    bpy.utils.register_class(OBJECT_OT_cube)
    bpy.types.VIEW3D_MT_object.append(menu_func)
 
  
def unregister():
    bpy.utils.unregister_class(OBJECT_OT_cube)
    bpy.types.VIEW3D_MT_object.remove(menu_func)
    
    
if __name__ == "__main__":
    register()

My goal is to get something like this below.

enter image description here

$\endgroup$
5
  • $\begingroup$ Can you draw a mockup of how you want it to work or something? You can edit the vertex XYZ in the sidebar of the 3D View (press [N]), although this takes some clicking around. It would also be possible to have a plain list of z values in the script which might make them easier to edit. $\endgroup$
    – scurest
    Mar 2, 2023 at 19:43
  • $\begingroup$ a list of z values in the script will work. but for some reason when I move any of the "Verts =" in the script things get messed up and vertices get moved around. $\endgroup$ Mar 2, 2023 at 19:50
  • $\begingroup$ how can i drop the .py script here? $\endgroup$ Mar 2, 2023 at 19:52
  • $\begingroup$ Edit your post and copy+paste the script. Place a line with ``` before and after it to mark it as code. $\endgroup$
    – scurest
    Mar 2, 2023 at 19:53
  • $\begingroup$ hopefully i did it right $\endgroup$ Mar 2, 2023 at 20:02

1 Answer 1

0
$\begingroup$

I'm not sure how easy this really is to use, but here's a script with a list of heights you can edit.

import bpy

# ===== Edit these! =====
width = 5         # number of verts in each row
height = 5        # number of verts in each column
side_length = 5   # side length of grid square
heights = [
    5.0, 5.0, 5.0, 5.0, 5.0,
    5.0, 4.0, 3.0, 4.0, 5.0,
    5.0, 3.0, 2.0, 4.0, 5.0,
    5.0, 2.0, 2.0, 3.0, 5.0,
    5.0, 2.0, 1.0, 1.0, 5.0,
]

#Build vertices
verts = []
for y in range(height):
    for x in range(width):
        h = heights[(height-1-y)*width + x]
        verts.append((x*side_length, y*side_length, 0))
        verts.append((x*side_length, y*side_length, h))

#Build faces
faces = []
r = 2 * width  # verts per row (one bottom + one top)
for y in range(height - 1):
    for x in range(width - 1):
        i = r*y + 2*x
        faces.append((i+2, i, i+r, i+r+2))                            # Bottom
        faces.append((i+3, i+r+3, i+r+1, i+1))                        # Top
        if y == 0: faces.append((i+1, i, i+2, i+3))                   # -Y Side
        if y == height - 2: faces.append((i+r+1, i+r+3, i+r+2, i+r))  # +Y Side
        if x == 0: faces.append((i, i+1, i+r+1, i+r))                 # -X Side
        if x == width - 2: faces.append((i+3, i+2, i+r+2, i+r+3))     # +X Side

#Define mesh and object
mesh = bpy.data.meshes.new("Cube")
object = bpy.data.objects.new("Cube", mesh)

#Set location and scene of object
object.location = bpy.context.scene.cursor.location
bpy.context.collection.objects.link(object)

#Create mesh
mesh.from_pydata(verts, [], faces)
mesh.update(calc_edges=True)
$\endgroup$
1
  • $\begingroup$ Thank you very much. This is definitely a lot easier than mine and should work for what I want. Thanks again. $\endgroup$ Mar 2, 2023 at 21:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .