Home Coding, Scripting, Shaders

[Blender 2.8] Import then Move

polycounter lvl 6
Offline / Send Message
bitinn polycounter lvl 6
I don't know this is so damn hard but after:

bpy.ops.import_scene.obj(filepath=output_filepath)

How should I move the imported object, assuming I want to move it to the same location as the previously active object.

I just need a minimal example, the imported object should be selected but not active, I tested this to work in console:

bpy.context.selected_objects[0].location = bpy.context.active_object.location

But it doesn't work in addon script, I don't know why.

Replies

  • bitinn
    Offline / Send Message
    bitinn polycounter lvl 6
    Actually, it does work.

    Damn, Blender doesn't refresh your addon when you modified it and install it again? (modified it as a separate file of course, not trying to change it in addon directory, knowing that caching will mess with me.)

    Do I have to delete its addon cache (pyc file) and restart Blender for changes to take effect?
  • RN
    Offline / Send Message
    RN sublime tool
    @bitinn you probably figured this out by now: if you want to change the code in an installed add-on, you can work on its installed files (exact location depends on if you're on Windows or Mac).
    You open the installed add-on script(s), change them, and to reload it with the new code you will untick + retick the add-on checkbox in the Add-ons tab of User Preferences. If you check the system console you'll see a "module changed on disk: [path to your addon]" message, meaning it reloads the add-on and runs the register() function again.

    If you're working on a script in the Text Editor view of Blender (something like an operator you're working on that is not yet installed), you'd just need to press the Run Script button again. Sometimes it's easier to test things this way.

    If you import some object / scene, it may change the active object. I would try doing what you want in three steps:
    1. Store the world matrix of the active object in some variable: (sourceMat = context.active_object.matrix_world.copy())
    2. Import the scene (import_scene.obj etc.)
    3. Identify the object in the loaded hierarchy, then overwrite its matrix: (loadedObj.matrix_world = sourceMat)
  • bitinn
    Offline / Send Message
    bitinn polycounter lvl 6
    RN said:
    @bitinn you probably figured this out by now: if you want to change the code in an installed add-on, you can work on its installed files (exact location depends on if you're on Windows or Mac).
    You open the installed add-on script(s), change them, and to reload it with the new code you will untick + retick the add-on checkbox in the Add-ons tab of User Preferences. If you check the system console you'll see a "module changed on disk: [path to your addon]" message, meaning it reloads the add-on and runs the register() function again.

    If you're working on a script in the Text Editor view of Blender (something like an operator you're working on that is not yet installed), you'd just need to press the Run Script button again. Sometimes it's easier to test things this way.

    If you import some object / scene, it may change the active object. I would try doing what you want in three steps:
    1. Store the world matrix of the active object in some variable: (sourceMat = context.active_object.matrix_world.copy())
    2. Import the scene (import_scene.obj etc.)
    3. Identify the object in the loaded hierarchy, then overwrite its matrix: (loadedObj.matrix_world = sourceMat)
    Thx, about the untick / retick to reload thing, I find my addon options are often lost during this process, is there a way to make my addon options persist?
  • RN
    Offline / Send Message
    RN sublime tool
    I don't think so. When you retick the checkbox your add-on is (re-)registered, and this will make it reset its properties to their default values.

    You'd need to change their default values to what you want, even if momentarily.
Sign In or Register to comment.