Skip to content

Commit

Permalink
Constrain bone node
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter-Noble committed Jun 21, 2017
1 parent a403fee commit 6b53ba6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
27 changes: 24 additions & 3 deletions cm_generation/cm_genNodes.py
Expand Up @@ -164,9 +164,6 @@ class LinkGroupNode(CrowdMasterAGenTreeNode):
constrainBone = StringProperty(name="Constrain Bone")

def init(self, context):
self.inputs.new('GeoSocketType', "Objects")
self.inputs[0].link_limit = 1

self.outputs.new('GeoSocketType', "Objects")

def draw_buttons(self, context, layout):
Expand All @@ -182,6 +179,27 @@ def getSettings(self):
"constrainBone": self.constrainBone}


class ConstrainBoneNode(CrowdMasterAGenTreeNode):
bl_idname = 'ConstrainNodeType'
bl_label = 'ConstrainBone'
bl_icon = 'SOUND'
bl_width_default = 160.0

def init(self, context):
self.inputs.new('GeoSocketType', "Parent Group")
self.inputs.new('GeoSocketType', "Child Object")
self.inputs[0].link_limit = 1
self.inputs[1].link_limit = 1

self.outputs.new('GeoSocketType', "Objects")

def draw_buttons(self, context, layout):
pass

def getSettings(self):
return {}


class ModifyBoneNode(CrowdMasterAGenTreeNode):
bl_idname = 'ModifyBoneNodeType'
bl_label = 'Modify Bone'
Expand Down Expand Up @@ -1105,6 +1123,7 @@ def poll(cls, context):
CrowdMasterAGenCategories("geometry", "Geometry", items=[
NodeItem("GroupInputNodeType"),
NodeItem("LinkGroupNodeType"),
NodeItem("ConstrainNodeType"),
NodeItem("ModifyBoneNodeType"),
NodeItem("ObjectInputNodeType"),
NodeItem("ParentNodeType"),
Expand Down Expand Up @@ -1153,6 +1172,7 @@ def register():
bpy.utils.register_class(ObjectInputNode)
bpy.utils.register_class(GroupInputNode)
bpy.utils.register_class(LinkGroupNode)
bpy.utils.register_class(ConstrainBoneNode)
bpy.utils.register_class(ModifyBoneNode)
bpy.utils.register_class(GeoSwitchNode)
bpy.utils.register_class(TemplateSwitchNode)
Expand Down Expand Up @@ -1198,6 +1218,7 @@ def unregister():
bpy.utils.unregister_class(ObjectInputNode)
bpy.utils.unregister_class(GroupInputNode)
bpy.utils.unregister_class(LinkGroupNode)
bpy.utils.unregister_class(ConstrainBoneNode)
bpy.utils.unregister_class(ModifyBoneNode)
bpy.utils.unregister_class(GeoSwitchNode)
bpy.utils.unregister_class(TemplateSwitchNode)
Expand Down
57 changes: 38 additions & 19 deletions cm_generation/cm_templates.py
Expand Up @@ -333,9 +333,6 @@ def duplicateProxyLink(dupDir, sourceBlend, sourceGroup, sourceRig):

class GeoTemplateLINKGROUPNODE(GeoTemplate):
def build(self, buildRequest):
gret = self.inputs["Objects"].build(buildRequest)
obj = gret.obj

blendfile = os.path.split(bpy.data.filepath)[0]
for d in self.settings["groupFile"][2:].split("/"):
if d == "..":
Expand All @@ -353,45 +350,66 @@ def build(self, buildRequest):
group = self.settings["groupName"]
rigObject = self.settings["rigObject"]

newObj, newRig = duplicateProxyLink(
dupDir, blendfile, group, rigObject)
newObj, newRig = duplicateProxyLink(dupDir, blendfile, group, rigObject)
buildRequest.group.objects.link(newObj)
buildRequest.group.objects.link(newRig)

constrainBone = newRig.pose.bones[self.settings["constrainBone"]]
gret = GeoReturn(newObj)

gret.overwriteRig = newRig
gret.constrainBone = newRig.pose.bones[self.settings["constrainBone"]]

return gret

def check(self):
return True


class GeoTemplateCONSTRAINBONE(GeoTemplate):
def build(self, buildRequest):
gretp = self.inputs["Parent Group"].build(buildRequest.copy())
gret = self.inputs["Child Object"].build(buildRequest.copy())

boneName = gret.constrainBone.name

newRig = gret.overwriteRig
constrainBone = gret.constrainBone

lastActive = bpy.context.scene.objects.active
bpy.context.scene.objects.active = newRig
bpy.ops.object.posemode_toggle()
armature = bpy.data.armatures[rigObject].bones
armature.active = armature[self.settings["constrainBone"]]
armature = newRig.data.bones
armature.active = armature[boneName]
bpy.ops.pose.constraint_add(type="COPY_LOCATION")
bpy.ops.pose.constraint_add(type="COPY_ROTATION")

Cloc = newRig.pose.bones[self.settings["constrainBone"]
].constraints[-2]
Crot = newRig.pose.bones[self.settings["constrainBone"]
].constraints[-1]
Cloc = newRig.pose.bones[boneName].constraints[-2]
Crot = newRig.pose.bones[boneName].constraints[-1]

Cloc.target = obj
Cloc.target = gretp.obj
Cloc.use_z = False

Crot.target = obj
Crot.target = gretp.obj
# Crot.use_offset = True

bpy.ops.object.posemode_toggle()
bpy.context.scene.objects.active = lastActive

gret.overwriteRig = newRig
gret.constrainBone = newRig.pose.bones[self.settings["constrainBone"]]
gretp.overwriteRig = newRig
gretp.constrainBone = newRig.pose.bones[boneName]

return gret
return gretp

def check(self):
if "Objects" not in self.inputs:
if "Parent Group" not in self.inputs:
return False
if not isinstance(self.inputs["Objects"], GeoTemplate):
if "Child Object" not in self.inputs:
return False
if not isinstance(self.inputs["Parent Group"], GeoTemplate):
return False
if not isinstance(self.inputs["Child Object"], GeoTemplate):
return False
# TODO check that object is in parent group
return True


Expand Down Expand Up @@ -1213,6 +1231,7 @@ def check(self):
("ObjectInputNodeType", GeoTemplateOBJECT),
("GroupInputNodeType", GeoTemplateGROUP),
("LinkGroupNodeType", GeoTemplateLINKGROUPNODE),
("ConstrainNodeType", GeoTemplateCONSTRAINBONE),
("ModifyBoneNodeType", GeoTemplateMODIFYBONE),
("GeoSwitchNodeType", GeoTemplateSWITCH),
("AddToGroupNodeType", TemplateADDTOGROUP),
Expand Down

0 comments on commit 6b53ba6

Please sign in to comment.