4
$\begingroup$

I have generated a large matrix with the (x,y) positions of all 4 vertices of a rectangle, using a Python script. It's called Matrix1, and has the dimensions (265,2,4)- 265 rectangles, 4 vertices with their x coordinate in the first row, and the y co-ordinate in the second row respectively.

As I am new to Blender, I am unsure as to what commands I would use to actually create rectangles with those vertices.

Any help or assistance would be greatly appreciated. Thank you in advance!

Edit for more information: (UPDATED)

For example:

[[0,1,1,0] [0,0,1,1]]

are the four vertices for the first rectangle, representing (0,0) (1,0), (1,1), (0,1); in general, if the order of vertices is [a,b,c,d] they should be joined to form edges ab, bc, cd, da.

Could anyone please help?

$\endgroup$
4
  • $\begingroup$ Something like this (for 2 rectangles)? [[(0, 0), (0, 1), (1, 0), (1, 1)], [(0, 0), (0, 1), (1, 0), (1, 1)]] $\endgroup$
    – batFINGER
    Jul 3, 2016 at 17:56
  • $\begingroup$ @batFINGER Yes, Matrix1 is exactly like that, but just for 1000 rectangles instead of just 2. My question is how would I go about setting those coordinates as the position of the vertices? $\endgroup$
    – Ishan
    Jul 3, 2016 at 17:57
  • $\begingroup$ One mesh or separate mesh object per rectangle? Also do you know about the vert order? eg if the verts are [a, b, c, d] will the edges be [ab, bc, cd, da] ? Perhaps edit q with some sample data. $\endgroup$
    – batFINGER
    Jul 3, 2016 at 18:05
  • 1
    $\begingroup$ I added some details to the question. Please let me know if there's any additional information required. $\endgroup$
    – Ishan
    Jul 3, 2016 at 18:14

2 Answers 2

1
$\begingroup$

This Code will create the mesh object with your faces.
As for the Colors you will have to decide how you want to store them.
With a different material per face?
With vertex colors?
In an image (UVMapping)?
It depends on what you want to do with your Object as to which answer is best for you.

import bpy
import bmesh

vertlist = [[(0, 0), (0, 1), (1, 1), (1, 0)],
            [(2, 2), (2, 3), (3, 3), (3, 2)]]

def create_mesh_object(context, vertlist):
    bm = bmesh.new()

    for faceverts in vertlist:
        bm_verts = []
        for vert in faceverts:
            bm_verts.append(bm.verts.new((vert[0], vert[1], 0)))
        bm.faces.new(bm_verts)

    me = bpy.data.meshes.new(name='MyMesh')
    ob = bpy.data.objects.new(name='MyObject', object_data=me)
    bm.to_mesh(ob.data)
    context.scene.objects.link(ob)

create_mesh_object(bpy.context, vertlist)
$\endgroup$
1
$\begingroup$

Code will create default plane primitive, then add a copy to scene, located and scaled to match each of your rectangles. This way all will share one mesh.

**(Assuming there's no rotation involved and that each is a rectangle)

import bpy
from mathutils import Vector
context = bpy.context
scene = context.scene
#define some rectangles
rectangles = [[(-1, -1), (1, -1), (-1, 1), (1, 1)],
              [(9, -1), (11, -1), (9, 1), (11, 1)],
              [(0, 0), (3, 0), (0, 9), (3, 9)]] 

# add a plane
bpy.ops.mesh.primitive_plane_add()
plane = context.active_object
# copy plane to each rect.
for rect in rectangles:
    r = plane.copy()
    verts = [Vector(v) for v in rect]
    # location and scale for copy from data.
    r.location.xy = sum(verts, Vector((0, 0))) / 4
    r.scale.xy = ((verts[0] - verts[1]).length / 2,
             (verts[1] - verts[3]).length / 2)
    #link to scene           
    scene.objects.link(r)
# remove initial plane
scene.objects.unlink(plane)
$\endgroup$
10
  • $\begingroup$ I got "TypeError: mathutils.Vector()" upon running your code, in your 16th line (with the for v in rect). Could you please advise me as to what I should do? $\endgroup$
    – Ishan
    Jul 5, 2016 at 18:47
  • $\begingroup$ @Ishan With just the code above, no edits, blender version 2.77? Have you added your own data, Vector(3) for instance will throw a type error. $\endgroup$
    – batFINGER
    Jul 5, 2016 at 19:17
  • $\begingroup$ verts = [Vector(v) for v in rect] # This line gave me a TypeError: mathutils.Vector() ; yes, I'm using Blender 2.77. Is there some way I could send my code to you? $\endgroup$
    – Ishan
    Jul 5, 2016 at 19:24
  • 1
    $\begingroup$ Notice the difference between [[(0, 0), (0, 1), (1, 0), (1, 1)]] and [[0,1,1,0], [0,0,1,1]] .. if you are using data of this form it is what is causing the error. You edited the question and changed to this format 2hrs ago. $\endgroup$
    – batFINGER
    Jul 5, 2016 at 19:27
  • 1
    $\begingroup$ Put a print(rect) statement before 210. If it isn't of the form [(0, 0), (0, 1), (1, 1), (1, 0)] then you will need to edit YOUR code accordingly. $\endgroup$
    – batFINGER
    Jul 5, 2016 at 19:51

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.