Trying To Comprehend getFromObject

I have a script that’s exporting out a Blender object’s vertex, face, normal, and UV information. I am attempting to convert from NMesh to Mesh. I’ve actually got it to work, however I can’t seem to get the subsurf modifier to be applied correctly in the export of the data. It’s previous way of doing this was:

mesh = NMesh.GetRawFromObject(obj.name)
mesh.transform(obj.getMatrix(), 1)
verts = mesh.verts
faces = mesh.faces

The new way (which is where I am having trouble) is:

mesh = Mesh.Get(obj.name)
mesh.getFromObject(obj.name, 0)
mesh.transform(obj.getMatrix(), 1)
verts = mesh.verts
faces = mesh.faces

When I use this, the subsurf modifier is applied, but via the subsurf’s levels rather than the render levels. And right as I export, the object subsurf’s again. And if I remove the subsurf modifier, it goes back one subsurface level put not to the original object (so I guess I am applying a subsurface level somehow). I’m hoping that the getFromObject is the key, but it’s implementation is what I’m not getting. Without getFromObject, no subsurf gets applied (since it doesn’t access the modifier). The api doc say that it should have two int values, but when I use two I get errors.
Any help/ideas would be appreciated.

mesh = Mesh.Get(obj.name)
mesh.getFromObject(obj.name, 0)

I don’t know if that’s the best way to do it. The first line will get the mesh reference from the obj and the second then copies the mesh plus applied modifiers back into that reference (this may be why you are seeing the subsurf being applied). Try using:

mesh = Blender.Mesh.New()
mesh.getFromObject(obj.name,0)

then just free up the mesh object when you’re done using del(mesh).

del mesh is okay but it wont free the datablock in blender if you ever link it to an object. (dealing with vertex groups)

you can do me.verts = None to free most of the memory the mesh uses,

I think I’m beginning to understand, but I’m still just shooting at the dark. I’ve got it to get the right subsurf, but now the object is in the wrong orientation when rendering to the outside renderer or just not looking right.
Here is what I’ve used that outputs the object in the wrong orientation:

mesh = Mesh.New()
mesh.getFromObject(obj.name, 0)
verts = mesh.verts[:]
mesh.transform(obj.matrix)

Here is the one that renders the right orientation, but it just looks odd when it renders… the shadows aren’t smooth and things like that.

mesh = Mesh.New()
mesh.getFromObject(obj.name, 0)
mesh.transform(obj.matrix)
verts = mesh.verts
faces = mesh.faces

Do I use me.verts = None after I export the data?

Okay, I think I got it now using:

mesh = Mesh.New()
mesh.getFromObject(obj.name, 0)
mesh.transform(obj.mat)
verts = mesh.verts
faces = mesh.faces

I think the matrix was the issue unless I was doing something stupid before.
Anyways, now the question is if creating a new mesh and using transform to export the vertex data has any consequences to the .blend size or script performance. I recall the transform example in the api savign then restoring vertices. Nor am I deleting any vertices (as per Cam’s suggestion).

I noticed that the subsurf data being exported is from the “levels” and not the “render levels.” The API says getFromObject(object, cage=0, render=0), with render determinng whether the render setting for modifiers will be used or not. The problem is when I try to use the second int (as in getFromObject(object, 0, 1) , I get an error (TypeError: Expected object or string and optional integer arguments). Is the second int for render not available in Blender 2.43?

if( !PyArg_ParseTuple( args, “O|ii”, &object_arg, &cage, &render ) )
return EXPP_ReturnPyObjError( PyExc_TypeError,
“expected object or string and optional integer arguments” );

so me.getFromObject(ob, 0, 1) should work

ALso, you can use teh object ratehr then object.name, its one less lookup and the name can return the wrong data when your using libs.

Thanks for the tip on using the object rather than the name.

I also went to Mesh.c to figure out what the deal was, but it definitely doesn’t seem to work. If anyone care’s to have a look at it - (cough ideasman cough) - You can find the script here:

http://www.geneome.net/blender/blenderfiles/scripts/sunflow_export_Mesh.py

On line 1132 I have getFromObject(obj, 0) but if you change it to getFromObject(obj, 0, 1), the script fails.

This works for me are you using blender 2.43?


import Blender
scn = Blender.Scene.GetCurrent()
ob = scn.objects.active

me = Blender.Mesh.New()
me.getFromObject(ob, 0, 1)

scn.objects.new(me)

Yeah, I’m using 2.43 and Python 2.4.4. Thanks for showing that you can get it to work. I’m still having no luck with getting the render int recognized, so I’ll keep trying to figure out what the deal is as it seems to be on my end.

does my small example work for you?

Cool, a sunflow exporter! - just tested om getFromObject(ob, 0,1) works in your script for me.

Your example doesn’t work for me :frowning: (I get the same error about the need for a string and two ints), but it works when it’s (ob, 0). And now that it works for you in the Sunflow script I posted, I’m even more perplexed. Did you use the code right in the script and just added the ,1 to the getFromObject?

yep, just added a 1 to getFromObject, to read me.getFromObject(ob, 0, 1)
strange it wont work since I dont think this has been touched since 2.43

can you double check your running 2.43, or get a recent build from graphicall

Sorry I relay dont mean to sound patronizing but this has been in for a while and works for me, the only thing I can think of that would cause this problem is that your running an older version of blender.

No worries about being patronizing. I could be missing something that’s right under my nose, so I’m glad to go through all options.

I just downloaded the default windows build from b.org and I downloaded the glass theme .exe from graphicall. The b.org version still doesn’t work, but the cvs build from graphicall does work. The only difference I see is that the two builds that don’t work have command windows that say compiled with Python 2.4 and are built with 2.43 source and the build that does work specifies that it’s compiled with Python 2.4.4 and is a cvs build.

I think I found the problem:
http://projects.blender.org/viewcvs/viewcvs.cgi/blender/source/blender/python/api2_2x/Mesh.c.diff?r1=1.123&r2=1.124&cvsroot=bf-blender
Looks like when 2.43 was released, getFromObject only allowed one int (O|i rather than O|ii). So builds after this commit should be fine (the cvs builds work fine). Looks like the script will have to wait till the next Blender release to use the render levels!
Thanks for your help ideasman!