Creating Meshes with UV from Python

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

ssontech
Posts: 6
Joined: Wed Dec 12, 2012 11:36 pm

Creating Meshes with UV from Python

Postby ssontech » Thu Dec 13, 2012 12:06 am

I have an existing data-transfer script for recreating meshes inside Blender using Python.... a script creates these scripts.

It applies UV coordinates to the vertices from a pre-built texFaces array using the following code:

texFaces = [
[[0, 0], [0.04166666667, 0.08333333333], [0, 0.08333333333]],
[[0.04166666667, 0], [0.08333333333, 0.08333333333], [0.04166666667, 0.08333333333]],
[[0.08333333333, 0], [0.125, 0.08333333333], [0.08333333333, 0.08333333333]], ... ]
uvtex = mshobj.uv_textures.new()
for n,tf in enumerate(texFaces):
...datum = uvtex.data[n]
...datum.uv1 = tf[0]
...datum.uv2 = tf[1]
...datum.uv3 = tf[2]

uv1...uv3 don't exist in MeshTexturePoly. I understand that the data structures have been changed. What is the equivalent code in the new regime? This is per-facet UV, created from per-vertex UV. The coordinate and facet data is similarly built from program-generated arrays:

mshobj = bpy.data.meshes.new('Object01')
mshobj.from_pydata(coords, [], faces)
mshobj.update(calc_edges=True)
mshobj.calc_normals()

Thanks.

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Thu Dec 13, 2012 1:13 am

since 2.63 (bmesh), there can't be uv1-uv4 anymore, as each face can have unlimited number of vertices (ngons). Therefore, the API and the internal data structures changed. Have a look at the official import scripts and the API docs:
http://wiki.blender.org/index.php/Dev:R ... 2.63/BMesh

http://www.blender.org/documentation/bl ... tion-faces

There are also couple threads about this, here and on blenderartists.org
I'm sitting, waiting, wishing, building Blender in superstition...

ssontech
Posts: 6
Joined: Wed Dec 12, 2012 11:36 pm

Postby ssontech » Thu Dec 13, 2012 7:21 pm

CoDEmanX wrote:since 2.63 (bmesh), there can't be uv1-uv4 anymore, as each face can have unlimited number of vertices (ngons). Therefore, the API and the internal data structures changed. Have a look at the official import scripts


Yes, I know the data structures have changed and have seen those documents and a number of threads (generally on exporting, not importing). There isn't anything particularly informative to write code from. What official import scripts are you referring to? I'm looking for some nice simple working code to do what could be done before simply. I don't care about ngons or loops or editing, just creating triangles, quick and easy.

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Fri Dec 14, 2012 11:27 pm

As stated here:
http://www.blender.org/documentation/bl ... tion-faces

you can still use the old data structures, but they got new names. When a mesh datablock is empty, you can write to tessfaces (former faces), UV access should also work via tessface_uv_texture (was uv_texture).

But refrain from using from_pydata(), as you won't be able to add UVs 'cause it calls an implicit mesh validation/update and the tessface_* structures will become read-only.

You can, however, use foreach_set() - which is also used by from_pydata().


Official addons are the addons which come with Blender and which have been developed by the Blender team. There are also a lot stock addons, which aren't official, but community-made - nonetheless stable (if they come with blender, they had had a code review to get to trunk).

Checkout the importer code in
Blender\x.xx\scripts\addons\ (x.xx --> blender version, e.g. 2.65)

-> io_scene_3ds\ (using loops)
and
-> io_scene_obj\ (using tessface_*)
I'm sitting, waiting, wishing, building Blender in superstition...


Return to “Python”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest