forked from johnroper100/CrowdMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcm_utilities.py
159 lines (134 loc) · 6.88 KB
/
cm_utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Copyright 2017 CrowdMaster Developer Team
#
# ##### BEGIN GPL LICENSE BLOCK ######
# This file is part of CrowdMaster.
#
# CrowdMaster is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CrowdMaster is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CrowdMaster. If not, see <http://www.gnu.org/licenses/>.
# ##### END GPL LICENSE BLOCK #####
import time
import bpy
from bpy.props import BoolProperty
from bpy.types import Operator
from .cm_iconLoad import cicon
bpy.types.Scene.show_utilities = BoolProperty(
name="Show or hide the utilities",
description="Show/hide the utilities",
default=False,
options={'HIDDEN'}
)
class Crowdmaster_place_deferred_geo(Operator):
bl_idname = "scene.cm_place_deferred_geo"
bl_label = "Place Deferred Geometry"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
preferences = context.user_preferences.addons[__package__].preferences
groups = bpy.data.groups
objects = context.scene.objects
for group in context.scene.cm_groups:
for agentType in group.agentTypes:
for agent in agentType.agents:
for obj in groups[agent.geoGroup].objects:
if "cm_deferObj" in obj:
newObj = objects[obj["cm_deferObj"]].copy()
materials = obj["cm_materials"]
D = bpy.data
for m in newObj.material_slots:
if m.name in materials:
replacement = materials[m.name]
m.material = D.materials[replacement]
child = False
for con in obj.constraints:
if con.type == "CHILD_OF":
child = True
nCon = newObj.constraints.new("CHILD_OF")
nCon.target = con.target
nCon.subtarget = con.subtarget
nCon.inverse_matrix = con.inverse_matrix
newObj.data.update()
if not child:
newObj.matrix_world = obj.matrix_world
bpy.context.scene.objects.link(newObj)
for user_group in obj.users_group:
user_group.objects.link(newObj)
elif "cm_deferGroup" in obj:
df = obj["cm_deferGroup"]
originalGroup = df["group"]
newObjs = []
if "aName" in df:
aName = df["aName"]
gp = list(groups[originalGroup].objects)
for groupObj in gp:
if groupObj.name != aName:
newObjs.append(groupObj.copy())
else:
newObjs.append(
context.scene.objects[agent.name])
for nObj in newObjs:
if nObj.name == agent.name:
continue
if nObj.parent in gp:
nObj.parent = newObjs[gp.index(
nObj.parent)]
groups[agent.geoGroup].objects.link(nObj)
bpy.context.scene.objects.link(nObj)
if nObj.type == 'MESH' and len(nObj.modifiers) > 0:
for mod in nObj.modifiers:
if mod.type == "ARMATURE":
mod.object = objects[agent.name]
else:
gp = list(groups[originalGroup].objects)
for groupObj in gp:
newObjs.append(groupObj.copy())
for nObj in newObjs:
if nObj.parent in gp:
nObj.parent = newObjs[gp.index(
nObj.parent)]
elif nObj.parent is None:
nObj.parent = obj
groups[agent.geoGroup].objects.link(nObj)
bpy.context.scene.objects.link(nObj)
if "cm_materials" in obj:
materials = obj["cm_materials"]
for nObj in newObjs:
D = bpy.data
for m in nObj.material_slots:
if m.name in materials:
replacement = materials[m.name]
m.material = D.materials[replacement]
return {'FINISHED'}
class Crowdmaster_switch_dupli_group(Operator):
bl_idname = "scene.cm_switch_dupli_groups"
bl_label = "Switch Dupli Groups"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
for obj in bpy.context.selected_objects:
if obj.dupli_type == "GROUP":
suffix = bpy.context.scene.cm_switch_dupli_group_suffix
if obj.dupli_group.name[-len(suffix):] == suffix:
target = bpy.context.scene.cm_switch_dupli_group_target
replaceName = obj.dupli_group.name[:-len(suffix)] + target
replaceSource = obj.dupli_group.library
for grp in bpy.data.groups:
if grp.name == replaceName:
if grp.library == replaceSource:
obj.dupli_group = grp
return {'FINISHED'}
def register():
bpy.utils.register_class(Crowdmaster_place_deferred_geo)
bpy.utils.register_class(Crowdmaster_switch_dupli_group)
def unregister():
bpy.utils.unregister_class(Crowdmaster_place_deferred_geo)
bpy.utils.unregister_class(Crowdmaster_switch_dupli_group)
if __name__ == "__main__":
register()