2
$\begingroup$

I’m trying to write coordinates into a vertex but somehow it doesn’t seem to take input

enter image description here

Every time this runs it prints some “2”s in the console as if it worked, but the next run they’re back to being 1 and -1 again.

import bpy
import bmesh

obj=bpy.context.edit_object
m=obj.data
bm=bmesh.from_edit_mesh(m)

print("---")

for i in range(0,8):

    v=bm.verts[i]

    co=obj.matrix_world@v.co
    print(co.x)
    co.x=2
    print(co.x)

bmesh.update_edit_mesh(m)

Please help

$\endgroup$

1 Answer 1

4
$\begingroup$

Well, the problem is here:

co=obj.matrix_world@v.co

So here you have used an operator @. Like adding + or multiplying * it gets 2 entities, makes an operation with them and results a new entity. The @ operator applies a transformation matrix on vector v.co and the result of this operation is saved in co property. So instead of linking the data it creates new data, so that assigning something to co doesn't make sense, because it holds a modified copy, not the link.

I suppose you can solve this like this:

import bpy
import bmesh
# import new module
import copy

obj=bpy.context.edit_object
m=obj.data
bm=bmesh.from_edit_mesh(m)

print("---")

for i in range(0,8):

    v=bm.verts[i]

    co=obj.matrix_world@v.co
    print(co.x)
    co.x=2
    print(co.x)
    
    # get a copy of transform matrix to be able to invert it:
    inverted = copy.copy(obj.matrix_world)
    inverted.invert()
    
    # now write property to the cube
    v.co = inverted @ co

bmesh.update_edit_mesh(m)

I have used inverted matrix to write coordinates back.

$\endgroup$
3
  • $\begingroup$ Thank you so much! I am very new to scripting so I just scavenged some code off of the web and put it together without really understanding much of it. I had no idea what the @ was. Could you point me to some systematic learning resources for Blender scripting? When I try to find resources the books are usually outdated and the online tutorials do not fully cover the fundamentals $\endgroup$
    – Egoless
    Jun 14 at 23:38
  • $\begingroup$ The course on blender cloud should be helpful, and it gets updated then something is changed in API studio.blender.org/training/scripting-for-artists $\endgroup$
    – Crantisz
    Jun 14 at 23:40
  • $\begingroup$ Fantastic! I appreciated it $\endgroup$
    – Egoless
    Jun 14 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.