1
$\begingroup$
def execute(self, context):
    stl_path = bpy.path.abspath(bpy.context.scene.stl_path)
    bpy.ops.import_mesh.stl(filepath=stl_path)
    try:
        ob_new = bpy.context.selected_objects[0]
        for f in ob_new.data.polygons:
            f.use_smooth = True
        for b in ob_new.data.polygons:
            bpy.context.space_data.show_textured_solid = True
            bpy.context.object.show_transparent = True
            bpy.context.scene.render.engine = 'CYCLES'
            bpy.ops.object.editmode_toggle()   
            bpy.ops.mesh.select_all(action='TOGGLE')                
            bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
            bpy.ops.mesh.select_all(action='TOGGLE')    
            bpy.ops.mesh.tris_convert_to_quads()
            bpy.ops.view3d.snap_cursor_to_selected()
            bpy.ops.object.origin_set(type = 'ORIGIN_CURSOR')                

Everything is working well, until I use origin_set. Maybe I should go to object mode, after converting mesh to quads. But when I'm adding bpy.ops.object.editmode_toggle() after tris_convert_to_quads Blender is freezing. Without this last command, everything is working, also snapping cursor to selected.


Ok, I changed script.

def execute(self, context):
    stl_path = bpy.path.abspath(bpy.context.scene.stl_path)
    bpy.ops.import_mesh.stl(filepath=stl_path)
    try:
        ob_new = bpy.context.selected_objects[0]
        for f in ob_new.data.polygons:
            f.use_smooth = True
            bpy.context.object.show_transparent = True
            bpy.context.scene.render.engine = 'CYCLES'
            bpy.ops.object.editmode_toggle()
            bpy.ops.mesh.select_all(action='TOGGLE')                
            bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
            bpy.ops.mesh.select_all(action='TOGGLE')    
            bpy.ops.mesh.tris_convert_to_quads()
            bpy.ops.mesh.faces_shade_smooth()
            bpy.ops.object.mode_set(mode='EDIT')
            bpy.ops.mesh.select_all(action='TOGGLE') 

to this line everything is ok, and script is ends in object mode. but If I add this line bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')

I have msg in console, that I must be in object mode. If i add this command bpy.ops.object.mode_set(mode='OBJECT') Blender is switching off.

$\endgroup$
4
  • 2
    $\begingroup$ Any error message in the console? Also please add what your actual goal is. $\endgroup$
    – brockmann
    Commented Mar 31, 2020 at 15:00
  • 2
    $\begingroup$ You can't set the origin in edit mode. $\endgroup$ Commented Mar 31, 2020 at 15:02
  • 1
    $\begingroup$ Using try and catch will remove seeing error messages in console. $\endgroup$
    – batFINGER
    Commented Mar 31, 2020 at 15:06
  • $\begingroup$ Also please don't ask same question again closed prior $\endgroup$
    – batFINGER
    Commented Mar 31, 2020 at 16:42

1 Answer 1

1
$\begingroup$

You are right. To set object origin, you must be in object mode. You can also try using bpy.ops.object.mode_set(mode='OBJECT') to change to object mode.

$\endgroup$
4
  • $\begingroup$ Ok, I added this bpy.ops.object.mode_set(mode='OBJECT'), after converting to quads, and Blender freezes again :-( When I added it after snap cursor to select, it happened the same $\endgroup$
    – Neuroup
    Commented Mar 31, 2020 at 16:02
  • $\begingroup$ In console I have only information that, import finished in 1.2480 sec. My goal is that, that I have button, that import stl, converting to quads, changing origin to volume, and after it, is moving object with new origin, to 0,0,0 $\endgroup$
    – Neuroup
    Commented Mar 31, 2020 at 16:11
  • $\begingroup$ @Neuroup I have a fix for blender freezing. Before running the script, go to 'window' and click 'toggle system console'. Rerun the script. If blender freezes, switch to the console and press 'q' to terminate the operation. In my experience, this results in the desired output many times. $\endgroup$ Commented Mar 31, 2020 at 16:23
  • $\begingroup$ I started the system console as you wrote. When Blender crashed, I pressed the q key in the console (few times). Unfortunately, it did nothing. I just have a message that Import Finished in 1.2490 sec. $\endgroup$
    – Neuroup
    Commented Apr 1, 2020 at 8:02

You must log in to answer this question.

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