clear mesh data

if i want to declare a mesh like me = Blender.Mesh.New(molding_name)

in the main program

then use it in different functions to add differents objects then draw it in blender

is there a way to clear up all the vertices edges faces data before using it inside a function

or may be at the end of the main function after drawing it in blender
so that it’s empty of data and ready to be used again

note: this is to be able to make functions and reduce the qty of codes required
but i need to make index for edge a global variable too
cause i need to add more vertex after the function

right now i have to repeat the code in each functions

Thanks

a way to do that :

this to create a temporary mesh if doesn’t already exist :

try :tmpMesh=Mesh.Get("tmpMesh")
except NameError:tmpMesh=Mesh.New("tmpMesh")

no verts means no datas at all.

this to initialise the mesh before operations :

tmpMesh.verts.delete(range(len(tmpMesh.verts)))

but before to init, you’ll have to copy the mesh datas to another mesh.
http://www.blender.org/documentation/249PythonDoc/Mesh.Mesh-class.html#copy

or at the object level (but you’ll have to create a temp object also) if your function also modify the container/object :
http://www.blender.org/documentation/249PythonDoc/Object-module.html#Duplicate

you can update just one time at the end, it will be faster.
link each new meshe to an object,
link each object to the scene,
redraw().

hope to clarify the situation

i don’t really want to duplicate the data
and the only data i have right now is vertex edges
later may be some faces

so i was wondering if there was a way to clear all data inside
so that it does not intefere with the new one i want to add in the new mesh
mind you the name of the mesh changes each time i do a new function

i can provide a sinple file with 2 functions just to see what i mean
might be easier to understand the situtaiton

i just create a mesh inside a function with a given name
then link it to the scene inside the function
so it’s job is done

after i call another function with another name
and do another mesh
but i will like to use only one mehs if possible that would then be global
if i can say that which measn tht at the end of the function i can clear it
and be ready to ereceive the new data in an another function!

Thanks

hope to clarify the situation

are you woody allen ? :smiley:

ok I read it once more… maybe this can help, else send your functions :wink:

if you want only one ‘global’ mesh in multiple objects, it implies that these objects will have exactly the same geometry (same verts, edges, faces), by concept. they can’t be different.

if you delete the data inside this mesh and recreate some other edges inside it, all the linked objects will receive the new datas.

you need several meshes if you want different shapes, there’s no other way.

you can deform an object with modifiers even if it shares a mesh with some other object. but it does not seem to be your purpose.

no problem with several mens that is what i’m doing right now

but at the same time i’m creating a new mesh with new name - vertex edges in each function

but i have to repeat the create mesh in each function

i want to try to save some of the repeating code in some function
which affect the mesh inside the function

the only way would be to create another functions
where these repeating code lines could be done
but right now as the mesh is defined insdie each function it is not global and not acesssible from other function!

that’s where the saving would be

but no certain if this can be done

Thanks

Not sure what do you want to achieve. Maybe you can post your code?

If you need multiple different meshes, then you must write mesh = Blender.Mesh.New() each time you create a new mesh.

If you do not want to repeat mesh = bpy.data.meshes.new(name) in each function, maybe loop helps?:


for i in range(0,number_of_meshes)
     my_mesh =bpy.data.meshes.new(name) 

Still, I’m not sure if you want to perform same or different routines for generation of ech mesh?

If you want to pass a mesh between functions, then just pass it as argument:


def First():
      my_mesh = bpy.data.meshes.new(name) 
     AddSomeVerts( my_mesh )
 
