get object by mesh

Is there a way to get the Object a Mesh-Object is linked to? I need this to be able to call makeDisplayList() on it when I change the Mesh. Remembering the Object is not an option, because I create the Mesh by NMesh.PutRaw(), so I don’t get the Object if the so called mesh already exists.

Or how can I make a copy of a NMesh without NMesh.PutRaw()?

do you meen this (user linked to a mesh):
http://www.blender.org/modules/documentation/237PythonDoc/NMesh.NMesh-class.html#users

suntzu

.users ony shows how many users there are.
The basic script would be…


# Datablock can be mesh, lamp, etc- anything that can be linked to an object.
def getAllDataUsers(datablock)
	datablockName = datablock.name
	users = datablock.users
	datablockType = type(datablock)
	objects = [] # Store all objects that use this mesh in this list
	
	
	for ob in Object.Get(): # a list of all objects in all scenes
		if ob.getData(1) != datablockName: # getData(1) is meshes name
			continue # Different data so skip the rest
		if type(ob.getData()) != datablockType:
			continue # skip the rest if were not the same type.
		# Ahah this is an object that uses us
		
		objects.append(ob)
		
		users -=1
		# There are 0 users, therefor we have found all object instances.
		if not users: 
			break
	return objects

This could not work cause Object.data has not getType() function .


import Blender

def getAllDataUsers(data) :
  return [O.getName() for O in Blender.Object.Get() 
       if O.getData().name==data.name and isinstance(O.getData(),type(data))]

print  getAllDataUsers(Blender.Object.GetSelected()[0].getData()) 


JMS your right, fixed code.
Now compares dataType which is a bit of a pity since getting the objects data is a bit if a speed hit, however it only ever does this if both data names are the same, so it can only happen a maximum of 6 times at worst- and thats if the user has 6 different object types all wih the same data name.

I do not understand the use of “continue” . Look at this :

def ANDsecondpart(): 
	  print 'AND 2'
	  return 1
TEST=[0,1,0,0,0,0]
print [t for t in TEST if  t==1 and ANDsecondpart()]

With the and operator, if the firt part is false the second part is not tested .

I do not understand the use of “continue” . Look at this :

def ANDsecondpart(): 
	  print 'AND 2'
	  return 1
TEST=[0,1,0,0,0,0]
print [t for t in TEST if  t==1 and ANDsecondpart()]

With the and operator, if the firt part is false the second part is not tested .[/quote]

“continue” seems lightly slower too :

import Blender
from Blender import Noise
	
def AND2(): 
	  print 'AND 2',
	  return 1

TEST=[]

for n in range(200000):
  if Noise.random()<0.1:
    TEST.append(1)
  else:		
    TEST.append(0)

t0=Blender.sys.time()

f=[t for t in TEST if  t==1 and AND2()]

t2= Blender.sys.time()-t0

t0=Blender.sys.time()
f=[]
for t in TEST:
	 if  t==0 :
		  continue
	 if AND2() :
		 f.append(t)

t3= Blender.sys.time()-t0

print t2
print t3

You comparint list comprehension to making a list and appending to it. Of course list comprehension if faster since it builds up the list internaly rather then python doing it.

Continue may be a tich slower and an IF with an AND, but your test compares other areas of python.

Also, Continue can be easier to read, more flexible and results in less heavly indented code.

I cant use list comprehension since Im not just building a list of objects, Im chicking weather I can break out of the loop allong thge way.

  • Cam

Like this ?

import Blender
from Blender import Noise
   
def AND2():
     print 'AND 2',
     return 1

TEST=[]
USERS=0
for n in range(200000):
  if Noise.random()<0.1:
    TEST.append(1)
    USERS+=1
  else:      
    TEST.append(0)

t0=Blender.sys.time()
f=[t for t in TEST if  t==1 and AND2()]
t2= Blender.sys.time()-t0

t0=Blender.sys.time()
f=[]
for t in TEST:
    if  t==0 :
        continue
    if AND2() :
       f.append(t)
       USERS-=1
    if USERS==0:
	   break
t3= Blender.sys.time()-t0

print
print t2
print t3

Okay, you right- using AND is faster then using continue, you just confused me when you threw in list comprehension, which is always faster then building a list with append.

Try with :

if Noise.random()<0.025:

Sorry if i confused you but all i wanted to say here was that, in this case, ‘and’ combined with list (how do you name it “comprehension” ?) is faster than making a lot of tests on a lot of variables with ‘if’ statements + ‘continue’ . Your code is faster if there is at least 100 objects to test and less then 3 clones .

import Blender
from Blender import Noise
   
def AND2():
     print 'AND 2',
     return 1

TEST=[]
USERS=0
for n in range(200000):
  if Noise.random()<0.1:
    TEST.append(1)
    USERS+=1
  else:     
    TEST.append(0)

USERS2=USERS

f=[]
t0=Blender.sys.time()
for t in TEST:
    if  t==0 :
        continue
    if AND2() :
       f.append(t)
       USERS-=1
    if not USERS:
      break
t2= Blender.sys.time()-t0

f=[]
t0=Blender.sys.time()
for t in TEST:
    if  t==0 and AND2() :
       f.append(t)
       USERS2-=1
    if not USERS2:
      break
t3= Blender.sys.time()-t0


print
print 'continue ',t2
print 'and ',t3