1

As I'm a Blender greenhorn, I need your help. Here is my problem:

I have a document added as plane. Next, I want to label certain points within the document. Afterwards, I transform (fold and rotate) the document. In the end, I want to read off the new positions of my labels. Moreover, the points should not be visible when rendering the image.

I am doing the transformation by rotating bones. This works great! (see picture)

enter image description here

However, I am struggling with the labeling of points (see example in the attached image) and reading off the new position.

I would appreciate any help :-)

Background:

I want to generate a training data set for my Neural Network.

Cheers,

Julian

Julian
  • 11
  • 2

1 Answers1

0

One thing you could try is creating separate vertex groups for each point you want to label and then using the following Python code in the script editor to get the positions for each vertex. Let me know if it works (or if it doesn't).

import bpy
import bmesh

ObjectName = "Plane" VertexGroups = ["VGroup 1", "VGroup 2", "VGroup 3"]

obj = bpy.data.objects[ObjectName] vGroupIndex = 0

def select_obj(obj): obj.select_set(True) bpy.context.view_layer.objects.active = obj

def create_cube(): bpyscene = bpy.context.scene mesh = bpy.data.meshes.new("Vertex") cube = bpy.data.objects.new("Vertex", mesh) bpyscene.collection.objects.link(cube) select_obj(cube)

bm = bmesh.new()
bmesh.ops.create_cube(bm, size=1.0)
bm.to_mesh(mesh)
bm.free()

return cube


def get_vertices_in_group(a,index): vlist = [] for v in a.data.vertices: for g in v.groups: if g.group == index: vlist.append(v.index) return vlist

def create_boxes(): boxes = []; for vgi in VertexGroups: vg = obj.vertex_groups[vgi] box = create_cube() bpy.ops.object.constraint_add(type="COPY_LOCATION") box.constraints["Copy Location"].target = obj box.constraints["Copy Location"].subtarget = vg.name boxes.append(box); return boxes

def get_locations(boxes, should_delete): for box in boxes: print(" ") print(box.name) mat = box.matrix_local pos = (mat[0][3], mat[1][3], mat[2][3]) print(pos) if should_delete: bpy.ops.object.select_all(action="DESELECT") select_obj(box) bpy.ops.object.delete(use_global=False)

boxes = create_boxes() get_locations(boxes, True)

You can replace ObjectName with whatever your plane is called and VertexGroups with the list of the names of the vertex groups you end up using for each vertex.

The way it works is by creating a box for each vertex group named in the VertexGroup list, and then using the copy location constraint to move the box over to wherever the vertex is. Then it uses the box's matrix_local to find it's actual position (just checking the box's location doesn't take constrains into account and thus just returns (0,0,0))

It doesn't work to just find the vertices in the vertex groups and spit out their positions (vertex.co) because that only gives the position of the vertex relative to the object's origin as it is in edit mode (i.e. it won't account for armature poses, constraints, or even moving the mesh in object mode)

I doubt this is the fastest or best way to do this, but hopefully it will work well enough. Hope it helps, and let me know if it works (or if it doesn't)

ElliotThomas
  • 848
  • 4
  • 14
  • thanks for you answer @ElliotThomas. I also had this idea in mind. However, adding vertices in the plane (i.e. document paper) messes up the rotation with the bones (pic will follow). Maybe there is a way to fix this problem so I can follow your approach. – Julian Jul 07 '20 at 06:59
  • @Julian, as I was trying to figure out what was causing the issue you were having, I realized that my script only gives the positions of the vertices without counting the deform caused by the bones. I've figured out a way to get around it, and I'm working on the script right now. Basically, I'm going to add boxes in object mode with the copy location modifier to copy the location of each vertex in the appropriate vertex groups. I'll then read the box.matrix_local to find it's true location and thus location of the vertex. – ElliotThomas Jul 08 '20 at 12:43
  • @Julian I fixed my answer. If you just create new vertex groups and add vertices to those, it shouldn't affect the armature. I mean, if you go into edit mode, the mesh will go back to its original pose without regard to the armature, but once you go back to object mode, it will fix itself. – ElliotThomas Jul 08 '20 at 13:41
  • I'll try as soon as possible (probably by tmrw). Thank you so much already :-) I'll let you know if it works! – Julian Jul 08 '20 at 13:47
  • First I have to apologize about saying that adding vertices messes up the rotation. I just had no clue about setting the vertex groups properly. Now I know how it works :-) About your idea ... I really like it! I think it does a quite nice job. However, it still returns (0,0,0) for the cube's position. I'll try to fix that. – Julian Jul 09 '20 at 08:43
  • As I need the position of the vertices, i.e. cubes, in the rendered pictured I just use @zeffii 's solution in https://blender.stackexchange.com/questions/882/how-to-find-image-coordinates-of-the-rendered-vertex. Thanks a lot!!! – Julian Jul 09 '20 at 09:12
  • @Julian Okay, I'm glad you found something that works. I had that same problem with it returning (0,0,0) as well, and I think it just happens when it doesn't have enough time to update. It might even work to put a sleep command between creating the boxes and reading their positions. I noticed that even when it returned (0,0,0), if I checked the position of the box later in the Python Console, it gives me the correct answer. So I guess if you were to run some other part of your program and read the cubes' positions later, it might work. – ElliotThomas Jul 09 '20 at 11:40