Confounding, Compounding, Compound issues,
I Beg, I Plea, I do wish you…
could pick up the blend and help with this fix, you?
why would doing the exact same thing 2 times fix it
Parent Code
import bge
from mathutils import Vector
from bge import render
scene=bge.logic.getCurrentScene()
def get_centroid_and_mass(objects):
centroid = Vector()
mass = 0.0
for obj in objects:
if 'Mass' in obj and obj.name!="Parent":
mass += obj['Mass']
print(mass,obj)
for obj in objects:
if 'Mass' in obj and obj.name!="Parent":
influence = obj['Mass'] / mass
centroid += obj.worldPosition * influence
return centroid, mass
def join_objects(obj,target):
children = obj['target_objects']
parent = scene.addObject('Parent', target,0)
centroid, mass = get_centroid_and_mass(children)
print(centroid, mass)
parent.worldPosition = centroid
parent.mass=mass
obj['Root']=parent
obj['RootName']=parent.name
target['Root']=parent
target['RootName']=parent.name
P2=target['Parent']
target.setParent(parent,1,0)
for objects in children:
if objects.name!="Parent" and objects.name!=target:
objects.removeParent()
if objects['Parent']!="Empty":
p=objects['Parent']
objects.setParent(p,0,0)
R=objects['Root']
objects['Root']=parent
objects['RootName']=parent.name
for objects in children:
objects['Children']=objects.childrenRecursive
objects['ChildrenNames']=str(objects['Children'])
target['Children']=target.childrenRecursive
for objects in children:
objects.removeParent()
objects.setParent(parent,1,0)
target.removeParent()
target.setParent(parent,1,0)
if obj.name!="Parent":
obj.setParent(parent, 1, 0)
else:
obj.applyMovement((0,0,-100),0)
##obj.endObject()
parent.worldLinearVelocity=(0,0,0)
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
scene=bge.logic.getCurrentScene()
Mouse = cont.sensors['Mouse']
Timer = cont.sensors['Property1']
##GetComponent A
if Mouse.positive and Timer.positive:
St = Vector ([0,1,0])
Start = own.worldPosition+(own.worldOrientation * St)
##add= scene.addObject("HL",own,10)
##add.worldPosition=Start
St2 = Vector ([0,7.5,0])
End = own.worldPosition+(own.worldOrientation * St2)
##add2= scene.addObject("HL",own,10)
##add2.worldPosition=End
hitOb, hitPoint, hitNormal, hitPoly = own.rayCast(End, Start, 0, 'Tmesh', 0, 1, 1)
render.drawLine(Start,End,(255,0,0))
if hitOb:
H=hitOb
print(H.name)
if 'Tmesh' in H:
if 'Component' in H:
if H.invalid==False:
T=H['Component']
if T.invalid==False:
target=H['Component']
print(target.name)
own['PtargetA']=target
##GetComponent B
t = Vector ([0,1.5,0])
Start2 = target.worldPosition+(target.worldOrientation * St)
##add= scene.addObject("HL",own,10)
##add.worldPosition=Start
St2 = Vector ([0,7.5,0])
End2 = hitOb.worldPosition+(hitOb.worldOrientation * St2)
hitOb2, hitPoint2, hitNormal2, hitPoly2 = hitOb.rayCast(End2, Start2, 0, 'Tmesh', 0, 1, 1)
render.drawLine(Start2,End2,(0,0,255))
if hitOb2:
if 'Tmesh' in hitOb2:
H2=hitOb2
Component_b=H2['Component']
own['PTargetB']=Component_b
## Get jack and check if occupied
if hitPoly2 and hitOb2:
hitMat = hitPoly2.material_name
target['hitMat']=hitMat
own['HitMat']=hitMat
if 'Jack_' in hitMat:
print(hitMat)
if 'hitMat' in Component_b:
if Component_b[hitMat]==False:
target['Parent']=Component_b
target['ParentName']=Component_b.name
print("Jack is empty")
Component_b[hitMat]=True
hitObPos=Component_b.worldPosition
spot=hitObPos+hitNormal2
target.alignAxisToVect(-hitNormal2, 1, 1)
list=[]
target.worldPosition=spot
hOffset = Vector([0,-1 ,0])
target.worldPosition += target.worldOrientation * hOffset
if Component_b['Root']!="Empty":
p=Component_b['Root']
if p.invalid==False:
Component_b=Component_b['Root']
list=Component_b.childrenRecursive
else:
list=[]
list=list+[target]
list=list+[Component_b]
Component_b['target_objects']=list
join_objects(Component_b,target)
main()
unparent
import bge
from mathutils import Vector
scene=bge.logic.getCurrentScene()
##get center of mass
def get_centroid_and_mass(objects):
centroid = Vector()
mass = 0.0
for obj in objects:
if 'Mass' in obj and obj.name!="Parent":
mass += obj['Mass']
print(mass,obj)
for obj in objects:
if 'Mass' in obj and obj.name!="Parent":
influence = obj['Mass'] / mass
centroid += obj.worldPosition * influence
return centroid, mass
##Parent objects to "fake center"
def join_objects(obj):
children = obj['target_objects']
##Add fake root
parent = scene.addObject('Parent', obj, 0)
##get new center of mass and mass
centroid, mass = get_centroid_and_mass(children)
print(centroid, mass)
##Move Fake root to center of mass
parent.worldPosition = centroid
parent.mass = mass
## trying to ditch extra fake centers as ending them crashes blender
for objects in children:
if objects.name!="Parent":
objects.removeParent()
if objects['Parent']!="Empty":
p=objects['Parent']
objects.setParent(p,0,0)
objects['Root']=parent
objects['RootName']=parent.name
for objects in children:
objects['Children']=objects.childrenRecursive
objects['ChildrenNames']=str(objects['Children'])
objects.removeParent()
objects.setParent(parent,1,0)
def main():
cont = bge.logic.getCurrentController()
own = cont.owner
Ray = cont.sensors[ 'Ray' ]
MouseR = cont.sensors[ 'Mouse1']
##selection of target of unparent logic
if Ray.positive and MouseR.positive and own[ 'Parenttimer' ] == 0:
target=Ray.hitObject
## Tmesh object
own['RParentTarget'] = target
## component assosiated with Tmesh flag
Comp=target['Component']
## root assosiated with component
print(Comp.name)
list2=[]
list1=[]
if 'Children' not in target:
target['Children']=[]
if 'Children' in Comp:
list2=Comp['Children']
store=Comp
if Comp['Root']!="Empty":
Root=Comp['Root']
else:
Root=Comp
if Root.children!=[]:
for items in Root.children:
if items!=Comp:
if items not in list2:
if items.name!="Parent":
list1=list1+[items]
if Comp['Parent']!="Empty":
P1=Comp['Parent']
P1[Comp['hitMat']] = False
print(list1)
for items in Root.childrenRecursive:
items.removeParent()
if 'COM' in Root:
Root.endObject()
Comp.removeParent()
Comp.reinstancePhysicsMesh()
Comp.applyMovement((0,-1,.5),1)
for items in list2:
items.removeParent()
list2=list2+[Comp]
Comp['target_objects'] = list2
Comp.removeParent()
print(list2)
store['Parent']="Empty"
if len(list2)>1:
store['Root']="Empty"
join_objects(Comp)
else:
Comp['Root']="Empty"
if len(list1)>1:
Root['target_objects'] = list1
join_objects(Root)
else:
if 'COM' in Root:
Root.endObject()
if 'COM' not in Root:
Root['Root']="Empty"
main()