-1
$\begingroup$

I was trying to replicate this solution: How to draw a flat ellipse surface in Blender with the following known dimensions?

import bpy
import math

#IMPORTANT : set 3d cursor to world origin    
bpy.context.area.spaces[1].cursor_location = (0.0, 0.0, 0.0)

#Add circle curve
bpy.ops.curve.primitive_bezier_circle_add(view_align=False, enter_editmode=False, location=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

#define variables
a = 10
b = 2
c = 3 # I use the half of c
d = 5

#Calculate width
width = a + b   #or width = a + d + b?

#Calculate horizontal distance from world origin of c
distance = b / 2

#Calculate height c for the circle
#x^2 + y^2 = 1, see Unit Circle
x = (distance / (width / 2)) * math.pi * 0.5  

cCircle = math.sqrt(1 - x**2)

#now we have to scale the height of the circle, so cCircle becomes c. And give its width.
factor = c / cCircle

bpy.ops.transform.resize(value=((width)/2, factor, 1), constraint_axis=(True, True, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)

#Calculate location origin
originX = -((width) / 2 ) + b
originY = 0
originZ = 0

#Set 3d Cursor at Origin location
bpy.context.area.spaces[1].cursor_location = (originX, originY, originZ)

#Set origin to 3d cursor
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')

#Move object to world origin
bpy.ops.transform.translate(value=((width/2) - b, 0, 0), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True)

#Voila

But there is a lot of errors on Blender, like this

enter image description here

How to make it work?

$\endgroup$
1
  • $\begingroup$ Why not use the Add>Mesh>Math function to create a surface $\endgroup$
    – X Y
    Jul 28, 2022 at 22:00

1 Answer 1

0
$\begingroup$
import bpy
import math

win = bpy.context.window
scr = win.screen

def use_temp_override():
    version = bpy.app.version
    major = version[0]
    minor = version[1]
    return not (major < 3 or (major == 3 and minor < 2))

def get_areas(type):
    return [area for area in scr.areas if area.type == type]

def get_regions(areas):
    return [region for region in areas[0].regions if region.type == 'WINDOW']

def execute():

    bpy.context.scene.cursor.location = (0,0,0)

    a = 10
    b = 2
    c = 3
    d = 5

    width = a + b
    distance = b / 2

    x = (distance / (width / 2)) * math.pi * 0.5  

    cCircle = math.sqrt(1 - x**2)
    factor = c / cCircle

    areas  = get_areas('VIEW_3D')

    # ========================================================================================
    # (if) execute using temp override
    # ========================================================================================

    if use_temp_override():

        with bpy.context.temp_override(window=win, area=areas[0], regions=get_regions(areas)[0], screen=scr):

            bpy.ops.view3d.snap_cursor_to_center()
            bpy.ops.curve.primitive_bezier_circle_add(radius=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
            bpy.ops.transform.resize(value=((width)/2, factor, 1), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
            bpy.context.scene.cursor.location = (-((width) / 2 ) + b, 0, 0)
            bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
            bpy.ops.transform.translate(value=((width/2) - b, 0, 0), orient_axis_ortho='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)

    # ========================================================================================
    # (else) execute using legacy override
    # ========================================================================================        

    else:
        override = {
            'window': win,
            'screen': scr,
            'area': areas[0],
            'region': get_regions(areas)[0],
        }
        bpy.ops.view3d.snap_cursor_to_center(override)
        bpy.ops.curve.primitive_bezier_circle_add(override, radius=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
        bpy.ops.transform.resize(override, value=((width)/2, factor, 1), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
        bpy.context.scene.cursor.location = (-((width) / 2 ) + b, 0, 0)
        bpy.ops.object.origin_set(override, type='ORIGIN_CURSOR')
        bpy.ops.transform.translate(override, value=((width/2) - b, 0, 0), orient_axis_ortho='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)

print("start ======")
execute()
$\endgroup$
4
  • $\begingroup$ Im not lucky: Python: Traceback (most recent call last): File "\Text", line 72, in <module> File "\Text", line 51, in execute UnboundLocalError: local variable 'override' referenced before assignment $\endgroup$
    – Yoshinatsu
    Jul 28, 2022 at 22:20
  • $\begingroup$ you are on blender 3.2 right? or higher $\endgroup$ Jul 28, 2022 at 22:21
  • $\begingroup$ yeah i fixed it now $\endgroup$ Jul 28, 2022 at 22:22
  • 1
    $\begingroup$ Works like magic!!!! Thank you! $\endgroup$
    – Yoshinatsu
    Jul 28, 2022 at 22:30

You must log in to answer this question.

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