def AddSomeVerts(mesh_object):
    mesh_object.verts.extend(#some verts...#)

As for the emptying a mesh: you can do that, but that won’t create a new mesh. It just empties a mesh data. You will loose all data in the old mesh and wont create a new mesh. Only mesh.new() creates a new empty instance of mesh.

but right now as the mesh is defined insdie each function it is not global and not acesssible from other function!

I think you make a confusion between the mesh name in Blender (the name in the gui) and the name of the python object representing your mesh.


# I use it because it's easier :
tmpMesh=Mesh.Get("tmpMesh")

# but in real python life it means :
PythonObjectName=Mesh.Get("meshRealName")

the mesh is neither global nor local. the mesh exists anyway. but the PythonObjectName is either local or global.

maybe try this :
whatever the name of the mesh and its object are, even if the name already exists and create .001 .002 etc names, func1 and func2 will work.


import Blender
from Blender import Object,Scene,Mesh

sc=Scene.GetCurrent()

def create(meshName) :
	global sc
	me=Mesh.New(meshName)
	ob=Object.New("Mesh", meshName)
	ob.link(me)
	sc.objects.link(ob)
	return ob,me

def func1() :
	print 'func1 created',
	ob,me=create('toto')
	print ob.name,'and',me.name


def func2() :
	print 'func2 created',
	ob,me=create('toto')
	print ob.name,'and',me.name
	ob.LocX=1.0
	
func1()
func2()
Blender.Redraw()

you can send to a function or return from a function a python object representing a mesh or an object. (or anything you want in fact) :


# an object represneting a mesh
def clear(me) :
	me.verts.delete(range(len(me.verts)))

toto=Mesh.Get('toto')
clear(toto)


# a string, the name of the mesh
def clear(meName) :	
	me=Mesh.Get(meName)
	me.verts.delete(range(len(me.verts)))

clear('toto')

i’m trying to create a script where i can drawn in blender

different type of moldings qty= 30 types

in many of the function i have to draw a circle segment and add this to a a mesh object
in each function with a different mesh name - one name per type of molding

here is one example of a function i did for an OVOLO molding

now the mesh in the function is given a name for the mesh throught the variable molding_name before calling it

the ob is created outside at the level of the main


 
def create_ovolo(molding_name):

 global edgeId,medges, mn_type_act, mesh_name 

 mn_type_act = 2
 edgeId=0
 genr=5.0
 XC=0
 YC=0
 delt=5.0
 medges=[]
 
 #create the mesh - using the mesh_name argument
 
 me = Blender.Mesh.New(molding_name)
 
     
#                                 N   =  ANGLE IN  DEGREES
 for n in range(0,90+delt/2,delt):
  ANGTR=n*pi/180
  XC=genr*cos(ANGTR)
  YC=genr*sin(ANGTR)
  p1=[XC,YC,0]
  verts=[p1]

# add vertices
  me.verts.extend( verts )
  
# make edges
#  print 'before edgeid=  ',edgeId
  
  if edgeId > 0:
   medges.append([edgeId-1, edgeId])
   me.edges.extend(medges)
   
  edgeId+= 1
 
    
#  do the bottom part 
 h1= -genr*0.2  #  Left   

 p1=[XC+h1,YC,0]
 verts=[p1]         
 me.verts.extend( verts )  
  
# medges.append([edgeId-1, edgeId])
# me.edges.extend(medges)
  
    
 #get the current scene
 scn = Blender.Scene.GetCurrent()
 #create/link the object to the scene              
 ob = scn.objects.new(me, molding_name + "Ob")
 
 mesh_name = me.name
 
 
 
 

but i have this type of circle segment making in a lot of functions
so i’m wondering if i can transfert theses segment circle portion into another function
so that it saves space

but as i indicated the problem seems to be with the fact that the me mesh is defined and created inside one function

but i think i’m beginning to see that may be this can be done hopefully!

the major problem right now is the space used by the script
i have done something like 24 functions
and i’m already at 3000 lines of codes!

i would like to save some spaces and make the lenght of the script shorter if this can be done

if necessary i can open another therad for a more complete script with example let say with 5 type of moldings
to show where the codes is being repeated

Thanks guys

but as i indicated the problem seems to be with the fact that the me mesh is defined and created inside one function


def create_ovolo(molding_name) :
   mesh=Blender.Mesh.New(molding_name)
   [...]
 <b>  return mesh</b>

from the other functions, call with :
aMesh=create_ovolo(mesh_name)

you should re-read the last posts :yes:

what does this instruction does

aMesh=create_ovolo(mesh_name)

what is this amesh - is this a new mesh ?

never use this type of instruction before!

Thanks

aMesh is just a variable name…
it says call the create_ovolo function with argument mesh_name then
retrieve the result of the function (the variable after return) in the aMesh variable name.

example

def toto() :
  a="one"
  return a

def toto2() :
  a="one"
  b="two"
  return a,b

c=toto()
print c

x,y=toto2()
print x,y

you should read the python documentation… not only the blender api. take time also to reread the last posts.
and also look how the other script works.

(be brave !)