1
$\begingroup$

I'm trying to make a simple script that extrude vertex in random ways, all works without extrude.

I do not know if it's good I wrote, but I do not know how to extrude vertex to the location of the cursor. Any idea what is wrong with my code and how to fix it? I run script in Edit mode with one vertex on the scene.

import bpy
import random

#generate random number
def RN():
    return int(random.random()*400)/20

for i in range(1,100):
#set 3d cursor to random location
    bpy.context.scene.cursor_location = (RN(),RN(),RN())
#extrude vertex to cursor location - doesn't work
    bpy.ops.mesh.dupli_extrude_cursor(rotate_source=True)
$\endgroup$

2 Answers 2

2
$\begingroup$

Invoking the bpy.ops.mesh.dupli_extrude_cursor(rotate_source=True) operator leads to this error message in the console window:

wm_operator_invoke: invalid operator call 'MESH_OT_dupli_extrude_cursor'

Although is is exactly what is printed in info header when manually extruding a vertex.

I can't tell why this doesn't work but try this:

import bpy
import random

#generate random number
def RN():
    return int(random.random()*400)/20

verts = []
edges = []
faces = []
for i in range(1,100):
#set 3d cursor to random location
    verts.append(( RN(),RN(),RN()))
    edges.append((i-1,i))
edges.pop() # remove last edge
print( verts )
print ( edges )
mesh = bpy.data.meshes.new("rand_mesh") 
mesh.from_pydata(verts, edges, faces)  
rand_obj = bpy.data.objects.new("rand_object", mesh) 
scene = bpy.context.scene 
scene.objects.link(rand_obj)

I really don't know for what something like this could be useful:

enter image description here

See also

http://wiki.blender.org/index.php/Doc:2.6/Manual/Extensions/Python/Geometry

$\endgroup$
1
$\begingroup$

It has no execute() method, but the default operator context is EXEC_DEFAULT. You need to use INVOKE_DEFAULT to make it call invoke().

However, it doesn't work as expected: cursor stands for the user mouse, not the 3D Cursor, and it will thus only extrude correctly if you make the following code an operator and run it in 3D View context:

import bpy

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        ctx = bpy.context.copy()
        ctx['area'] = area
        ctx['region'] = area.regions[-1]
        ctx['space_data'] = area.spaces.active
        bpy.ops.mesh.dupli_extrude_cursor(ctx, 'INVOKE_DEFAULT', rotate_source=True)
        break

If you run it from the PyConsole or Text Editor instead, it will extrude to a point outside the viewport (towards where the mouse is on screen).

$\endgroup$

You must log in to answer this question.

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