2
$\begingroup$

I want to add my custom mesh to Blender. I already know about vertices, edges, faces, bpy.data.meshes, etc, but I can't figure out how to create an interactive parametric mesh, just like Maya's primary object that can adjust the height or radius of the mesh on the fly.

enter image description here

UPDATE:

What shape is actually not important, it can be any shape that can be adjusted on the fly.

It can be a plane with only 3 vertices, like this:

import bpy

verts = []
edges = []
faces = []

verts.append([0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0])
faces.append([0,1,2])

mesh = bpy.data.meshes.new("New Mesh") 
obj = bpy.data.objects.new("New Object", mesh) 
col = bpy.data.collections.get("Collection") 
col.objects.link(obj)
bpy.context.view_layer.objects.active = obj

mesh.from_pydata(verts, edges, faces)

Now I want to add a slider to control the position of the vertices, or change its shape or even the number of vertices. How to do that?

My goal is to create an addon that dynamically generates pie charts and adjusts them on the fly. This is what I did in Maya:

enter image description here

I know about the Extra Objects addon, but I also found out that it doesn't have a dynamic slider either. For example, for Add > Mesh > Gear, you can find some parameters, but you can't see the result in real-time. After changing the parameters, you have to right click > Change Gear to refresh the object.

That's why I wanted to make my own, dynamically adjustable object. Just like in Maya.

$\endgroup$
4
  • $\begingroup$ Hello and welcome. Oh so you already have your shape? What shape do you want to add? because Blender has already alot of shapes available with the Extra Objects addon Please update your post to show what mesh you want to add. You can also see this answer for some idea blender.stackexchange.com/a/298786/142292 $\endgroup$
    – Harry McKenzie
    Commented Aug 11, 2023 at 2:44
  • $\begingroup$ Add -> mesh -> gear", you can find some parameters, but you can't see the result in real-time.. > No that is not correct, you can actually see the adjustments in real time like changing the number of teeth in the lower left corner of the Add Gear popup, just like in my gif i presented with the torus. Either you are doing it incorrectly or there is a problem with your blender. Please show a gif that demonstrates what you mean with the "you can't see the result in real-time" $\endgroup$
    – Harry McKenzie
    Commented Aug 11, 2023 at 4:57
  • $\begingroup$ here is the blender gear video: youtube.com/shorts/3BuReo0-1pw?feature=share $\endgroup$
    – g2m.agent
    Commented Aug 11, 2023 at 6:03
  • 1
    $\begingroup$ Oh, I found the bottom left panel and it works. Wondering what the parameters in "Object Property -> Custom Properties" do. $\endgroup$
    – g2m.agent
    Commented Aug 11, 2023 at 6:14

1 Answer 1

2
$\begingroup$

Blender already has several Polyhedrons you can choose from. For example if you enable the built-in Add Mesh: Extra Objects addon under menu Edit > Preferences > Add-ons, you can add the Torus via menu Add > Mesh > Torus and you can also add several different Parametric Surfaces using Add > Mesh > Math Function. You can add more objects from the Add Mesh: Geodesic Domes. It's also possible to add these complex surfaces using Geometry Nodes. See this and that.

enter image description here

If you want to add your own mesh, you can do it like this or you can use the bmesh module. Here I have prepared a script which you can run in the Text Editor which registers a new operator called MyShapeOperator that creates a simple Torus when you click Run Script, but you can create any shape with any editable properties. Then go to 3D View and press F3 to search it. Now you can edit the example properties I have added using the Adjust Last Operation popup panel that appears in the lower left corner.

enter image description here

import bpy
from bpy.props import *
import math

class MyShapeOperator(bpy.types.Operator):
    bl_idname = "object.my_shape"
    bl_label = "My Shape Object Operator"
    bl_options = {'REGISTER', 'UNDO'}

    radius_major : FloatProperty (
        name = "Major Radius",
        description = "This is the Major or Outer Radius",
        default = 2.0,
        min = 0.3,
        max = 10.0
    )

    radius_minor : FloatProperty (
        name = "Minor Radius",
        description = "This is the Minor or Inner Radius",
        default = 0.5,
        min = 0.4,
        max = 7.0
    )

    segments_major : IntProperty (
        name = "Major Segments",
        description = "This is Major Segments",
        default = 48,
        min = 4,
        max = 200
    )
    
    segments_minor : IntProperty (
        name = "Minor Segments",
        description = "This is Minor Segments",
        default = 12,
        min = 4,
        max = 200
    )

    def create_or_update_your_shape(self, context):
        location = (0, 0, 0)
        rotation = (0, 0, 0)

        bpy.ops.mesh.primitive_torus_add(
            align='WORLD',
            major_radius=self.radius_major,
            minor_radius=self.radius_minor,
            major_segments=self.segments_major,
            minor_segments=self.segments_minor,
            location=location,
            rotation=rotation
        )

    def execute(self, context):
        self.create_or_update_your_shape(context)
        
        return {'FINISHED'}

def register():
    bpy.utils.register_class(MyShapeOperator)

def unregister():
    bpy.utils.unregister_class(MyShapeOperator)


if __name__ == "__main__":
    register()
$\endgroup$
0

You must log in to answer this question.

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